为手电筒 类 设置 __index
Setting __index for Torch Classes
是否可以为 torch classes 设置一个 __index 方法?我已尝试实现一个简单的 dataset
class,如 Deep Learning with Torch 教程中所述:(ipynb here)
trainset = {
inputs = {0, 1, 1, 0},
targets = {1, 1, 1, 0}
}
index = function(t, i)
return {t.inputs[i], t.targets[i]}
end
setmetatable(trainset, {
__index = index
)
这允许你做 trainset[1]]
其中 returns {0, 1}
.
但是,将此作为 torch class 实现是行不通的。
local torch = require("torch")
do
Dataset = torch.class("Dataset")
function Dataset:__init(i, t)
self.inputs = i
self.targets = t
end
function Dataset.__index(t, v)
print("inside index")
return {
rawget(t, inputs)[v],
rawget(t, targets)[v]
}
end
end
Dataset({0, 1, 1, 0}, {1, 1, 1, 0}) -- fails
似乎在创建对象时,__index()
被调用但失败了,因为 index
和 targets
尚未创建。如果不使用rawget
,则会导致堆栈溢出。
我对 Lua 的理解是有限的,但我很惊讶地看到 __index()
在对象创建过程中被调用:我认为幕后发生了一些我不完全理解的事情。
Torch类全部实现__index
,会在元表中寻找__index__
,用于重载
来自docs:
If one wants to provide index or newindex in the metaclass,
these operators must follow a particular scheme:
index must either return a value and true or return false only. In the first case, it means index was able to handle the given
argument (for e.g., the type was correct). The second case means it
was not able to do anything, so __index in the root metatable can then
try to see if the metaclass contains the required value.
这意味着对于示例,__index__
(不是 __index
!)方法必须检查是否 type(v) == "number"
,如果不是,则 return false
所以__index
可以在对象元表中查找值。
local torch = require("torch")
do
Dataset = torch.class("Dataset")
function Dataset:__init(i, t)
self.inputs = i
self.targets = t
end
function Dataset.__index__(t, v)
if type(v) == "number" then
local tbl = {
t.inputs[v],
t.targets[v]
}
return tbl, true
else
return false
end
end
local dset = Dataset({0, 1, 1, 0}, {1, 1, 1, 0})
dset[1] --> {0, 1}
是否可以为 torch classes 设置一个 __index 方法?我已尝试实现一个简单的 dataset
class,如 Deep Learning with Torch 教程中所述:(ipynb here)
trainset = {
inputs = {0, 1, 1, 0},
targets = {1, 1, 1, 0}
}
index = function(t, i)
return {t.inputs[i], t.targets[i]}
end
setmetatable(trainset, {
__index = index
)
这允许你做 trainset[1]]
其中 returns {0, 1}
.
但是,将此作为 torch class 实现是行不通的。
local torch = require("torch")
do
Dataset = torch.class("Dataset")
function Dataset:__init(i, t)
self.inputs = i
self.targets = t
end
function Dataset.__index(t, v)
print("inside index")
return {
rawget(t, inputs)[v],
rawget(t, targets)[v]
}
end
end
Dataset({0, 1, 1, 0}, {1, 1, 1, 0}) -- fails
似乎在创建对象时,__index()
被调用但失败了,因为 index
和 targets
尚未创建。如果不使用rawget
,则会导致堆栈溢出。
我对 Lua 的理解是有限的,但我很惊讶地看到 __index()
在对象创建过程中被调用:我认为幕后发生了一些我不完全理解的事情。
Torch类全部实现__index
,会在元表中寻找__index__
,用于重载
来自docs:
If one wants to provide index or newindex in the metaclass, these operators must follow a particular scheme:
index must either return a value and true or return false only. In the first case, it means index was able to handle the given argument (for e.g., the type was correct). The second case means it was not able to do anything, so __index in the root metatable can then try to see if the metaclass contains the required value.
这意味着对于示例,__index__
(不是 __index
!)方法必须检查是否 type(v) == "number"
,如果不是,则 return false
所以__index
可以在对象元表中查找值。
local torch = require("torch")
do
Dataset = torch.class("Dataset")
function Dataset:__init(i, t)
self.inputs = i
self.targets = t
end
function Dataset.__index__(t, v)
if type(v) == "number" then
local tbl = {
t.inputs[v],
t.targets[v]
}
return tbl, true
else
return false
end
end
local dset = Dataset({0, 1, 1, 0}, {1, 1, 1, 0})
dset[1] --> {0, 1}