Hello everyone, I found many topics about this error but I couldn’t find a solution, I kindly ask for help me.
CODE:
import torch
import torch.nn as nn
import torchvision.transforms as transforms
# MNIST
train_data = torchvision.datasets.MNIST(
root="./data", train=True, download=True, transform=transforms.ToTensor()
)
# NN
class MyNet(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.pool2 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(20 * 32 * 32, 500)
self.fc2 = nn.Linear(500, 10)
def forward(self, x):
x = self.conv1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
x = x.view(-1, 20 * 32 * 32)
x = self.fc1(x)
x = self.fc2(x)
return x
net = MyNet()
# data train
train_loader = torch.utils.data.DataLoader(
train_data, batch_size=32, shuffle=True
)
optimizer = torch.optim.SGD(net.parameters(), lr=0.001)
for epoch in range(10):
for i, (images, labels) in enumerate(train_loader):
outputs = net(images)
loss = torch.nn.functional.cross_entropy(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % 100 == 0:
print(f"Loss: {loss.item()}")
ERROR:
`
RuntimeError Traceback (most recent call last)
in <cell line: 40>()
40 for epoch in range(10):
41 for i, (images, labels) in enumerate(train_loader):
—> 42 outputs = net(images)
43
44 loss = torch.nn.functional.cross_entropy(outputs, labels)
2 frames
in forward(self, x)
24 x = self.conv2(x)
25 x = self.pool2(x)
—> 26 x = x.view(-1, 20 * 32 * 32)
27 x = self.fc1(x)
28 x = self.fc2(x)
RuntimeError: shape ‘[-1, 20480]’ is invalid for input of size 10240
`
I’m trying to resize the shapes but I can’t do it right because solving one brings up another.
I will appreciate an explanation of my error.
Thanks in advance.