-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
195 lines (153 loc) · 7.77 KB
/
training.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import torch
from random import randint
from neural_process import NeuralProcessImg
from torch import nn
from torch.distributions.kl import kl_divergence
from utils import *
from agents import *
class MovieNPTrainer():
def __init__(self, device, neural_process, optimizer, init_context_num=5, max_opt_iteration=20, max_target_num=50, print_freq=100):
self.device = device
self.neural_process = neural_process
self.optimizer = optimizer
self.print_freq = print_freq
self.init_context_num = init_context_num
self.max_opt_iteration = max_opt_iteration
self.max_target_num = max_target_num
self.steps = 0
self.epoch_loss_history = []
self.agents = EpsilonGreedyAgents()
def train(self, data_loader, epochs):
for epoch in range(epochs):
epoch_loss = 0.
for i, data in enumerate(data_loader):
x, y = data
x, y, context_loc = sample_targets_and_save_contexts(x, y)
if len(context_loc) == 0:
print('no safe initialization possible')
continue
for step in range(min(self.max_opt_iteration, y.shape[1] - len(context_loc))):
self.optimizer.zero_grad()
x_context, y_context, x_target, y_target = x[:, context_loc, :], y[:, context_loc, :], x, y
# print(context_loc, y_target[:, context_loc, :])
try:
p_y_pred, q_target, q_context = self.neural_process(x_context, y_context, x_target, y_target)
except ValueError:
print(x_context.shape, y_context.shape, x_target.shape, y_target.shape)
print(context_loc)
loss = self._loss(p_y_pred, y_target, q_target, q_context)
loss.backward()
self.optimizer.step()
# call the agent to perform querying
mu = p_y_pred.loc.detach().numpy().squeeze(0)
sigma = p_y_pred.scale.detach().numpy().squeeze(0)
next_context = self.agents.get_next_query_point(mu, sigma, context_loc)
context_loc.append(next_context)
context_loc = sorted(context_loc)
epoch_loss += loss.item()
self.steps += 1
if self.steps % self.print_freq == 0:
print("iteration {}, loss {:.3f}".format(self.steps, loss.item()))
print("Epoch: {}, Avg_loss: {}".format(epoch, epoch_loss / len(data_loader)))
self.epoch_loss_history.append(epoch_loss / len(data_loader))
def _loss(self, p_y_pred, y_target, q_target, q_context):
log_likelihood = p_y_pred.log_prob(y_target).mean(dim=0).sum()
kl = kl_divergence(q_target, q_context).mean(dim=0).sum()
return -log_likelihood + kl
class NeuralProcessTrainer():
"""
Class to handle training of Neural Processes for functions and images.
Parameters
----------
device : torch.device
neural_process : neural_process.NeuralProcess or NeuralProcessImg instance
optimizer : one of torch.optim optimizers
num_context_range : tuple of ints
Number of context points will be sampled uniformly in the range given
by num_context_range.
num_extra_target_range : tuple of ints
Number of extra target points (as we always include context points in
target points, i.e. context points are a subset of target points) will
be sampled uniformly in the range given by num_extra_target_range.
print_freq : int
Frequency with which to print loss information during training.
"""
def __init__(self, device, neural_process, optimizer, num_context_range,
num_extra_target_range, print_freq=100):
self.device = device
self.neural_process = neural_process
self.optimizer = optimizer
self.num_context_range = num_context_range
self.num_extra_target_range = num_extra_target_range
self.print_freq = print_freq
# Check if neural process is for images
self.is_img = isinstance(self.neural_process, NeuralProcessImg)
self.steps = 0
self.epoch_loss_history = []
def train(self, data_loader, epochs):
"""
Trains Neural Process.
Parameters
----------
dataloader : torch.utils.DataLoader instance
epochs : int
Number of epochs to train for.
"""
for epoch in range(epochs):
epoch_loss = 0.
for i, data in enumerate(data_loader):
self.optimizer.zero_grad()
# Sample number of context and target points
num_context = randint(*self.num_context_range)
num_extra_target = randint(*self.num_extra_target_range)
# Create context and target points and apply neural process
if self.is_img:
img, _ = data # data is a tuple (img, label)
batch_size = img.size(0)
context_mask, target_mask = \
batch_context_target_mask(self.neural_process.img_size,
num_context, num_extra_target,
batch_size)
img = img.to(self.device)
context_mask = context_mask.to(self.device)
target_mask = target_mask.to(self.device)
p_y_pred, q_target, q_context = \
self.neural_process(img, context_mask, target_mask)
# Calculate y_target as this will be required for loss
_, y_target = img_mask_to_np_input(img, target_mask)
else:
x, y = data
x_context, y_context, x_target, y_target = \
context_target_split(x, y, num_context, num_extra_target)
p_y_pred, q_target, q_context = \
self.neural_process(x_context, y_context, x_target, y_target)
loss = self._loss(p_y_pred, y_target, q_target, q_context)
loss.backward()
self.optimizer.step()
epoch_loss += loss.item()
self.steps += 1
if self.steps % self.print_freq == 0:
print("iteration {}, loss {:.3f}".format(self.steps, loss.item()))
print("Epoch: {}, Avg_loss: {}".format(epoch, epoch_loss / len(data_loader)))
self.epoch_loss_history.append(epoch_loss / len(data_loader))
def _loss(self, p_y_pred, y_target, q_target, q_context):
"""
Computes Neural Process loss.
Parameters
----------
p_y_pred : one of torch.distributions.Distribution
Distribution over y output by Neural Process.
y_target : torch.Tensor
Shape (batch_size, num_target, y_dim)
q_target : one of torch.distributions.Distribution
Latent distribution for target points.
q_context : one of torch.distributions.Distribution
Latent distribution for context points.
"""
# Log likelihood has shape (batch_size, num_target, y_dim). Take mean
# over batch and sum over number of targets and dimensions of y
log_likelihood = p_y_pred.log_prob(y_target).mean(dim=0).sum()
# KL has shape (batch_size, r_dim). Take mean over batch and sum over
# r_dim (since r_dim is dimension of normal distribution)
kl = kl_divergence(q_target, q_context).mean(dim=0).sum()
return -log_likelihood + kl