2015-11-17 22 views
1

我在理解Torch中類變量的工作原理方面存在問題。如何刪除火炬中的類變量?

我做了以下內容:

mydata=torch.class('something') 

我通過鍵入who()檢查用戶變量,它表明:

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

我首先是試圖通過

mydata=nil 
刪除 mydata它的工作原理是

mydata現在被釋放並可以重新初始化爲任何值。但是,當我試圖通過鍵入

soemthing=nil 

刪除該變量something看來它不是雖然可變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 

誰能告訴我,這背後的原因是什麼?

回答

3

torch.class()將類metatable存儲在lua-registry中,參見http://www.lua.org/pil/27.3.1.html和火炬C後端的luaT_lua_newmetatable()函數。

爲了註銷現有的類,有必要請從LUA的註冊表項。您可以通過debug.getregistry()函數的幫助來訪問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') 
+0

正是我想要的。謝謝 – ZijunLost