forked from EmilienDupont/neural-processes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
132 lines (105 loc) · 5.12 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
import torch
from random import randint
from neural_process import NeuralProcessImg
from torch.distributions.kl import kl_divergence
from utils import (context_target_split, batch_context_target_mask,
img_mask_to_np_input)
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 = []
self.batches = 16
def train(self, data_loader, epochs):
"""
Trains Neural Process.
Parameters
----------
data_loader : torch.utils.DataLoader instance
epochs : int
Number of epochs to train for.
"""
for epoch in range(epochs):
epoch_loss = 0.
self.neural_process.hidden = self.neural_process.gru.init_hidden(self.batches)
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