-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtrain.py
134 lines (125 loc) · 4.84 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import time
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from dataset.dataset import *
from utility.utils import *
from model import *
def train(net: VIN, trainloader, config, criterion, optimizer):
print_header()
# Automatically select device to make the code device agnostic
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
for epoch in range(config.epochs): # Loop over dataset multiple times
avg_error, avg_loss, num_batches = 0.0, 0.0, 0.0
start_time = time.time()
for i, data in enumerate(trainloader): # Loop over batches of data
# Get input batch
X, S1, S2, labels = [d.to(device) for d in data]
if X.size()[0] != config.batch_size:
continue # Drop those data, if not enough for a batch
net = net.to(device)
# Zero the parameter gradients
optimizer.zero_grad()
# Forward pass
outputs, predictions = net(X, S1, S2, config.k)
# Loss
loss = criterion(outputs, labels)
# Backward pass
loss.backward()
# Update params
optimizer.step()
# Calculate Loss and Error
loss_batch, error_batch = get_stats(loss, predictions, labels)
avg_loss += loss_batch
avg_error += error_batch
num_batches += 1
time_duration = time.time() - start_time
# Print epoch logs
print_stats(epoch, avg_loss, avg_error, num_batches, time_duration)
print('\nFinished training. \n')
def test(net: VIN, testloader, config):
total, correct = 0.0, 0.0
# Automatically select device, device agnostic
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
for i, data in enumerate(testloader):
# Get inputs
X, S1, S2, labels = [d.to(device) for d in data]
if X.size()[0] != config.batch_size:
continue # Drop those data, if not enough for a batch
net = net.to(device)
# Forward pass
outputs, predictions = net(X, S1, S2, config.k)
# Select actions with max scores(logits)
_, predicted = torch.max(outputs, dim=1, keepdim=True)
# Unwrap autograd.Variable to Tensor
predicted = predicted.data
# Compute test accuracy
correct += (torch.eq(torch.squeeze(predicted), labels)).sum()
total += labels.size()[0]
print('Test Accuracy: {:.2f}%'.format(100 * (correct / total)))
if __name__ == '__main__':
# Parsing training parameters
parser = argparse.ArgumentParser()
parser.add_argument(
'--datafile',
type=str,
default='dataset/gridworld_8x8.npz',
help='Path to data file')
parser.add_argument('--imsize', type=int, default=8, help='Size of image')
parser.add_argument(
'--lr',
type=float,
default=0.005,
help='Learning rate, [0.01, 0.005, 0.002, 0.001]')
parser.add_argument(
'--epochs', type=int, default=30, help='Number of epochs to train')
parser.add_argument(
'--k', type=int, default=10, help='Number of Value Iterations')
parser.add_argument(
'--l_i', type=int, default=2, help='Number of channels in input layer')
parser.add_argument(
'--l_h',
type=int,
default=150,
help='Number of channels in first hidden layer')
parser.add_argument(
'--l_q',
type=int,
default=10,
help='Number of channels in q layer (~actions) in VI-module')
parser.add_argument(
'--batch_size', type=int, default=128, help='Batch size')
config = parser.parse_args()
# Get path to save trained model
save_path = "trained/vin_{0}x{0}.pth".format(config.imsize)
# Instantiate a VIN model
net = VIN(config)
# Loss
criterion = nn.CrossEntropyLoss()
# Optimizer
optimizer = optim.RMSprop(net.parameters(), lr=config.lr, eps=1e-6)
# Dataset transformer: torchvision.transforms
transform = None
# Define Dataset
trainset = GridworldData(
config.datafile, imsize=config.imsize, train=True, transform=transform)
testset = GridworldData(
config.datafile,
imsize=config.imsize,
train=False,
transform=transform)
# Create Dataloader
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=config.batch_size, shuffle=True, num_workers=0)
testloader = torch.utils.data.DataLoader(
testset, batch_size=config.batch_size, shuffle=False, num_workers=0)
# Train the model
train(net, trainloader, config, criterion, optimizer)
# Test accuracy
test(net, testloader, config)
# Save the trained model parameters
torch.save(net.state_dict(), save_path)