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