Having problem with conv layers

hindy: <p>import torch</p>
<p>from torchvision import d</p>
<p>from torch import nn</p>
<p>from torch.utils.data import dataset, dataloader</p>
<p>from torch import optim</p>
<p>train_loader = torch.utils.data.DataLoader(</p>
<p>torchvision.datasets.MNIST(’/files/’, train=True, download=True,</p>
<pre><code>                         transform=torchvision.transforms.Compose([

                           torchvision.transforms.ToTensor(),

                           torchvision.transforms.Normalize(

                             (0.1307,), (0.3081,))

                         ])),
</code></pre>
<p>batch_size=64, shuffle=True)</p>
<p>val_loader = torch.utils.data.DataLoader(</p>
<p>torchvision.datasets.MNIST(’/files/’, train=False, download=True,</p>
<pre><code>                         transform=torchvision.transforms.Compose([

                           torchvision.transforms.ToTensor(),

                           torchvision.transforms.Normalize(

                             (0.1307,), (0.3081,))

                         ])),
</code></pre>
<p>batch_size=1000, shuffle=True)</p>
<h1>
<a name="model-1" class="anchor" href="#model-1"></a>model</h1>
<p>class CNN(nn.Module):</p>
<p>def <strong>init</strong>(self):</p>
<pre><code>super(CNN, self).__init__()

self.conv = nn.Sequential(

    nn.Conv2d(1, 10, kernel_size=5), # non-RGB image hence input_channel = 1, out_channels is the number of feature maps,

    nn.ReLU(),

    nn.MaxPool2d(2),

    nn.Conv2d(10, 20, kernel_size=5), 

    nn.ReLU(),

    nn.MaxPool2d(2)

    )



self.flatten = nn.Flatten()

self.dense = nn.Sequential(

    nn.Linear(320, 50),

    nn.BatchNorm2d(50), 

    nn.Linear(50, 10)

    )
</code></pre>
<p>def forward(self, x):</p>
<pre><code>x = self.conv(x)

x = self.flatten(x)

x = self.dense(x)

return x
</code></pre>
<p>model = CNN()</p>
<p>loss_fn = nn.CrossEntropyLoss()</p>
<p>optimizer = optim.Adam(model.parameters())</p>
<p>epochs = 20</p>
<p>train_loss = []</p>
<p>val_loss = []</p>
<p>for t in range(epochs):</p>
<p>for feature, label in train_loader:</p>
<pre><code># forward

pred = model(feature)

# compute the objective function

output = loss_fn(pred, label)

# clean the gradients

model.zero_grad()

# backward propogation

output.backward()

# update

optimizer.step()

train_loss.append(output.item())
</code></pre>
<p>print(f’Epoch {t + 1}, train loss: {output}’)</p>
<p>for feature2, label2 in val_loader:</p>
<pre><code>with torch.no_grad():

# forward

  pred2 = model(feature2)

# compute the objective function

output2 = loss_fn(pred2, label2)

val_loss.append(output2.item())

print(f'Epoch {t + 1}, train loss: {output}')
</code></pre>
<p>ValueError: expected 4D input (got 2D input)</p>

hindy: <p>Have been trying the whole, I seriously don’t know what’s wrong.</p>

eqy: <p>If you are passing in a single HxW grayscale image, you likely need to change it to NxCxHxW format (even if the batch size and the number of channels is 1). /<br>
e.g., something like</p>
<pre><code class="lang-auto">feature = feature.unsqueeze(0).unsqueeze(0)
</code></pre>

hindy: <p>now the error becomes<br>
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [10, 1, 5, 5], but got 6-<br>
dimensional input of size [1, 1, 64, 1, 28, 28] instead</p>

eqy: <p>OK, it seems that the shape mismatch is somewhere else in the model. You might want to find the line where the original error occurs and print the shapes of the input to see the cause of the issue.</p>