-
Notifications
You must be signed in to change notification settings - Fork 21
/
param_collection.py
92 lines (79 loc) · 2.61 KB
/
param_collection.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
import tensorflow as tf
import numpy as np
class ParamCollection(object):
def __init__(self, sess, params):
"""
params should be a list of TensorFlow nodes.
"""
self._params = params
# Have to import the session to get the values being used.
self.sess = sess
self.sess.run(tf.variables_initializer(params))
@property
def params(self):
return self._params
def get_values(self):
"""
Returns list of values of parameter arrays
"""
return [self.sess.run(param) for param in self._params]
def get_shapes(self):
"""
Shapes of parameter arrays
"""
return [param.get_shape().as_list() for param in self._params]
def get_total_size(self):
"""
Total number of parameters
"""
return sum(np.prod(shape) for shape in self.get_shapes())
def num_vars(self):
"""
Number of parameter arrays
"""
return len(self._params)
def set_values(self, parvals):
"""
Set values of parameter arrays given list of values `parvals`
"""
assert len(parvals) == len(self._params)
for (param, newval) in zip(self._params, parvals):
self.sess.run(tf.assign(param, newval))
assert tuple(param.get_shape().as_list()) == newval.shape
def set_values_flat(self, theta):
"""
Set parameters using a vector which represents all of the parameters
flattened and concatenated.
"""
arrs = []
n = 0
for shape in self.get_shapes():
size = np.prod(shape)
arrs.append(theta[n:n+size].reshape(shape))
n += size
assert theta.size == n
self.set_values(arrs)
def get_values_flat(self):
"""
Flatten all parameter arrays into one vector and return it as a numpy array.
"""
theta = np.empty(self.get_total_size())
n = 0
for param in self._params:
s = np.prod(param.get_shape().as_list())
theta[n:n+s] = self.sess.run(param).flatten()
n += s
assert theta.size == n
return theta
def _params_names(self):
return [(param, param.name) for param in self._params]
def to_h5(self, grp):
"""
Save parameter arrays to hdf5 group `grp`
"""
for (param, name) in self._params_names():
arr = self.sess.run(param)
grp[name] = arr
def from_h5(self, grp):
parvals = [grp[name] for(_, name) in self._params_names()]
self.set_values(parvals)