Lua 如何删除Torch中的类变量?
我在理解Torch中的类变量如何工作时遇到问题 我做了以下工作:Lua 如何删除Torch中的类变量?,lua,torch,Lua,Torch,我在理解Torch中的类变量如何工作时遇到问题 我做了以下工作: mydata=torch.class('something') 我通过键入who()检查用户变量,它显示: == User Variables == [_RESULT] = table - size: 0 [mydata] = table - size: 0 [something] = table - size: 0 首先,我试图通过 mydata=nil 它起作用了mydata现在
mydata=torch.class('something')
我通过键入who()
检查用户变量,它显示:
== User Variables ==
[_RESULT] = table - size: 0
[mydata] = table - size: 0
[something] = table - size: 0
首先,我试图通过
mydata=nil
它起作用了mydata
现在被释放,可以重新初始化为任何值。但是当我试图通过键入来删除变量something
soemthing=nil
即使变量something
不再列在who()
中,它似乎也不起作用。当我尝试时:
mydata2=torch.class('something')
错误弹出:
/data/torch/install/share/lua/5.1/torch/init.lua:65: something has been already assigned a factory
stack traceback:
[C]: in function 'newmetatable'
/data/torch/install/share/lua/5.1/torch/init.lua:65: in function 'class'
[string "mydata2=torch.class('something')"]:1: in main chunk
[C]: in function 'xpcall'
/data/torch/install/share/lua/5.1/trepl/init.lua:648: in function 'repl'
/data/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
[C]: at 0x00406670
谁能告诉我背后的原因吗?torch.class()将类元表存储在lua注册表中,请参阅,并将函数存储在torch C-backend中
为了注销现有类,必须从lua注册表中删除该项。在函数的帮助下,您可以从lua访问注册表
使用注册表中的removeal,您的示例可以工作:
mydata = torch.class('something')
mydata = nil
soemthing = nil
-- remove the global registration:
debug.getregistry()['something'] = nil
-- now it is possible to register the class again
mydata2 = torch.class('something')
正是我想要的。谢谢