简体   繁体   English

PyTorch LSTM尺寸

[英]PyTorch LSTM dimension

I am trying to teach my first LSTM with numeric sequence data to predict a singular value.我正在尝试用数字序列数据教我的第一个 LSTM 来预测奇异值。

My training set is a numpy matrix shape=207x7 where each input vector has 7 features.我的训练集是一个 numpy 矩阵 shape=207x7,其中每个输入向量有 7 个特征。 I am not sure how to setup and train my LSTM properly.我不确定如何正确设置和训练我的 LSTM。 I have CNN experience, but now first 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)

But I get this error:但我得到这个错误:

Expected hidden[0] size (1, 200, 100), got (1, 1, 100)
  File "train_lstm.py", line 44, in forward

Your input has size [batch_size, seq_len, num_features] = [1, 200, 7] .您的输入大小为[batch_size, seq_len, num_features] = [1, 200, 7] The LSTM on the other hand, expects the input to have size [seq_len, batch_size, num_features] (as described in nn.LSTM - Inputs ).另一方面,LSTM 期望输入的大小为[seq_len, batch_size, num_features] (如nn.LSTM - Inputs中所述)。

You can either change the dimensions of your input, or you can set batch_first=True when creating the LSTM, if you prefer having batch size as the first dimension, in which case batch_size and seq_len are swapped and your current input size would be the expected one.您可以更改输入的尺寸,也可以在创建 LSTM 时设置batch_first=True ,如果您更喜欢将批量大小作为第一个维度,在这种情况下, batch_sizeseq_len被交换并且您当前的输入大小将是预期的一。

self._lstm = nn.LSTM(input_size, hidden_layer_size, batch_first=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM