chrisjay commited on
Commit
37e69f6
·
1 Parent(s): f91a2b3

trained pth files 2

Browse files
Files changed (3) hide show
  1. app.py +25 -22
  2. weights/mnist_model.pth +0 -0
  3. weights/optimizer.pth +0 -0
app.py CHANGED
@@ -8,7 +8,7 @@ import torch.optim as optim
8
 
9
  # This is just to show an interface where one draws a number and gets prediction.
10
 
11
- n_epochs = 100
12
  batch_size_train = 128
13
  batch_size_test = 1000
14
  learning_rate = 0.01
@@ -67,16 +67,16 @@ class MNIST_Model(nn.Module):
67
  return F.log_softmax(x)
68
 
69
  train_loader = torch.utils.data.DataLoader(
70
- torchvision.datasets.MNIST('.files/', train=True, download=True,
71
  transform=torchvision.transforms.Compose([
72
  torchvision.transforms.ToTensor(),
73
  torchvision.transforms.Normalize(
74
- (0.1307,), (0.3081,))
75
  ])),
76
  batch_size=batch_size_train, shuffle=True)
77
 
78
  test_loader = torch.utils.data.DataLoader(
79
- torchvision.datasets.MNIST('.files/', train=False, download=True,
80
  transform=torchvision.transforms.Compose([
81
  torchvision.transforms.ToTensor(),
82
  torchvision.transforms.Normalize(
@@ -84,25 +84,24 @@ test_loader = torch.utils.data.DataLoader(
84
  ])),
85
  batch_size=batch_size_test, shuffle=True)
86
 
87
- def train(epochs,network,optimizer,train_loader):
88
 
89
  train_losses=[]
90
  network.train()
91
- for epoch in range(epochs):
92
- for batch_idx, (data, target) in enumerate(train_loader):
93
- optimizer.zero_grad()
94
- output = network(data)
95
- loss = F.nll_loss(output, target)
96
- loss.backward()
97
- optimizer.step()
98
- if batch_idx % log_interval == 0:
99
- print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
100
- epoch, batch_idx * len(data), len(train_loader.dataset),
101
- 100. * batch_idx / len(train_loader), loss.item()))
102
- train_losses.append(loss.item())
103
-
104
- torch.save(network.state_dict(), MODEL_WEIGHTS_PATH)
105
- torch.save(optimizer.state_dict(), OPTIMIZER_PATH)
106
 
107
  def test():
108
  test_losses=[]
@@ -121,6 +120,7 @@ def test():
121
  acc = acc.item()
122
  test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
123
  test_loss,acc)
 
124
  return test_metric,acc
125
 
126
 
@@ -143,8 +143,11 @@ if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
143
  optimizer_state_dict = torch.load(optimizer_state_dict)
144
  optimizer.load_state_dict(optimizer_state_dict)
145
  # Train
146
- train(n_epochs,network,optimizer,train_loader)
147
- test()
 
 
 
148
 
149
 
150
  def image_classifier(inp):
 
8
 
9
  # This is just to show an interface where one draws a number and gets prediction.
10
 
11
+ n_epochs = 10
12
  batch_size_train = 128
13
  batch_size_test = 1000
14
  learning_rate = 0.01
 
67
  return F.log_softmax(x)
68
 
69
  train_loader = torch.utils.data.DataLoader(
70
+ torchvision.datasets.MNIST('files/', train=True, download=True,
71
  transform=torchvision.transforms.Compose([
72
  torchvision.transforms.ToTensor(),
73
  torchvision.transforms.Normalize(
74
+ mean=(0.1307,), std=(0.3081,))
75
  ])),
76
  batch_size=batch_size_train, shuffle=True)
77
 
78
  test_loader = torch.utils.data.DataLoader(
79
+ torchvision.datasets.MNIST('files/', train=False, download=True,
80
  transform=torchvision.transforms.Compose([
81
  torchvision.transforms.ToTensor(),
82
  torchvision.transforms.Normalize(
 
84
  ])),
85
  batch_size=batch_size_test, shuffle=True)
86
 
87
+ def train(epoch,network,optimizer,train_loader):
88
 
89
  train_losses=[]
90
  network.train()
91
+ for batch_idx, (data, target) in enumerate(train_loader):
92
+ optimizer.zero_grad()
93
+ output = network(data)
94
+ loss = F.nll_loss(output, target)
95
+ loss.backward()
96
+ optimizer.step()
97
+ if batch_idx % log_interval == 0:
98
+ print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
99
+ epoch, batch_idx * len(data), len(train_loader.dataset),
100
+ 100. * batch_idx / len(train_loader), loss.item()))
101
+ train_losses.append(loss.item())
102
+
103
+ torch.save(network.state_dict(), MODEL_WEIGHTS_PATH)
104
+ torch.save(optimizer.state_dict(), OPTIMIZER_PATH)
 
105
 
106
  def test():
107
  test_losses=[]
 
120
  acc = acc.item()
121
  test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
122
  test_loss,acc)
123
+ print(test_metric)
124
  return test_metric,acc
125
 
126
 
 
143
  optimizer_state_dict = torch.load(optimizer_state_dict)
144
  optimizer.load_state_dict(optimizer_state_dict)
145
  # Train
146
+
147
+ for epoch in range(n_epochs):
148
+
149
+ train(epoch,network,optimizer,train_loader)
150
+ test()
151
 
152
 
153
  def image_classifier(inp):
weights/mnist_model.pth ADDED
Binary file (89.9 kB). View file
 
weights/optimizer.pth ADDED
Binary file (89.8 kB). View file