我想實現在Lua一類,這是應該被實例化的第一個參數。但是,在輸入參數BatchLoader.init
的輸入參數發生偏移時出現錯誤。我是否缺少將self
關鍵字作爲參數輸入到Python中的任何成員函數中,或者類定義是否還有其他錯誤?的Lua:類方法跳過
整個classcode看起來像這樣(我剝出不重要的事情):
function BatchLoader:new()
setmetatable({}, BatchLoader)
self.epoch_done = false
return self
end
function BatchLoader:init(X, y, sids, batch_size, argshuffle)
print("In batchloader")
print(X:size()) -- Prints size(62000, 1) [which is 'y']
print(y:size()) -- Prints size(62000, 1) [which is 'sids']
print(sids) -- Prints '10' [which is 'batch_size']
end
但是,當我創建這個類,然後調用函數BatchLoader.init
似乎y被解釋爲X,Y被解釋爲sids等。因此,batch_size
的輸入被解釋爲sids
,其中每個參數都從實際參數'移開'。
下面的代碼是我正確的進入是BatchLoader.init
之前調用。這裏的一切都按預期打印。
local BatchLoader = require "../datahandler/BatchLoader.lua"
local trainLoader = BatchLoader:new()
print(X_data[{{1, 62000}, {}, {}}]:size()) -- Prints size(62000, 8, 16)
print(y_data[{{1, 62000}, {}}]:size()) -- Prints size(62000, 1)
print(sid_data[{{1, 62000}, {}}]:size()) -- Prints size(62000, 1)
local X_train, y_train, sid_train = trainLoader.init(
X_data[{{1, a}, {}, {}}],
y_data[{{1, a}, {}}],
sid_data[{{1, a}, {}}],
10, true)
我的問題是:那裏的類聲明有什麼問題嗎?這是我在Lua的第一個OOP代碼,所以任何想法或幫助表示讚賞! :)
你已經用冒號(不帶點)定義了'BatchLoader:init',所以你必須使用帶有冒號的'trainLoader:init'。 –
非常感謝,我的錯誤,太愚蠢了! – DaveTheAl
您還可以編寫如下代碼:function Foo.init(...)local _,a,b if ... == Foo then _,a,b = ... else a,b = ... end print (a,b)結束' – moteus