-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
94 lines (63 loc) · 2.66 KB
/
train.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import time
print("Loading libraries...")
start_time = time.time()
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
from model import *
import numpy as np
import matplotlib.pyplot as plt
print(f"Libraries loaded in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Loading data...")
training_data = datasets.EMNIST(root="data", train=True, download=True, transform=ToTensor(), split="bymerge")
test_data = datasets.EMNIST(root="data", train=False, download=True, transform=ToTensor(), split="bymerge")
print(f"Data loaded in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Setting configuration...")
start_time = time.time()
device = ("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
print(f"Using {device} device")
torch.set_default_device('cuda:0')
torch.set_default_dtype(torch.float64)
torch.set_default_tensor_type(torch.cuda.FloatTensor)
print(f"Configuration set in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Loading model...")
start_time = time.time()
model = Model().cuda()
print(f"Model loaded in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Preparing data...")
start_time = time.time()
# Create data loaders.
batch_size = 128
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)
'''for i in range(len(training_data)):
x = training_data[i]
y = test_data[i]
print(f"Input: {x} | Output: {y}")'''
print(f"Data prepared in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Training model...")
start_time = time.time()
train_losses = []
test_losses = []
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
epochs = 16
for t in range(epochs):
print(f"Epoch {t + 1}\n-------------------------------")
train_loss = train(train_dataloader, model, loss_fn, optimizer, device)
test_loss = test(test_dataloader, model, loss_fn, device)
train_losses.append(train_loss)
test_losses.append(test_loss)
print(f"Model trained in {round((time.time() - start_time) * 1000, 3)} ms.")
print("Saving model...")
start_time = time.time()
torch.save(model.state_dict(), "model.pth")
print(f"Model saved in {round((time.time() - start_time) * 1000, 3)} ms.")
'''print("Visualizing loss...")
plt.plot(train_losses, label="Training loss", c="#EE6660")
plt.plot(test_losses, label="Test loss", c="#90DDF0")
plt.legend()
plt.show()
print(f"Loss visualized.")'''