如何在Torch中删除类变量?

时间:2015-11-17 22:17:09

标签: lua torch

我在理解Torch中的类变量如何工作方面遇到了问题。

我做了以下事情:

mydata=torch.class('something')

我通过输入who()检查了用户变量,并显示:

== User Variables ==
[_RESULT]        = table - size: 0
[mydata]         = table - size: 0
[something]      = table - size: 0

我首先尝试通过

删除mydata
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

有人能告诉我背后的原因吗?

1 个答案:

答案 0 :(得分:3)

torch.class()将类metatable存储在lua-registry中,请参阅火炬C-backend中的http://www.lua.org/pil/27.3.1.htmlluaT_lua_newmetatable()函数。

要取消注册现有类,必须从lua-registry中删除该条目。您可以借助debug.getregistry()函数从lua访问注册表。

从注册表中移除您的示例:

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')