-
Notifications
You must be signed in to change notification settings - Fork 47
/
neural_process.py
188 lines (155 loc) · 6.36 KB
/
neural_process.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
import torch
from models import Encoder, MuSigmaEncoder, Decoder
from torch import nn
from torch.distributions import Normal
from utils import img_mask_to_np_input
class NeuralProcess(nn.Module):
"""
Implements Neural Process for functions of arbitrary dimensions.
Parameters
----------
x_dim : int
Dimension of x values.
y_dim : int
Dimension of y values.
r_dim : int
Dimension of output representation r.
z_dim : int
Dimension of latent variable z.
h_dim : int
Dimension of hidden layer in encoder and decoder.
"""
def __init__(self, x_dim, y_dim, r_dim, z_dim, h_dim):
super(NeuralProcess, self).__init__()
self.x_dim = x_dim
self.y_dim = y_dim
self.r_dim = r_dim
self.z_dim = z_dim
self.h_dim = h_dim
# Initialize networks
self.xy_to_r = Encoder(x_dim, y_dim, h_dim, r_dim)
self.r_to_mu_sigma = MuSigmaEncoder(r_dim, z_dim)
self.xz_to_y = Decoder(x_dim, z_dim, h_dim, y_dim)
def aggregate(self, r_i):
"""
Aggregates representations for every (x_i, y_i) pair into a single
representation.
Parameters
----------
r_i : torch.Tensor
Shape (batch_size, num_points, r_dim)
"""
return torch.mean(r_i, dim=1)
def xy_to_mu_sigma(self, x, y):
"""
Maps (x, y) pairs into the mu and sigma parameters defining the normal
distribution of the latent variables z.
Parameters
----------
x : torch.Tensor
Shape (batch_size, num_points, x_dim)
y : torch.Tensor
Shape (batch_size, num_points, y_dim)
"""
batch_size, num_points, _ = x.size()
# Flatten tensors, as encoder expects one dimensional inputs
x_flat = x.view(batch_size * num_points, self.x_dim)
y_flat = y.contiguous().view(batch_size * num_points, self.y_dim)
# Encode each point into a representation r_i
r_i_flat = self.xy_to_r(x_flat, y_flat)
# Reshape tensors into batches
r_i = r_i_flat.view(batch_size, num_points, self.r_dim)
# Aggregate representations r_i into a single representation r
r = self.aggregate(r_i)
# Return parameters of distribution
return self.r_to_mu_sigma(r)
def forward(self, x_context, y_context, x_target, y_target=None):
"""
Given context pairs (x_context, y_context) and target points x_target,
returns a distribution over target points y_target.
Parameters
----------
x_context : torch.Tensor
Shape (batch_size, num_context, x_dim). Note that x_context is a
subset of x_target.
y_context : torch.Tensor
Shape (batch_size, num_context, y_dim)
x_target : torch.Tensor
Shape (batch_size, num_target, x_dim)
y_target : torch.Tensor or None
Shape (batch_size, num_target, y_dim). Only used during training.
Note
----
We follow the convention given in "Empirical Evaluation of Neural
Process Objectives" where context is a subset of target points. This was
shown to work best empirically.
"""
# Infer quantities from tensor dimensions
batch_size, num_context, x_dim = x_context.size()
_, num_target, _ = x_target.size()
_, _, y_dim = y_context.size()
if self.training:
# Encode target and context (context needs to be encoded to
# calculate kl term)
mu_target, sigma_target = self.xy_to_mu_sigma(x_target, y_target)
mu_context, sigma_context = self.xy_to_mu_sigma(x_context, y_context)
# Sample from encoded distribution using reparameterization trick
q_target = Normal(mu_target, sigma_target)
q_context = Normal(mu_context, sigma_context)
z_sample = q_target.rsample()
# Get parameters of output distribution
y_pred_mu, y_pred_sigma = self.xz_to_y(x_target, z_sample)
p_y_pred = Normal(y_pred_mu, y_pred_sigma)
return p_y_pred, q_target, q_context
else:
# At testing time, encode only context
mu_context, sigma_context = self.xy_to_mu_sigma(x_context, y_context)
# Sample from distribution based on context
q_context = Normal(mu_context, sigma_context)
z_sample = q_context.rsample()
# Predict target points based on context
y_pred_mu, y_pred_sigma = self.xz_to_y(x_target, z_sample)
p_y_pred = Normal(y_pred_mu, y_pred_sigma)
return p_y_pred
class NeuralProcessImg(nn.Module):
"""
Wraps regular Neural Process for image processing.
Parameters
----------
img_size : tuple of ints
E.g. (1, 28, 28) or (3, 32, 32)
r_dim : int
Dimension of output representation r.
z_dim : int
Dimension of latent variable z.
h_dim : int
Dimension of hidden layer in encoder and decoder.
"""
def __init__(self, img_size, r_dim, z_dim, h_dim):
super(NeuralProcessImg, self).__init__()
self.img_size = img_size
self.num_channels, self.height, self.width = img_size
self.r_dim = r_dim
self.z_dim = z_dim
self.h_dim = h_dim
self.neural_process = NeuralProcess(x_dim=2, y_dim=self.num_channels,
r_dim=r_dim, z_dim=z_dim,
h_dim=h_dim)
def forward(self, img, context_mask, target_mask):
"""
Given an image and masks of context and target points, returns a
distribution over pixel intensities at the target points.
Parameters
----------
img : torch.Tensor
Shape (batch_size, channels, height, width)
context_mask : torch.ByteTensor
Shape (batch_size, height, width). Binary mask indicating
the pixels to be used as context.
target_mask : torch.ByteTensor
Shape (batch_size, height, width). Binary mask indicating
the pixels to be used as target.
"""
x_context, y_context = img_mask_to_np_input(img, context_mask)
x_target, y_target = img_mask_to_np_input(img, target_mask)
return self.neural_process(x_context, y_context, x_target, y_target)