PyTorch LSTM尺寸

问题描述 投票:0回答:1

我正在尝试用数字序列数据教我的第一个LSTM以预测奇异值。

我的训练集是一个numpy矩阵shape = 207x7,其中每个输入向量都有7个特征。我不确定如何正确设置和训练我的LSTM。我有CNN经验,但是现在是LSTM。

class LSTM_NN(nn.Module):
"""
Stores the network format
"""
def __init__(self, input_size=7, hidden_layer_size=100, output_size=1):
    super(self.__class__, self).__init__()
    self._hidden_layer_size = hidden_layer_size
    self._lstm = nn.LSTM(input_size, hidden_layer_size)
    self._hidden_cell = (torch.zeros(1, 1, self._hidden_layer_size),
                         torch.zeros(1, 1, self._hidden_layer_size))
    self._linear = nn.Linear(hidden_layer_size, output_size)

def forward(self, input_data):
    """
    Forward propagation
    """

    lstm_out, self._hidden_cell = self._lstm(
        torch.tensor(np.expand_dims(input_data, 0)),
        self._hidden_cell) 

training_data.shape
# (200, 7)
model = LSTM_NN(input_size=training_data.shape[1])
model.forward(training_data)

但我收到此错误:

Expected hidden[0] size (1, 200, 100), got (1, 1, 100)
  File "train_lstm.py", line 44, in forward
python pytorch lstm
1个回答
1
投票

您的输入的大小为[[batch_size,seq_len,num_features] = [1,200,7]] >>。另一方面,LSTM期望输入的大小为[[seq_len,batch_size,num_features](如nn.LSTM - Inputs中所述)。

您可以更改输入的尺寸,也可以在创建LSTM时设置nn.LSTM,如果您希望将批量大小作为第一维,则在这种情况下,batch_size

seq_len是交换,您当前的输入大小将是预期的大小。
batch_first=True    
© www.soinside.com 2019 - 2024. All rights reserved.