Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions Tasks/daily tasks/Sourav_Menon/task_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#Tried to change learning rate into 3,2,1,.1,.01,.001 but gradual and regular decrease in log-loss was only seen with the values with .1,.01,.001

#No considerable change can be seen in log-loss when tried to change values of kernel-size and max-pooling

#small change made in input-layer of cnn led to the error


import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim

transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(
(0.5, 0.5, 0.5),
(0.5, 0.5, 0.5)
)
]
)

trainset = torchvision.datasets.CIFAR10(
root='./data',
train=True,
download=False,
transform=transform
)

testset = torchvision.datasets.CIFAR10(
root='./data',
train=False,
download=False,
transform=transform
)

trainloader = torch.utils.data.DataLoader(
trainset,
batch_size=4,
shuffle=True,
num_workers=2
)

testloader = torch.utils.data.DataLoader(
testset,
batch_size=4,
shuffle=False,
num_workers=2
)

classes = (
'plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck'
)

class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)

def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x


net = Net()

loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(
net.parameters(),
lr=0.001
)

for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# data = (inputs, labels)
inputs, labels = data
optimizer.zero_grad()

outputs = net(inputs)
loss = loss_function(outputs, labels)
loss.backward()
optimizer.step()

running_loss = running_loss + loss.item()
if i % 2000 == 1999:
print(
'[%d, %5d] loss: %.3f' %
(epoch + 1, i+1, running_loss/2000)
)
running_loss = 0.0
print("vola")