我如何表示 PyTorch LSTM 3D 张量?
How do I represent a PyTorch LSTM 3D Tensor?
根据 the docs,我看到 Pytorch’s LSTM expects all of its inputs to be 3D tensors.
我正在尝试做一个简单的序列到序列 LSTM,我有:
class BaselineLSTM(nn.Module):
def __init__(self):
super(BaselineLSTM, self).__init__()
self.lstm = nn.LSTM(input_size=100, hidden_size=100)
def forward(self, x):
print('x', x)
x = self.lstm(x)
return x
我的 x.size()
是 torch.Size([100, 1])
。我希望我以某种方式需要第三维,但我不确定它的实际含义。任何帮助将不胜感激。
输入形状在 this Pytorch 文档的 Inputs: input, (h_0, c_0)
部分中有进一步详细说明。输入张量的第一维预计对应序列长度,第二维对应批量大小,第三维对应输入大小。
因此对于您的示例,输入张量 x
实际上应该是大小 (seq_length, batch_size, 100)
。
这里是 Pytorch 论坛上的一个详细线程,以了解更多详细信息:https://discuss.pytorch.org/t/why-3d-input-tensors-in-lstm/4455/9
根据 the docs,我看到 Pytorch’s LSTM expects all of its inputs to be 3D tensors.
我正在尝试做一个简单的序列到序列 LSTM,我有:
class BaselineLSTM(nn.Module):
def __init__(self):
super(BaselineLSTM, self).__init__()
self.lstm = nn.LSTM(input_size=100, hidden_size=100)
def forward(self, x):
print('x', x)
x = self.lstm(x)
return x
我的 x.size()
是 torch.Size([100, 1])
。我希望我以某种方式需要第三维,但我不确定它的实际含义。任何帮助将不胜感激。
输入形状在 this Pytorch 文档的 Inputs: input, (h_0, c_0)
部分中有进一步详细说明。输入张量的第一维预计对应序列长度,第二维对应批量大小,第三维对应输入大小。
因此对于您的示例,输入张量 x
实际上应该是大小 (seq_length, batch_size, 100)
。
这里是 Pytorch 论坛上的一个详细线程,以了解更多详细信息:https://discuss.pytorch.org/t/why-3d-input-tensors-in-lstm/4455/9