1. RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
解决:loss.backward(retain_graph=True)
2. RuntimeError: expected scalar type Long but found Int
解决:.long()
3. ValueError: Expected input batch_size (370) to match target batch_size (5).
说明:在使用LSTM提取句子特征时,LSTM的输入(len(sentence), batch_size, embedding_dim), self.hidden),输入是(output, (h_n, c_n)),output是每个隐含层的输出集合,(len(sentence), batch_size, num_directions * hidden_size),如果只想要最后一个隐含层的输出,那么就是h_n,(num_layers * num_directions, batch_size, hidden_size),这里的num_directions是方向数,如双向LSTM就是2,num_layers是隐含层数。
想要最后一个隐含层输出作为句子的特征表示,并且后面接一个线性函数Linear(hidden_size, 2),做二分类用,还要调用CrossEntropyLoss()计算loss,因此,希望LSTM输出的特征size是(batch_size, hidden_size)。
而在一层单向LSTM中,输出的h_n大小是(1, batch_size, hidden_size),因此:
解决:h_n.sequeeze(),去掉这一维
4. 在准备输入到LSTM中的句子张量里新增加一个词向量大小的数据,假设原张量size是(5, 73, 32),分别表示batch_size = 5,len(sentence) = 73,embedding_dim = 32,那么首先需要保证新加入的词向量size是(5,1,32),如果不是,可以通过torch.unsqueeze()和torch.squeeze()在相应的维度进行变化,然后使用torch.cat((new, old), dim = 1),就可以添加进去了,更新后的张量size是(5, 74, 32),顺利拼接。
参考:
https://zhuanlan.zhihu.com/p/79064602
网友评论