我试图在使用 skorch 时在 PyTorch 中拟合模型。我的问题是,我的模型使用 LSTM 层,它需要 3d 输入,但我不知道如何正确传递输入。
将 2d 数组传递给fit()
我从 PyTorch 收到预期 3d 输入的错误。如果传递一个 3d 数组,我会从fit()
长度不一致的方法中得到错误(这对我来说都很有意义)。
下面的示例代码:
import numpy as np
import torch
from torch import nn
import skorch
class lstmNet(nn.Module):
def __init__(self, input_size, hidden_size, num_layers):
super(lstmNet, self).__init__()
self.rnn = nn.LSTM(
input_size,
hidden_size,
num_layers )
self.lin = nn.Linear(
in_features=hidden_size,
out_features=1
)
def forward(self, x):
print(x.size())
out, hn = self.rnn(x)
out = self.lin(out)
return out
input_feat = 5
hidden_size = 10
lstmLayers = 2
seq = 20
batch = 30
features = 5
net = skorch.NeuralNet(
module=lstmNet(
input_size=input_feat,
hidden_size=hidden_size,
num_layers=lstmLayers
),
criterion=torch.nn.MSELoss,
optimizer=torch.optim.SGD,
lr=0.1,
max_epochs=10
)
#inputArr2d = np.random.rand(seq * batch, features)
inputArr3d = np.random.rand(seq, batch, features)
print('input:\n {}\nshape: {}'.format(inputArr3d, inputArr3d.shape))
targetArr = np.random.rand((seq * batch))
#print('target:\n {}\nshape: {}'.format(targetArr, targetArr.shape))
net.fit(X=inputArr3d, y=targetArr)
这是调用时的错误net.fit(X=inputArr2d, y=targetArr)
:
Traceback (most recent call last):
File "C:\Spielplatz\Python\examples\playground.py", line 64, in <module>
net.fit(X=inputArr2d, y=targetArr)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 686, in fit
self.partial_fit(X, y, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 646, in partial_fit
self.fit_loop(X, y, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 584, in fit_loop
step = self.train_step(Xi, yi, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 507, in train_step
y_pred = self.infer(Xi, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 810, in infer
return self.module_(x, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "C:\Spielplatz\Python\examples\playground.py", line 33, in forward
out, hn = self.rnn(x)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\rnn.py", line 178, in forward
self.check_forward_args(input, hx, batch_sizes)
File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\rnn.py", line 126, in check_forward_args
expected_input_dim, input.dim()))
RuntimeError: input must have 3 dimensions, got 2
这是调用时的错误net.fit(X=inputArr3d, y=targetArr)
:
Traceback (most recent call last):
File "C:\Spielplatz\Python\examples\playground.py", line 64, in <module>
net.fit(X=inputArr3d, y=targetArr)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 686, in fit
self.partial_fit(X, y, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 646, in partial_fit
self.fit_loop(X, y, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 573, in fit_loop
X, y, **fit_params)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 1004, in get_split_datasets
dataset = self.get_dataset(X, y)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\net.py", line 961, in get_dataset
return dataset(X, y, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\skorch\dataset.py", line 104, in __init__
raise ValueError("X and y have inconsistent lengths.")
ValueError: X and y have inconsistent lengths.