问题
我正在尝试使用 LSTM 构建文本分类器网络。我得到的错误是:
RuntimeError: Expected hidden[0] size (4, 600, 256), got (4, 64, 256)
细节
数据是json,看起来像这样:
{"cat": "music", "desc": "I'm in love with the song's intro!", "sent": "h"}
我torchtext
用来加载数据。
from torchtext import data
from torchtext import datasets
TEXT = data.Field(fix_length = 600)
LABEL = data.Field(fix_length = 10)
BATCH_SIZE = 64
fields = {
'cat': ('c', LABEL),
'desc': ('d', TEXT),
'sent': ('s', LABEL),
}
我的 LSTM 看起来像这样
EMBEDDING_DIM = 64
HIDDEN_DIM = 256
N_LAYERS = 4
MyLSTM(
(embedding): Embedding(11967, 64)
(lstm): LSTM(64, 256, num_layers=4, batch_first=True, dropout=0.5)
(dropout): Dropout(p=0.3, inplace=False)
(fc): Linear(in_features=256, out_features=8, bias=True)
(sig): Sigmoid()
)
我最终得到了以下尺寸inputs
和labels
batch = list(train_iterator)[0]
inputs, labels = batch
print(inputs.shape) # torch.Size([600, 64])
print(labels.shape) # torch.Size([100, 2, 64])
我初始化的隐藏张量看起来像:
hidden # [torch.Size([4, 64, 256]), torch.Size([4, 64, 256])]
问题
我试图了解每个步骤的尺寸应该是多少。隐藏维度应该初始化为 (4, 600, 256) 还是 (4, 64, 256)?