-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (33 loc) · 1.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import ANN
import torch
from torch import nn
from torchvision import datasets
from torchvision import transforms
from torch import optim
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,)),])
trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
#Defining the neural network
Brain = ANN.neural_network()
#Define the loss
criterion = nn.CrossEntropyLoss()
#Defining the optimizer
optimizer = optim.Adam(Brain.parameters(), lr=0.002)
#Defining the epochs
epochs = 20
#Training the model
for data in range(1,epochs+1):
total_loss = 0
for images,lables in trainloader:
#Flating the image
images = images.view(images.shape[0],-1)
#Training pass
optimizer.zero_grad()
output = Brain.Forward(images)
loss = criterion(output, lables)
loss.backward()
optimizer.step()
total_loss = total_loss + loss.item()
else:
value = total_loss/len(trainloader)
print("EPOCHS NO : {EPOCHS} Training loss : {Training_loss}".format(EPOCHS=data,Training_loss=value))