-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathestimators.py
227 lines (185 loc) · 6.97 KB
/
estimators.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# based on Sean Robertson's pydrobert-pytorch.estimators
import torch
import torch.nn as nn
import torch.nn.functional as F
from distributions import PlackettLuce
from utils import logcumsumexp, reverse_logcumsumexp, smart_perm, make_permutation_matrix
from itertools import permutations
def to_z(logits, u=None):
'''Samples random noise, then injects it into logits to produce z
Parameters
----------
logits : torch.Tensor
u : torch.Tensor (you can specify noise yourself)
Returns
-------
z : torch.Tensor
'''
if u is not None:
assert u.size() == logits.size()
else:
u = torch.distributions.utils.clamp_probs(torch.rand_like(logits))
log_probs = F.log_softmax(logits, dim=-1)
z = log_probs - torch.log(-torch.log(u))
z.requires_grad_(True)
return z
def _to_z_tilde(logits, b, v=None):
'''Produce posterior z
Parameters
----------
logits : torch.Tensor
b : torch.Tensor
v : torch.Tensor (you can specify noise yourself)
Returns
-------
z : torch.Tensor
'''
if v is not None:
assert v.size() == logits.size()
else:
v = torch.distributions.utils.clamp_probs(torch.rand_like(logits))
b_inv = torch.sort(b, dim=-1)[1]
log_probs = smart_perm(F.log_softmax(logits, dim=-1), b)
gumbel = torch.log(-torch.log(v))
z_tilde = -logcumsumexp(gumbel - reverse_logcumsumexp(log_probs, dim=-1), dim=-1)
z_tilde = smart_perm(z_tilde, b_inv)
return z_tilde
def _reattach_z_to_new_logits(logits, z):
log_probs = F.log_softmax(logits, dim=-1)
z = z.detach() + log_probs - log_probs.detach()
return z
def to_b(z):
'''Converts z to sample using a deterministic mapping
Parameters
----------
z : torch.Tensor
Returns
-------
b : torch.Tensor
'''
b = torch.sort(z, descending=True, dim=-1)[1]
return b
def reinforce(fb, b, logits, **kwargs):
r'''Perform REINFORCE gradient estimation
REINFORCE [williams1992]_, or the score function, has a single-sample
implementation as
.. math:: g = f(b) \partial \log Pr(b; logits) / \partial logits
It is an unbiased estimate of the derivative of the expectation w.r.t
`logits`.
Though simple, it is often cited as high variance.
Parameters
----------
fb : torch.Tensor
b : torch.Tensor
logits : torch.Tensor
Returns
-------
g : torch.tensor
A tensor with the same shape as `logits` representing the estimate
of ``d fb / d logits``
Notes
-----
It is common (such as in A2C) to include a baseline to minimize the
variance of the estimate. It's incorporated as `c` in
.. math:: g = (f(b) - c) \partial \log Pr(b; logits) / \partial logits
Note that :math:`c_i` should be conditionally independent of :math:`b_i`
for `g` to be unbiased. You can, however, condition on any preceding
outputs :math:`b_{i - j}, j > 0` and all of `logits`.
To get this functionality, simply subtract `c` from `fb` before passing it
to this method. If `c` is the output of a neural network, a common (but
sub-optimal) loss function is the mean-squared error between `fb` and `c`.
'''
fb = fb.detach()
b = b.detach()
log_pb = PlackettLuce(logits=logits).log_prob(b)
fb = fb.unsqueeze(-1)
g = fb * torch.autograd.grad([log_pb], [logits], grad_outputs=torch.ones_like(log_pb))[0]
assert g.size() == logits.size()
return g
def relax(fb, b, logits, z, c, v=None, **kwargs):
r'''Perform RELAX gradient estimation
RELAX [grathwohl2017]_ has a single-sample implementation as
.. math::
g = (f(b) - c(\widetilde{z}))
\partial \log Pr(b; logits) / \partial logits
+ \partial c(z) / \partial logits
- \partial c(\widetilde{z}) / \partial logits
where :math:`b = H(z)`, :math:`\widetilde{z} \sim Pr(z|b, logits)`, and `c`
can be any differentiable function. It is an unbiased estimate of the
derivative of the expectation w.r.t `logits`.
`g` is itself differentiable with respect to the parameters of the control
variate `c`. If the c is trainable, an easy choice for its loss is to
minimize the variance of `g` via ``(g ** 2).sum().backward()``. Propagating
directly from `g` should be suitable for most situations. Insofar as the
loss cannot be directly computed from `g`, setting the argument for
`components` to true will return a tuple containing the terms of `g`
instead.
Parameters
----------
fb : torch.Tensor
b : torch.Tensor
logits : torch.Tensor
z : torch.Tensor
c : callable
A module or function that accepts input of the shape of `z` and outputs
a tensor of the same shape if modelling a Bernoulli, or of shape
``z[..., 0]`` (minus the last dimension) if Categorical.
Returns
-------
g : torch.Tensor or tuple
`g` will be the gradient estimate with respect to `logits`.
'''
fb = fb.detach()
b = b.detach()
# in all computations (including those made by the user later), we don't
# want to backpropagate past "logits" into the model. We make a detached
# copy of logits and rebuild the graph from the detached copy to z
logits = logits.detach().requires_grad_(True)
z = _reattach_z_to_new_logits(logits, z)
z_tilde = _to_z_tilde(logits, b, v)
c_z = c(z, **kwargs)
c_z_tilde = c(z_tilde, **kwargs)
fb = fb.unsqueeze(-1)
diff = fb - c_z_tilde
log_pb = PlackettLuce(logits=logits).log_prob(b)
dlog_pb, = torch.autograd.grad(
[log_pb],
[logits],
grad_outputs=torch.ones_like(log_pb)
)
# we need `create_graph` to be true here or backpropagation through the
# control variate will not include the graphs of the derivative terms
dc_z, = torch.autograd.grad(
[c_z],
[logits],
create_graph=True,
retain_graph=True,
grad_outputs=torch.ones_like(c_z)
)
dc_z_tilde, = torch.autograd.grad(
[c_z_tilde],
[logits],
create_graph=True,
retain_graph=True,
grad_outputs=torch.ones_like(c_z_tilde)
)
g = diff * dlog_pb + dc_z - dc_z_tilde
assert g.size() == logits.size()
return g
class ExactGradientEstimator(nn.Module):
"""
just computing exact gradient using log-derivative trick
and taking expectation over all possible permutations
"""
def __init__(self, f, d):
super(ExactGradientEstimator, self).__init__()
self.f = f
self.d = d
self.pi = torch.tensor(list(permutations(range(self.d))))
self.f_all = self.f(make_permutation_matrix(self.pi))
def forward(self, log_theta, **kwargs):
# return gradients and expectation
logprob = PlackettLuce(log_theta).log_prob(self.pi)
loss = (torch.exp(logprob).detach() * self.f_all * logprob).sum()
d_log_theta = torch.autograd.grad([loss], [log_theta])[0]
return d_log_theta, (torch.exp(logprob).detach() * self.f_all).sum()