如何使用 torch 的 class 系统创建自定义 class

How do I use torch's class system to create a custom class

我正试图关注 torch documentation for utility functions

我做了以下事情:

Blah = torch.class('Blah')
function Blah:__init(); end
blah = Blah()

但是我得到以下错误:

attempt to call global 'Blah' (a table value)

我期待 __init() 函数通过 __call 元表机制以某种方式工作,但 Blah 似乎甚至没有元表:

th> getmetatable(Blah) == nil
true

也许文档已经过时了?但是火炬似乎在内部以这种方式创造了大量 类。

我刚刚更新到最新的 torch,所以我知道不是我的 torch 版本太旧了...

想法?

do
 local Blah = torch.class('Blah')
 function Blah:__init() end
end

blah = Blah()

1. 你需要:

local Blah = torch.class('Blah')

2. 你需要使用:

do 
end 

lexical scoping,如果你想从 same 模块调用 class 'Blah'。 但是,如果您从 other 模块调用它 - 正如我们通常使用 class 所做的那样 -,我们不需要使用 do-end lexical scoping

因此,如果您的模块的目的只是声明一个手电筒类型 class,然后在其他模块中多次使用它,您只需要像上面 [=34= 部分中那样声明为 local ]1. 而你不需要(但你 可以do-end lexical scoping.

实际上torch documentation备注:

-- for naming convenience

我想这里有点误导。