【问题标题】:Pytorch hidden cell LSTM with mini-batches带有小批量的 Pytorch 隐藏单元 LSTM
【发布时间】:2020-03-26 15:45:42
【问题描述】:

问题

我不明白在小批量训练时如何处理 LSTM 隐藏单元,因为训练数据以 n 个序列批量发送到网络,而只处理 1 个序列每次测试期间。

代码

具体来说,我的网络是:

class Pytorch_LSTM(nn.Module):
    def __init__(self, params):
        super(Pytorch_LSTM, self).__init__()
        self.params = params
        self.hidden_layer_size = params['hidden_layer_size']
        # Define layers
        self.lstm = nn.LSTM(input_size = params['in_features'], hidden_size = params['hidden_layer_size'])        
        self.linear1 = nn.Linear(params['hidden_layer_size'], params['hidden_layer_size'])
        self.linear2 = nn.Linear(params['hidden_layer_size'], params['out_features'])
        self.hidden_cell = (torch.zeros(1,self.params['batch_size'],self.hidden_layer_size),
                           torch.zeros(1,self.params['batch_size'],self.hidden_layer_size))

    def forward(self, input_seq):        
        lstm_out, self.hidden_cell = self.lstm(input_seq.view(self.params['time_window'],-1,self.params['in_features']), self.hidden_cell)
        linear1_out = self.linear1(lstm_out)
        predictions = self.linear2(linear1_out)
        return predictions[-1]

在我的train() 方法中:

def train(self, input_sequence, params, test_idx, final, verbose=True):        

        ....
        ....

        # Model
        self.model = Pytorch_LSTM(params)
        # Let's train the model
        for epoch in range(epochs):
            for count_1,seq in enumerate(train_data_batch):      
                optimizer.zero_grad()
                self.model.hidden_cell = (torch.zeros(1, params['batch_size'], self.model.hidden_layer_size),
                                          torch.zeros(1, params['batch_size'], self.model.hidden_layer_size))   
                y_pred = self.model(seq)     # seq.shape: (n_batches, 25, 4)
                single_loss = mse_loss(y_pred, y_label)    # y_pred.shape, y_label.shape : (batch_size, 4)

我相信这是对模型的小批量训练。
当我测试它时,我每次只有一个序列,而不是多个批次。在我的test()

for count,seq in enumerate(val_data[j]):                   
    y_pred = self.model(seq)           # seq.shape: (25,4)
    single_loss = mse_loss(y_pred, y_label)

这会返回错误:

RuntimeError: Expected hidden[0] size (1, 1, 100), got (1, 704, 100)

n_batches= 704。

我应该如何处理 hidden_​​cell?

【问题讨论】:

    标签: python machine-learning pytorch lstm recurrent-neural-network


    【解决方案1】:

    您在每次调用 shape (1, batch_size, 100) 时将 (h_0, c_0) 参数传递给 lstm。 batch_size 用于并行处理并且是任意的,但您每次都在进行硬编码

    self.hidden_cell = (torch.zeros(1,self.params['batch_size'],self.hidden_layer_size),
                        torch.zeros(1,self.params['batch_size'],self.hidden_layer_size))
    

    这个hidden_cellh_0c_0 参数,即隐藏和单元状态的初始值。

    尝试传递 (1, batch_size, 100) 大小的数组是不必要的,因为 it defaults to zero 本身就是所需大小的向量。

    只需去掉 self.hidden_cell 并仅将 input_seq 传递给 forward 方法中的 self.lstm。它应该工作

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2019-06-02
      • 2020-11-13
      • 2019-07-01
      • 2018-10-05
      • 2018-06-26
      • 1970-01-01
      • 2019-05-10
      • 2017-10-23
      相关资源
      最近更新 更多