Hi, I am new to pytorch and deep learning.
The loss of my model was almost static throughout the training p
Model class
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3,32,3)
self.conv2 = nn.Conv2d(32,64,3)
self.conv3 = nn.Conv2d(64,128,3)
self.fc1 = nn.Linear(128*2*2,100)
self.fc2 = nn.Linear(100,10)
def forward(self,x):
x = self.conv1(x)
x = F.relu(x)
x = F.max_pool2d(x,(2,2))#32x15x15
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x,(2,2))#64x6x6
x = self.conv3(x)
x = F.relu(x)
x = F.max_pool2d(x,(2,2))#128x2x2
x = x.view(-1,128*2*2)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.softmax(x,dim=1)
return x
I was training the model on the cifar10 dataset.
train.py
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
from cifarloader import cifar10
from model import Model
t = transforms.Compose(
[
lambda x:torch.Tensor(x),
lambda x:x/255,
transforms.Normalize((0.475,0.475,0.475),(0.253,0.253,0.253))
]
)
cifar10 = cifar10(transforms=t)
dataloader = DataLoader(cifar10,batch_size=20,shuffle=True)
net = Model()
loss_fn = nn.MSELoss()
optim = optim.Adam(net.parameters(),lr=0.003)
epochs = 100
for e in range(epochs):
tloss = 0
for b in dataloader:
x,y = b
optim.zero_grad()
p = net(x)
loss = loss_fn(p,y)
tloss+=loss*20
loss.backward()
optim.step()
if e%10 == 0:
print(f'Epoch: {e}, Loss{tloss/20}')
torch.save(net,f'e-{e}-l-{tloss}.pth')
Was there something i did wrong, It would be cool if someone could help me out