Python 运行时错误:输入必须有3个维度,得到2个维度
嗨,我有以下代码Python 运行时错误:输入必须有3个维度,得到2个维度,python,pytorch,recurrent-neural-network,Python,Pytorch,Recurrent Neural Network,嗨,我有以下代码 class GRU(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes ): super(GRU, self).__init__() #WE STORE THE LAYERS AND HIDDENSIZE self.num_layers = num_layers self.hidde
class GRU(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes ):
super(GRU, self).__init__()
#WE STORE THE LAYERS AND HIDDENSIZE
self.num_layers = num_layers
self.hidden_size = hidden_size
self.gru = nn.GRU(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size* sequence_length, num_classes)
def forward(self, x):
# Set initial hidden states (and cell states for LSTM)
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
# Forward propagate RNN
out, _ = self.gru(x, h0)
out=out.reshape(out.shape[0], -1)
out = self.fc(out)
return out
以及以下超参数
# Hyper-parameters
num_classes = 6
batch_size = 32
learning_rate = 0.0025
dropout=0.2
epochs = 10
input_size = len(X_train[0][0])
sequence_length = len(X_train[0])
hidden_size = 64
num_layers = 2
print (input_size)
print (sequence_length)
6
100
我的数据是三维的
X_train.shape
torch.Size([4078, 100, 6])
X_test.shape
torch.Size([1020, 100, 6])
然而,当我试图运行代码时,我收回了描述中的错误。我查看了PyTorch的官方网站和其他相同的问题,但找不到解决方案
如果有人能帮我写代码,我将不胜感激
这是完整的错误消息
RuntimeError Traceback (most recent call last)
<ipython-input-47-21beb733af1e> in <module>
1 test_accs, confusion_mtxes = [], []
2 for epoch in range(1, epochs + 1):
----> 3 train(X_train, y_train)
4 test_acc, confusion_mtx = test(X_test, y_test)
5 test_accs.append(test_acc)
<ipython-input-45-d10728615dab> in train(X_train, y_train)
4 data, target = data, target
5 optimizer.zero_grad()
----> 6 output = model(data)
7 loss = criterion(output, target)
8 loss.backward()
~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
--> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)
<ipython-input-42-afe206da09cb> in forward(self, x)
14 h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
15 # Forward propagate RNN
---> 16 out, _ = self.gru(x, h0)
17 out=out.reshape(out.shape[0], -1)
18 out = self.fc(out)
~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
--> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)
~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\rnn.py in forward(self, input, hx)
711 hx = self.permute_hidden(hx, sorted_indices)
712
--> 713 self.check_forward_args(input, hx, batch_sizes)
714 if batch_sizes is None:
715 result = _VF.gru(input, hx, self._flat_weights, self.bias, self.num_layers,
~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\rnn.py in check_forward_args(self, input, hidden, batch_sizes)
178 def check_forward_args(self, input, hidden, batch_sizes):
179 # type: (Tensor, Tensor, Optional[Tensor]) -> None
--> 180 self.check_input(input, batch_sizes)
181 expected_hidden_size = self.get_expected_hidden_size(input, batch_sizes)
182
~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\rnn.py in check_input(self, input, batch_sizes)
153 raise RuntimeError(
154 'input must have {} dimensions, got {}'.format(
--> 155 expected_input_dim, input.dim()))
156 if self.input_size != input.size(-1):
157 raise RuntimeError(
RuntimeError: input must have 3 dimensions, got 2
我尝试在这里使用相同的逻辑,这意味着我将序列长度与输入大小相乘,但仍然无法解决问题我认为一个问题是您在nn.GRU中错误地设置了batch\u first=True。输入数据应为batch_first=False。在这里,您的培训和测试数据集(X_train和X_test)的形状已经是(seq_len,batch_size,features),您不需要为nn.GRU设置batch_first=True。它将给出形状输出(顺序、批次大小、隐藏大小)。仅当数据的形状为(批次大小、顺序、特征)时,才使用batch_first=True。希望有帮助。@HVD不,它不起作用。我还发布了完整的错误消息。谢谢你的尝试
model = Sequential()
model.add(GRU(units=64, return_sequences=True,recurrent_regularizer=l2(0.0015), input_shape=(timesteps, input_dim), activation="tanh"))
model.add(GRU(units=64, recurrent_regularizer=l2(0.0015), input_shape=(timesteps, input_dim), activation="tanh"))
model.add(Dropout(0.5))
model.add(Dense(64, activation='tanh'))
model.add(Dense(64, activation='tanh'))
model.add(Dense(n_classes, activation='softmax'))
model.summary()
model.compile(optimizer=Adam(learning_rate = 0.0025), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
history =model.fit(X_train, y_train, batch_size=32, epochs=100)