Error Running lstm

radhika: <p>I get this error when i run mycode</p>
<p>C:\Users\Charvee\AppData\Local\Programs\Python\Python37\python.exe E:/Jigna/Image-Forgery-Detection-CNN-master/Image-Forgery-Detection-CNN-master/src/cnn_lstm/lstm.py<br>
Traceback (most recent call last):<br>
File “E:/Jigna/Image-Forgery-Detection-CNN-master/Image-Forgery-Detection-CNN-master/src/cnn_lstm/lstm.py”, line 164, in <br>
outputs = model(images)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 547, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “E:/Jigna/Image-Forgery-Detection-CNN-master/Image-Forgery-Detection-CNN-master/src/cnn_lstm/lstm.py”, line 94, in forward<br>
out, (hn, cn) = self.lstm(x, (h0, c0))<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py”, line 547, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\rnn.py”, line 564, in forward<br>
return self.forward_tensor(input, hx)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\rnn.py”, line 543, in forward_tensor<br>
output, hidden = self.forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\rnn.py”, line 523, in forward_impl<br>
self.check_forward_args(input, hx, batch_sizes)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\rnn.py”, line 496, in check_forward_args<br>
self.check_input(input, batch_sizes)<br>
File “C:\Users\Charvee\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\rnn.py”, line 145, in check_input<br>
expected_input_dim, input.dim()))<br>
RuntimeError: input must have 3 dimensions, got 4</p>
<p>My Code is  as below</p>
<p>``import torch<br>
import torch.nn as nn<br>
import torchvision.transforms as transforms<br>
import torchvision.datasets as dsets<br>
from torch.autograd import Variable<br>
import torch<br>
import pandas as pd<br>
import torchvision.transforms as transforms<br>
from torchvision import datasets</p>
<p>‘’’<br>
STEP 1: LOADING DATASET<br>
‘’’</p>
<h1>train_dataset = dsets.MNIST(root=’./data’,</h1>
<h1>train=True,</h1>
<h1>transform=transforms.ToTensor(),</h1>
<h1>download=False)</h1>
<h1></h1>
<h1>test_dataset = dsets.MNIST(root=’./data’,</h1>
<h1>train=False,</h1>
<h1>transform=transforms.ToTensor())</h1>
<p>DATA_DIR = “…/generated_patches_ComoFo/” #“my_patches”  # put the directory of the patches<br>
transform = transforms.Compose([transforms.ToTensor()])</p>
<p>data = datasets.ImageFolder(root=DATA_DIR, transform=transform)  # Fetch data</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>train_loader = torch.utils.data.DataLoader(data, batch_size=64, shuffle=True, pin_memory=True)</p>
<p>test_loader = torch.utils.data.DataLoader(data, batch_size=64, shuffle=True, pin_memory=True)</p>
<p>‘’’<br>
STEP 2: MAKING DATASET ITERABLE<br>
‘’’</p>
<p>batch_size =100 <span class="hashtag">#64</span> <span class="hashtag">#100</span><br>
n_iters = 3000 <span class="hashtag">#3000</span><br>
num_epochs = n_iters / (len(data) / batch_size)<br>
num_epochs = int(num_epochs)</p>
<p>train_loader = torch.utils.data.DataLoader(dataset=data,<br>
batch_size=batch_size,<br>
shuffle=True)</p>
<p>test_loader = torch.utils.data.DataLoader(dataset=data,<br>
batch_size=batch_size,<br>
shuffle=False)</p>
<p>‘’’<br>
STEP 3: CREATE MODEL CLASS<br>
‘’’</p>
<p>class LSTMModel(nn.Module):<br>
def <strong>init</strong>(self, input_dim, hidden_dim, layer_dim, output_dim):<br>
super(LSTMModel, self).<strong>init</strong>()<br>
# Hidden dimensions<br>
self.hidden_dim = hidden_dim</p>
<pre><code>    # Number of hidden layers
    self.layer_dim = layer_dim

    # Building your LSTM
    # batch_first=True causes input/output tensors to be of shape
    # (batch_dim, seq_dim, feature_dim)
    self.lstm = nn.LSTM(input_dim, hidden_dim, layer_dim, batch_first=True)

    # Readout layer
    self.fc = nn.Linear(hidden_dim, output_dim)

def forward(self, x):
    # Initialize hidden state with zeros
    #######################
    #  USE GPU FOR MODEL  #
    #######################

    if torch.cuda.is_available():
        h0 = Variable(torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).cuda())
    else:
        h0 = Variable(torch.zeros(self.layer_dim, x.size(0), self.hidden_dim))

    # Initialize cell state
    if torch.cuda.is_available():
        c0 = Variable(torch.zeros(self.layer_dim, x.size(0), self.hidden_dim).cuda())
    else:
        c0 = Variable(torch.zeros(self.layer_dim, x.size(0), self.hidden_dim))

    # One time step
    out, (hn, cn) = self.lstm(x, (h0, c0))

    # Index hidden state of last time step
    # out.size() --&gt; 100, 28, 100
    # out[:, -1, :] --&gt; 100, 100 --&gt; just want last time step hidden states! 
    out = self.fc(out[:, -1, :])
    # out.size() --&gt; 100, 10
    return out
</code></pre>
<p>‘’’<br>
STEP 4: INSTANTIATE MODEL CLASS<br>
‘’’<br>
input_dim =28 <span class="hashtag">#16</span> <span class="hashtag">#4</span> <span class="hashtag">#28</span><br>
hidden_dim = 100<br>
layer_dim = 3  # ONLY CHANGE IS HERE FROM ONE LAYER TO TWO LAYER<br>
output_dim = 10</p>
<p>model = LSTMModel(input_dim, hidden_dim, layer_dim, output_dim)</p>
<p>#######################</p>
<h1>USE GPU FOR MODEL</h1>
<p>#######################</p>
<p>if torch.cuda.is_available():<br>
model.cuda()</p>
<p>‘’’<br>
STEP 5: INSTANTIATE LOSS CLASS<br>
‘’’<br>
criterion = nn.CrossEntropyLoss()</p>
<p>‘’’<br>
STEP 6: INSTANTIATE OPTIMIZER CLASS<br>
‘’’<br>
learning_rate = 0.1</p>
<p>optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)</p>
<p>‘’’<br>
STEP 7: TRAIN THE MODEL<br>
‘’’</p>
<h1>Number of steps to unroll</h1>
<p>seq_dim = 28</p>
<p>iter = 0<br>
for epoch in range(num_epochs):<br>
for i, (images, labels) in enumerate(train_loader):<br>
# Load images as Variable<br>
#######################<br>
#  USE GPU FOR MODEL  #<br>
#######################<br>
if torch.cuda.is_available():<br>
images = Variable(images.view(-1, seq_dim, input_dim).cuda())<br>
labels = Variable(labels.cuda())<br>
else:<br>
# images = Variable(images.view(-1, 196608,16))<br>
<span class="hashtag">#labels</span> = Variable(labels)</p>
<pre><code>        images = Variable(images)
        labels = Variable(labels)

    # Clear gradients w.r.t. parameters
    optimizer.zero_grad()


    #for loop and changed sampled_ids = torch.cat(sampled_ids, 1) to sampled_ids = torch.cat(sampled_ids, 0)
    # Forward pass to get output/logits
    # outputs.size() --&gt; 100, 10
    outputs = model(images)





    print(0)
    # Calculate Loss: softmax --&gt; cross entropy loss
    loss = criterion(outputs, labels)
    print("1")
    # Getting gradients w.r.t. parameters
    loss.backward()
    print("2")
    # Updating parameters
    optimizer.step()
    print("3")
    iter += 1
    print("4")
    if iter % 500 == 0:
        # Calculate Accuracy         
        correct = 0
        total = 0
        print("6")
        # Iterate through test dataset
        for images, labels in test_loader:
            #######################
            #  USE GPU FOR MODEL  #
            #######################
            if torch.cuda.is_available():
                images = Variable(images.view(-1, seq_dim, input_dim).cuda())
            else:
                images = Variable(images.view(-1, seq_dim, input_dim))

            # Forward pass only to get logits/output
            outputs = model(images)

            # Get predictions from the maximum value
            _, predicted = torch.max(outputs.data, 1)

            # Total number of labels
            total += labels.size(0)

            # Total correct predictions
            #######################
            #  USE GPU FOR MODEL  #
            #######################
            if torch.cuda.is_available():
                correct += (predicted.cpu() == labels.cpu()).sum()
            else:
                correct += (predicted == labels).sum()

        accuracy = 100 * correct / total

        # Print Loss
        print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter, loss.data[0], accuracy))``</code></pre>