我已经扩展nn.Module
以实现我的网络,其转发功能是这样的......
def forward(self, X, **kwargs):
batch_size, seq_len = X.size()
length = kwargs['length']
embedded = self.embedding(X) # [batch_size, seq_len, embedding_dim]
if self.use_padding:
if length is None:
raise AttributeError("Length must be a tensor when using padding")
embedded = nn.utils.rnn.pack_padded_sequence(embedded, length, batch_first=True)
#print("Size of Embedded packed", embedded[0].size())
hidden, cell = self.init_hidden(batch_size)
if self.rnn_unit == 'rnn':
out, _ = self.rnn(embedded, hidden)
elif self.rnn_unit == 'lstm':
out, (hidden, cell) = self.rnn(embedded, (hidden, cell))
# unpack if padding was used
if self.use_padding:
out, _ = nn.utils.rnn.pad_packed_sequence(out, batch_first = True)
我像这样初始化了一个skorch NeuralNetClassifier
,
net = NeuralNetClassifier(
model,
criterion=nn.CrossEntropyLoss,
optimizer=Adam,
max_epochs=8,
lr=0.01,
batch_size=32
)
现在,如果我调用net.fit(X, y, length=X_len)
它会引发错误
TypeError: __call__() got an unexpected keyword argument 'length'
根据文档 fit 函数需要一个fit_params
字典,
**fit_params : dict Additional parameters passed to the ``forward`` method of the module and to the ``self.train_split`` call.
并且源代码总是将我的参数发送到train_split
显然我的关键字参数不会被识别的地方。
有没有办法将参数传递给我的转发函数?