PyToch Skorch通过3维输入

时间:2018-06-21 12:38:23

标签: python scikit-learn pytorch skorch

我正在尝试使用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.

1 个答案:

答案 0 :(得分:0)

就像您自己说的那样,传递2D数组是行不通的,因为那时没有时间维度,因此在这种情况下错误消息是正确的(LSTM期望3维)。

令人困惑的部分是传递3D数组时的错误消息。发生的情况是您将数据格式化为(sequence, batch, feature),即“批处理第二个”。还有一种称为“批量优先”的替代格式,其中数据格式为(batch, sequence, feature),这是sklearn(因此是skorch)的默认格式。如果您以这种方式重新格式化数据并将LSTM配置为使用这种格式(通过batch_first=True),则该错误将消失:

    self.rnn = nn.LSTM(
        input_size,
        hidden_size,
        num_layers,
        batch_first=True,   
    )
# ...
inputArr3d = np.random.rand(batch, seq, features)

请注意,您的模块还需要转换RNN的返回值 转化为以下几层可以处理的内容(即展平 时间维度),否则线性层将不知道 与额外的时间维度有关。