如何表示Pytork LSTM三维张量?

2024-04-18 19:02:37 发布

您现在位置:Python中文网/ 问答频道 /正文

根据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])。我想我需要一个三维空间,但我不确定它到底意味着什么。任何帮助都将不胜感激


Tags: oftheselfdocssizeinitdefnn
1条回答
网友
1楼 · 发布于 2024-04-18 19:02:37

输入形状在thisPytorch文档的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

相关问题 更多 >