首页 > 解决方案 > 使 PyTorch LSTM bidirectional=True 原因:张量 a (322) 的大小必须与非单维 2 处的张量 b (161) 的大小相匹配

问题描述

我的模型是:

class BaselineModel(nn.Module):
    def __init__(self, feature_dim=5, hidden_size=5, num_layers=2, seq_length=1, dropout=0.1):
        super(BaselineModel, self).__init__()
        self.num_layers = num_layers
        self.hidden_size = hidden_size
        self.seq_length = seq_length

        self.lstm = nn.LSTM(input_size=feature_dim,
                            hidden_size=hidden_size, num_layers=num_layers, dropout=0.1, bidirectional=False)

    def forward(self, x, hidden=None):
        lstm_out, hidden = self.lstm(x, hidden)
        return lstm_out, hidden

    def init_hidden(self, batch_size):
        hidden = torch.zeros(
            self.num_layers, self.seq_length, self.hidden_size)
        cell = torch.zeros(self.num_layers, self.seq_length,
                           self.hidden_size)
        return (hidden.float(), cell.float())

一切正常。我的输入和输出有维度torch.Size([32, 15, 161]),我的hiddencell有维度torch.Size([2, 15, 161])

但是当我设置时bidirectional=True,我要做的第一件事就是在我的init_hidden,更改self.num_layersself.num_layers * 2。这很好。但是当我执行我的训练循环时,我得到:

The size of tensor a (322) must match the size of tensor b (161) at non-singleton dimension 2

它指的是具有loss = loss_fn(pred, outputs). 如果这很重要,我正在使用loss_fn = torch.nn.MSELoss(reduction='sum')。那么我做错了什么?

标签: pythonpytorchlstmbidirectional

解决方案


推荐阅读