LSTM Error For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors

username: NancyShehata
cooked: <p>I need a help to solve the mentioned error please? here is my code and the exact script of the error I got. the data shape is (7, 2, 141)</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p>import os<br>
import numpy as np<br>
from pathlib import Path<br>
import json<br>
import matplotlib.pyplot as plt<br>
import pandas as pd<br>
from sklearn.preprocessing import MinMaxScaler<br>
from sklearn.metrics import mean_squared_error<br>
from torch.autograd import Variable<br>
import torch<br>
import torch.nn as nn<br>
import librosa<br>
import librosa.display<br>
from tqdm import tqdm<br>
features = r"C:\Users\data"<br>
def normalize_features(features):</p>
<pre><code>inputs_m, inputs_s = features.mean(), features.std()
features = (features - inputs_m) / inputs_s
return features
</code></pre>
<p>features_test=normalize_features(features_test)<br>
def sliding_windows(data, seq_length):<br>
x = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
y = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>for i in range(len(data)-seq_length-1):
    _x = data[i:(i+seq_length)]
    _y = data[i+seq_length]
    x.append(_x)
    y.append(_y)

return np.array(x),np.array(y)
</code></pre>
<p>seq_length = 5<br>
x, y = sliding_windows(features_test, seq_length)<br>
train_size = int(len(y) * 0.80)<br>
test_size = len(y) - train_size<br>
trainX = (torch.Tensor(np.array(x[0:train_size])))<br>
trainY = (torch.Tensor(np.array(y[0:train_size])))</p>
<p>testX = (torch.Tensor(np.array(x[train_size:len(x)])))<br>
testY = (torch.Tensor(np.array(y[train_size:len(y)])))<br>
class LSTM(nn.Module):</p>
<pre><code>def __init__(self, num_classes, input_size, hidden_size, num_layers):
    super(LSTM, self).__init__()
    
    self.num_classes = num_classes
    self.num_layers = num_layers
    self.input_size = input_size
    self.hidden_size = hidden_size
    self.seq_length = seq_length
    
    self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
                        num_layers=num_layers, batch_first=True)
    
    self.fc = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    h_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size))
    
    c_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size))
    
    # Propagate input through LSTM
    ula, (h_out, _) = self.lstm(x, (h_0, c_0))
    
    h_out = h_out.view(-1, self.hidden_size)
    
    out = self.fc(h_out)
    
    return out
</code></pre>
<p>num_epochs = 2000<br>
learning_rate = 0.01</p>
<p>input_size = 7<br>
hidden_size = 13<br>
num_layers = 1</p>
<p>num_classes = 1</p>
<p>lstm = LSTM(num_classes, input_size, hidden_size, num_layers)</p>
<p>criterion = torch.nn.MSELoss()    # mean-squared error for regression<br>
optimizer = torch.optim.Adam(lstm.parameters(), lr=learning_rate)<br>
<span class="hashtag-raw">#optimizer</span> = torch.optim.SGD(lstm.parameters(), lr=learning_rate)</p>
<h1><a name="train-the-model-1" class="anchor" href="#train-the-model-1"></a>Train the model</h1>
<p>for epoch in range(num_epochs):<br>
outputs = lstm(trainX)<br>
optimizer.zero_grad()</p>
<pre><code># obtain the loss function
loss = criterion(outputs, trainY)

loss.backward()

optimizer.step()
if epoch % 100 == 0:
    print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
</code></pre>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p><strong>the error</strong></p>
<p>RuntimeError                              Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_19112/2621872190.py in <br>
67 # Train the model<br>
68 for epoch in range(num_epochs):<br>
—&gt; 69     outputs = lstm(trainX)<br>
70     optimizer.zero_grad()<br>
71</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1109                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1110             return forward_call(*input, **kwargs)<br>
1111         # Do not call functions when jit is used<br>
1112         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>~\AppData\Local\Temp/ipykernel_19112/2621872190.py in forward(self, x)<br>
43<br>
44         # Propagate input through LSTM<br>
—&gt; 45         ula, (h_out, _) = self.lstm(x, (h_0, c_0))<br>
46<br>
47         h_out = h_out.view(-1, self.hidden_size)</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1109                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1110             return forward_call(*input, **kwargs)<br>
1111         # Do not call functions when jit is used<br>
1112         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\rnn.py in forward(self, input, hx)<br>
750                         msg = (“For unbatched 2-D input, hx and cx should "<br>
751                                f"also be 2-D but got ({hx[0].dim()}-D, {hx[1].dim()}-D) tensors”)<br>
 → 752                         raise RuntimeError(msg)<br>
753                     hx = (hx[0].unsqueeze(1), hx[1].unsqueeze(1))<br>
754</p>
<p>RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors</p>
<p>​</p>