-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
271 lines (194 loc) · 8.73 KB
/
monitor.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
import numpy as np
from functools import reduce
import cupy
import chainer
# The name template of the statistic to collect and include in the report,
# e.g. 'predictor/conv1/W/grad/percentile/sigma_one'
key_template = '{model}/{layer}/{param}/{attr}/{statistic}'
def weight_statistics(model, layer_name=None):
"""Collect weight statistict from the given model and return it as a
``dict``.
Args:
model (~chainer.Chain): The model from which statistics are collected.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
return parameter_statistics(model, 'W', 'data', layer_name)
def bias_statistics(model, layer_name=None):
"""Collect bias statistict from the given model and return it as a
``dict``.
Args:
model (~chainer.Chain): The model from which statistics are collected.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
return parameter_statistics(model, 'b', 'data', layer_name)
def weight_gradient_statistics(model, layer_name=None):
"""Collect weight gradient statistict from the given model and return it
as a ``dict``.
Args:
model (~chainer.Chain): The model from which statistics are collected.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
return parameter_statistics(model, 'W', 'grad', layer_name)
def bias_gradient_statistics(model, layer_name=None):
"""Collect bias gradient statistict from the given model and return it
as a ``dict``.
Args:
model (~chainer.Chain): The model from which statistics are collected.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
return parameter_statistics(model, 'b', 'grad', layer_name)
def sparsity(model, include_bias=False, layer_name=None):
"""Count the number of parameters with the value zero for the given model
and return it as a ``dict``.
Args:
model (~chainer.Chain): The model from which statistics are collected.
include_bias (bool): ``True`` to include the number of biases that are
zero, ``False`` to exclude them.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
xp = model.xp
def reduce_count_zeros(acc, param):
if param.name == 'W' or (include_bias and param.name == 'b'):
acc += param.data.size - xp.count_nonzero(param.data)
return acc
if layer_name is not None:
sparsity = reduce(reduce_count_zeros, [getattr(model, layer_name)], 0)
else:
sparsity = reduce(reduce_count_zeros, model.params(), 0)
key = key_template.format(model=model.name,
layer='*' if layer_name is None else layer_name,
param='Wb' if include_bias else 'W' ,
attr='sparsity',
statistic='zeros')
return { key: sparsity }
def parameter_statistics(model, param_name, attr_name, layer_name=None):
"""Collect statistict from the given model and return it as a ``dict``.
The returned ``dict`` contains a key for each metric, mapping to a NumPy
or CuPy ``float32`` value depending on if the given model was on the CPU or
the GPU.
Args:
model (~chainer.Chain): The model from which statistics are collected.
param_name (str): Name of the parameter, ``'W'`` or ``'b'``.
attr_name (str): Name of the attribute, ``'data'`` or ``'grad'``.
layer_name (str): Name of the layer which may be specified or set to
``None`` to aggregate over all layers.
Returns:
dict: Parameter statistics.
"""
if layer_name is not None: # Collect statistics for a single layer only
l = getattr(model, layer_name)
lp = layer_params(l, param_name, attr_name)
return as_statistics(lp, model.name, param_name, attr_name,
layer_name=layer_name)
lp = layers_params(model, param_name, attr_name)
return as_statistics(lp, model.name, param_name, attr_name)
def layer_params(layer, param_name, attr_name):
"""Return parameters in a flattened array from the given layer or an empty
array if the parameters are not found.
Args:
layer (~chainer.Link): The layer from which parameters are collected.
param_name (str): Name of the parameter, ``'W'`` or ``'b'``.
attr_name (str): Name of the attribute, ``'data'`` or ``'grad'``.
Returns:
array: Flattened array of parameters.
"""
if isinstance(layer, chainer.Chain):
# Nested chainer.Chain, aggregate all underlying statistics
return layers_params(layer, param_name, attr_name)
elif not hasattr(layer, param_name):
return layer.xp.array([])
params = getattr(layer, param_name)
params = getattr(params, attr_name)
return params.flatten()
def layers_params(model, param_name, attr_name):
"""Return all parameters in a flattened array from the given model.
Args:
model (~chainer.Chain): The model from which parameters are collected.
param_name (str): Name of the parameter, ``'W'`` or ``'b'``.
attr_name (str): Name of the attribute, ``'data'`` or ``'grad'``.
Returns:
array: Flattened array of parameters.
"""
xp = model.xp
params = xp.array([], dtype=xp.float32)
for param in model.params():
if param.name == param_name:
values = getattr(param, attr_name)
values = values.flatten()
params = xp.concatenate((params, values)) # Slow?
return params
def as_statistics(data, model_name, param_name, attr_name, *, layer_name=None,
statistics=('min', 'max', 'mean', 'std'),
percentiles=(0.13, 2.28, 15.87, 50, 84.13, 97.72, 99.87)):
"""Compute statistics based on the given data and return it as a ``dict``.
Args:
data (array): NumPy or CuPy array of data.
model_name (str): Name of the model, e.g. ``predictor``.
param_name (str): Name of the parameter, ``'W'`` or ``'b'``.
attr_name (str): Name of the attribute, ``'data'`` or ``'grad'``.
layer_name (str): Name of the layer which may be specified or set to
``None``. In the case of ``None`` the layer name will be set to
``'*'``.
Returns:
dict: Parameter statistics.
"""
stats = {}
if layer_name is None:
layer_name = '*'
if percentiles:
ps = get_percentiles(data, sigma=percentiles)
for i, percentile in enumerate(ps):
key = key_template.format(model=model_name,
layer=layer_name,
param=param_name,
attr=attr_name,
statistic='percentile/{}'.format(i))
stats[key] = percentile
for s in statistics:
key = key_template.format(model=model_name,
layer=layer_name,
param=param_name,
attr=attr_name,
statistic=s)
try:
stats[key] = getattr(data, s)()
except ValueError:
# If data is missing from uninitialized model parameters, add
# NaN placeholders instead of skipping the measurements completely
# or registering zeros
stats[key] = float('NaN')
return stats
def get_percentiles(data, sigma):
"""Compute percentiles for data and return an array with the same length
as the number of elements in ``sigma``.
Args:
data (array): 1-dimensional NumPy or CuPy arryay.
sigma (tuple): Sigmas for which percentiles are computed.
Returns:
array: Array of percentiles.
"""
def _get_percentiles(_data, _sigma):
try:
return np.percentile(_data, _sigma)
except IndexError: # Handle uninitialized model parameters
return np.array((float('NaN'),) * 7)
if isinstance(data, cupy.ndarray):
# TODO(hvy): Make percentile computation faster for GPUs
data = cupy.asnumpy(data)
return cupy.asarray(_get_percentiles(data, sigma))
return _get_percentiles(data, sigma)