Abdullah-Nazhat commited on
Commit
f05936b
·
verified ·
1 Parent(s): 9047b70

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +15 -9
train.py CHANGED
@@ -85,24 +85,25 @@ def train(dataloader, model, loss_fn, optimizer):
85
  correct = 0
86
  for batch, (X,y) in enumerate(dataloader):
87
  X, y = X.to(device), y.to(device)
88
-
89
  pred = model(X)
90
  loss = loss_fn(pred, y)
91
 
92
  optimizer.zero_grad()
93
  loss.backward()
94
  optimizer.step()
 
95
  train_loss += loss.item()
96
  _, labels = torch.max(pred.data, 1)
97
- correct += labels.eq(y.data).type(torch.float).sum()
98
-
99
  if batch % 100 == 0:
100
  loss, current = loss.item(), batch * len(X)
101
  print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
102
 
103
  train_loss /= num_batches
104
- train_accuracy = 100. * correct.item() / size
105
- print(f"Train Metric: \n Accuracy: {(100 * correct):>0.1f}% \n")
 
106
  return train_loss, train_accuracy
107
 
108
  def test(dataloader, model, loss_fn):
@@ -114,13 +115,18 @@ def test(dataloader, model, loss_fn):
114
  with torch.no_grad():
115
  for X,y in dataloader:
116
  X,y = X.to(device), y.to(device)
 
117
  pred = model(X)
118
- test_loss += loss_fn(pred, y).item()
119
- correct += (pred.argmax(1) == y).type(torch.float).sum().item()
 
 
 
 
120
  test_loss /= num_batches
121
  correct /= size
122
- print(f"Test Metrics: \n Accuracy: {(100 * correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
123
- test_accuracy = 100 * correct
124
  return test_loss, test_accuracy
125
 
126
  logname = "/PATH/Normalizer/Experiments_cifar10/logs_normalizer/logs_cifar10.csv"
 
85
  correct = 0
86
  for batch, (X,y) in enumerate(dataloader):
87
  X, y = X.to(device), y.to(device)
88
+
89
  pred = model(X)
90
  loss = loss_fn(pred, y)
91
 
92
  optimizer.zero_grad()
93
  loss.backward()
94
  optimizer.step()
95
+
96
  train_loss += loss.item()
97
  _, labels = torch.max(pred.data, 1)
98
+ correct += labels.eq(y.data).type(torch.float).sum().item()
 
99
  if batch % 100 == 0:
100
  loss, current = loss.item(), batch * len(X)
101
  print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
102
 
103
  train_loss /= num_batches
104
+ correct /= size
105
+ train_accuracy = 100 * correct
106
+ print(f"Train Metrics: \n Accuracy: {train_accuracy:>0.1f}%, Avg loss: {train_loss:>8f} \n")
107
  return train_loss, train_accuracy
108
 
109
  def test(dataloader, model, loss_fn):
 
115
  with torch.no_grad():
116
  for X,y in dataloader:
117
  X,y = X.to(device), y.to(device)
118
+
119
  pred = model(X)
120
+ loss = loss_fn(pred, y)
121
+
122
+ test_loss += loss.item()
123
+ _, labels = torch.max(pred.data, 1)
124
+ correct += labels.eq(y.data).type(torch.float).sum().item()
125
+
126
  test_loss /= num_batches
127
  correct /= size
128
+ test_accuracy = 100 * correct
129
+ print(f"Test Metrics: \n Accuracy: {test_accuracy:>0.1f}%, Avg loss: {test_loss:>8f} \n")
130
  return test_loss, test_accuracy
131
 
132
  logname = "/PATH/Normalizer/Experiments_cifar10/logs_normalizer/logs_cifar10.csv"