解决RNN分类器报错RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors
在使用RNN分类器模型时,经常会遇到以下错误:'RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors'。这个错误通常发生在使用LSTM层时,是因为输入数据维度与隐藏状态维度不匹配。
该问题可以通过调整zero_state方法中的hidden和cell的维度来解决。在zero_state方法中,需要将hidden和cell的维度从2-D改为3-D,以匹配LSTM层的输入要求。可以将以下两行代码的注释取消掉:
hidden = torch.zeros(self.num_layers, batch_size, self.hidden_dim)
cell = torch.zeros(self.num_layers, batch_size, self.hidden_dim)
通过调整这些维度,可以确保输入数据和隐藏状态维度一致,从而解决'RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors'错误。
原文地址: https://www.cveoy.top/t/topic/o9vi 著作权归作者所有。请勿转载和采集!