python - 使 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])
,我的hidden
和cell
有维度torch.Size([2, 15, 161])
。
但是当我设置时bidirectional=True
,我要做的第一件事就是在我的init_hidden
,更改self.num_layers
为self.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')
。那么我做错了什么?
解决方案
推荐阅读
- user-interface - 颤振持续时间用户输入
- android - 如果我从 ndk.abiFilters 中删除 'armeabi-v7a'、'x86' 会有什么问题吗?
- data-modeling - 如何限制 OWL 本体中的关系
- c# - 根据输入数字查找十进制值
- windows - 如何仅使用脚本获取最后 5 个字符的 Office 许可证密钥
- c# - 统一坠落精灵
- javascript - 计算年龄并检查值是否正常
- python-3.x - 谷歌云功能:达到 --max-instances 时的行为
- google-cloud-platform - gsutil 和 gcloud 有什么区别?
- php - 如何在 TWIG 中获取 Cookie 值?