-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
51 lines (37 loc) · 1.18 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
import torch
from torch import nn
import torch.optim as optim
import matplotlib.pyplot as plt
from model import Network
from data import data_loader
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Network().to(device)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
num_epochs = 150
train_loss = []
train_epoch = []
for epoch in range(num_epochs):
running_loss = 0.0
for i, batch in enumerate(data_loader):
inputs, labels = batch
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
train_loss.append(running_loss / len(data_loader))
train_epoch.append(epoch + i / len(data_loader))
print('[%d] loss: %f' % (epoch + 1, running_loss / len(data_loader)))
# Plot the training loss
plt.plot(train_epoch, train_loss)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss')
plt.show()
print('Finished Training')
model_path = "model.pt"
torch.save(model.state_dict(), model_path)