forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeanof.py
522 lines (398 loc) · 14 KB
/
theanof.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# Copyright 2020 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import theano
from theano import change_flags, scalar
from theano import tensor as tt
from theano.gof import Op
from theano.gof.graph import inputs
from theano.sandbox.rng_mrg import MRG_RandomStreams
from pymc3.blocking import ArrayOrdering
from pymc3.data import GeneratorAdapter
from pymc3.vartypes import continuous_types, int_types, typefilter
__all__ = [
"gradient",
"hessian",
"hessian_diag",
"inputvars",
"cont_inputs",
"floatX",
"intX",
"smartfloatX",
"jacobian",
"CallableTensor",
"join_nonshared_inputs",
"make_shared_replacements",
"generator",
"set_tt_rng",
"tt_rng",
"take_along_axis",
]
def inputvars(a):
"""
Get the inputs into a theano variables
Parameters
----------
a: theano variable
Returns
-------
r: list of tensor variables that are inputs
"""
return [v for v in inputs(makeiter(a)) if isinstance(v, tt.TensorVariable)]
def cont_inputs(f):
"""
Get the continuous inputs into a theano variables
Parameters
----------
a: theano variable
Returns
-------
r: list of tensor variables that are continuous inputs
"""
return typefilter(inputvars(f), continuous_types)
def floatX(X):
"""
Convert a theano tensor or numpy array to theano.config.floatX type.
"""
try:
return X.astype(theano.config.floatX)
except AttributeError:
# Scalar passed
return np.asarray(X, dtype=theano.config.floatX)
_conversion_map = {"float64": "int32", "float32": "int16", "float16": "int8", "float8": "int8"}
def intX(X):
"""
Convert a theano tensor or numpy array to theano.tensor.int32 type.
"""
intX = _conversion_map[theano.config.floatX]
try:
return X.astype(intX)
except AttributeError:
# Scalar passed
return np.asarray(X, dtype=intX)
def smartfloatX(x):
"""
Converts numpy float values to floatX and leaves values of other types unchanged.
"""
if str(x.dtype).startswith("float"):
x = floatX(x)
return x
"""
Theano derivative functions
"""
def gradient1(f, v):
"""flat gradient of f wrt v"""
return tt.flatten(tt.grad(f, v, disconnected_inputs="warn"))
empty_gradient = tt.zeros(0, dtype="float32")
def gradient(f, vars=None):
if vars is None:
vars = cont_inputs(f)
if vars:
return tt.concatenate([gradient1(f, v) for v in vars], axis=0)
else:
return empty_gradient
def jacobian1(f, v):
"""jacobian of f wrt v"""
f = tt.flatten(f)
idx = tt.arange(f.shape[0], dtype="int32")
def grad_i(i):
return gradient1(f[i], v)
return theano.map(grad_i, idx)[0]
def jacobian(f, vars=None):
if vars is None:
vars = cont_inputs(f)
if vars:
return tt.concatenate([jacobian1(f, v) for v in vars], axis=1)
else:
return empty_gradient
def jacobian_diag(f, x):
idx = tt.arange(f.shape[0], dtype="int32")
def grad_ii(i):
return theano.grad(f[i], x)[i]
return theano.scan(grad_ii, sequences=[idx], n_steps=f.shape[0], name="jacobian_diag")[0]
@change_flags(compute_test_value="ignore")
def hessian(f, vars=None):
return -jacobian(gradient(f, vars), vars)
@change_flags(compute_test_value="ignore")
def hessian_diag1(f, v):
g = gradient1(f, v)
idx = tt.arange(g.shape[0], dtype="int32")
def hess_ii(i):
return gradient1(g[i], v)[i]
return theano.map(hess_ii, idx)[0]
@change_flags(compute_test_value="ignore")
def hessian_diag(f, vars=None):
if vars is None:
vars = cont_inputs(f)
if vars:
return -tt.concatenate([hessian_diag1(f, v) for v in vars], axis=0)
else:
return empty_gradient
def makeiter(a):
if isinstance(a, (tuple, list)):
return a
else:
return [a]
class IdentityOp(scalar.UnaryScalarOp):
@staticmethod
def st_impl(x):
return x
def impl(self, x):
return x
def grad(self, inp, grads):
return grads
def c_code(self, node, name, inp, out, sub):
return "{z} = {x};".format(x=inp[0], z=out[0])
def __eq__(self, other):
return isinstance(self, type(other))
def __hash__(self):
return hash(type(self))
def make_shared_replacements(vars, model):
"""
Makes shared replacements for all *other* variables than the ones passed.
This way functions can be called many times without setting unchanging variables. Allows us
to use func.trust_input by removing the need for DictToArrayBijection and kwargs.
Parameters
----------
vars: list of variables not to make shared
model: model
Returns
-------
Dict of variable -> new shared variable
"""
othervars = set(model.vars) - set(vars)
return {var: theano.shared(var.tag.test_value, var.name + "_shared") for var in othervars}
def join_nonshared_inputs(xs, vars, shared, make_shared=False):
"""
Takes a list of theano Variables and joins their non shared inputs into a single input.
Parameters
----------
xs: list of theano tensors
vars: list of variables to join
Returns
-------
tensors, inarray
tensors: list of same tensors but with inarray as input
inarray: vector of inputs
"""
if not vars:
raise ValueError("Empty list of variables.")
joined = tt.concatenate([var.ravel() for var in vars])
if not make_shared:
tensor_type = joined.type
inarray = tensor_type("inarray")
else:
inarray = theano.shared(joined.tag.test_value, "inarray")
ordering = ArrayOrdering(vars)
inarray.tag.test_value = joined.tag.test_value
get_var = {var.name: var for var in vars}
replace = {
get_var[var]: reshape_t(inarray[slc], shp).astype(dtyp)
for var, slc, shp, dtyp in ordering.vmap
}
replace.update(shared)
xs_special = [theano.clone(x, replace, strict=False) for x in xs]
return xs_special, inarray
def reshape_t(x, shape):
"""Work around fact that x.reshape(()) doesn't work"""
if shape != ():
return x.reshape(shape)
else:
return x[0]
class CallableTensor:
"""Turns a symbolic variable with one input into a function that returns symbolic arguments
with the one variable replaced with the input.
"""
def __init__(self, tensor):
self.tensor = tensor
def __call__(self, input):
"""Replaces the single input of symbolic variable to be the passed argument.
Parameters
----------
input: TensorVariable
"""
(oldinput,) = inputvars(self.tensor)
return theano.clone(self.tensor, {oldinput: input}, strict=False)
scalar_identity = IdentityOp(scalar.upgrade_to_float, name="scalar_identity")
identity = tt.Elemwise(scalar_identity, name="identity")
class GeneratorOp(Op):
"""
Generator Op is designed for storing python generators inside theano graph.
__call__ creates TensorVariable
It has 2 new methods
- var.set_gen(gen): sets new generator
- var.set_default(value): sets new default value (None erases default value)
If generator is exhausted, variable will produce default value if it is not None,
else raises `StopIteration` exception that can be caught on runtime.
Parameters
----------
gen: generator that implements __next__ (py3) or next (py2) method
and yields np.arrays with same types
default: np.array with the same type as generator produces
"""
__props__ = ("generator",)
def __init__(self, gen, default=None):
super().__init__()
if not isinstance(gen, GeneratorAdapter):
gen = GeneratorAdapter(gen)
self.generator = gen
self.set_default(default)
def make_node(self, *inputs):
gen_var = self.generator.make_variable(self)
return theano.Apply(self, [], [gen_var])
def perform(self, node, inputs, output_storage, params=None):
if self.default is not None:
output_storage[0][0] = next(self.generator, self.default)
else:
output_storage[0][0] = next(self.generator)
def do_constant_folding(self, node):
return False
__call__ = change_flags(compute_test_value="off")(Op.__call__)
def set_gen(self, gen):
if not isinstance(gen, GeneratorAdapter):
gen = GeneratorAdapter(gen)
if not gen.tensortype == self.generator.tensortype:
raise ValueError("New generator should yield the same type")
self.generator = gen
def set_default(self, value):
if value is None:
self.default = None
else:
value = np.asarray(value, self.generator.tensortype.dtype)
t1 = (False,) * value.ndim
t2 = self.generator.tensortype.broadcastable
if not t1 == t2:
raise ValueError("Default value should have the same type as generator")
self.default = value
def generator(gen, default=None):
"""
Generator variable with possibility to set default value and new generator.
If generator is exhausted variable will produce default value if it is not None,
else raises `StopIteration` exception that can be caught on runtime.
Parameters
----------
gen: generator that implements __next__ (py3) or next (py2) method
and yields np.arrays with same types
default: np.array with the same type as generator produces
Returns
-------
TensorVariable
It has 2 new methods
- var.set_gen(gen): sets new generator
- var.set_default(value): sets new default value (None erases default value)
"""
return GeneratorOp(gen, default)()
_tt_rng = MRG_RandomStreams()
def tt_rng(random_seed=None):
"""
Get the package-level random number generator or new with specified seed.
Parameters
----------
random_seed: int
If not None
returns *new* theano random generator without replacing package global one
Returns
-------
`theano.sandbox.rng_mrg.MRG_RandomStreams` instance
`theano.sandbox.rng_mrg.MRG_RandomStreams`
instance passed to the most recent call of `set_tt_rng`
"""
if random_seed is None:
return _tt_rng
else:
ret = MRG_RandomStreams(random_seed)
return ret
def set_tt_rng(new_rng):
"""
Set the package-level random number generator.
Parameters
----------
new_rng: `theano.sandbox.rng_mrg.MRG_RandomStreams` instance
The random number generator to use.
"""
# pylint: disable=global-statement
global _tt_rng
# pylint: enable=global-statement
if isinstance(new_rng, int):
new_rng = MRG_RandomStreams(new_rng)
_tt_rng = new_rng
def floatX_array(x):
return floatX(np.array(x))
def ix_(*args):
"""
Theano np.ix_ analog
See numpy.lib.index_tricks.ix_ for reference
"""
out = []
nd = len(args)
for k, new in enumerate(args):
if new is None:
out.append(slice(None))
new = tt.as_tensor(new)
if new.ndim != 1:
raise ValueError("Cross index must be 1 dimensional")
new = new.reshape((1,) * k + (new.size,) + (1,) * (nd - k - 1))
out.append(new)
return tuple(out)
def largest_common_dtype(tensors):
dtypes = {
str(t.dtype) if hasattr(t, "dtype") else smartfloatX(np.asarray(t)).dtype for t in tensors
}
return np.stack([np.ones((), dtype=dtype) for dtype in dtypes]).dtype
def _make_along_axis_idx(arr_shape, indices, axis):
# compute dimensions to iterate over
if str(indices.dtype) not in int_types:
raise IndexError("`indices` must be an integer array")
shape_ones = (1,) * indices.ndim
dest_dims = list(range(axis)) + [None] + list(range(axis + 1, indices.ndim))
# build a fancy index, consisting of orthogonal aranges, with the
# requested index inserted at the right location
fancy_index = []
for dim, n in zip(dest_dims, arr_shape):
if dim is None:
fancy_index.append(indices)
else:
ind_shape = shape_ones[:dim] + (-1,) + shape_ones[dim + 1 :]
fancy_index.append(tt.arange(n).reshape(ind_shape))
return tuple(fancy_index)
def take_along_axis(arr, indices, axis=0):
"""Take values from the input array by matching 1d index and data slices.
This iterates over matching 1d slices oriented along the specified axis in
the index and data arrays, and uses the former to look up values in the
latter. These slices can be different lengths.
Functions returning an index along an axis, like argsort and argpartition,
produce suitable indices for this function.
"""
arr = tt.as_tensor_variable(arr)
indices = tt.as_tensor_variable(indices)
# normalize inputs
if axis is None:
arr = arr.flatten()
arr_shape = (len(arr),) # flatiter has no .shape
_axis = 0
else:
if axis < 0:
_axis = arr.ndim + axis
else:
_axis = axis
if _axis < 0 or _axis >= arr.ndim:
raise ValueError(
"Supplied `axis` value {} is out of bounds of an array with "
"ndim = {}".format(axis, arr.ndim)
)
arr_shape = arr.shape
if arr.ndim != indices.ndim:
raise ValueError("`indices` and `arr` must have the same number of dimensions")
# use the fancy index
return arr[_make_along_axis_idx(arr_shape, indices, _axis)]