MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _make_histogram_ops

Method _make_histogram_ops

tensorflow/python/keras/callbacks_v1.py:177–230  ·  view source on GitHub ↗

Defines histogram ops when histogram_freq > 0.

(self, model)

Source from the content-addressed store, hash-verified

175 self.writer = tf_summary.FileWriter(self.log_dir)
176
177 def _make_histogram_ops(self, model):
178 """Defines histogram ops when histogram_freq > 0."""
179 # only make histogram summary op if it hasn't already been made
180 if self.histogram_freq and self.merged is None:
181 for layer in self.model.layers:
182 for weight in layer.weights:
183 mapped_weight_name = weight.name.replace(':', '_')
184 tf_summary.histogram(mapped_weight_name, weight)
185 if self.write_images:
186 w_img = array_ops.squeeze(weight)
187 shape = K.int_shape(w_img)
188 if len(shape) == 2: # dense layer kernel case
189 if shape[0] > shape[1]:
190 w_img = array_ops.transpose(w_img)
191 shape = K.int_shape(w_img)
192 w_img = array_ops.reshape(w_img, [1, shape[0], shape[1], 1])
193 elif len(shape) == 3: # convnet case
194 if K.image_data_format() == 'channels_last':
195 # switch to channels_first to display
196 # every kernel as a separate image
197 w_img = array_ops.transpose(w_img, perm=[2, 0, 1])
198 shape = K.int_shape(w_img)
199 w_img = array_ops.reshape(w_img,
200 [shape[0], shape[1], shape[2], 1])
201 elif len(shape) == 1: # bias case
202 w_img = array_ops.reshape(w_img, [1, shape[0], 1, 1])
203 else:
204 # not possible to handle 3D convnets etc.
205 continue
206
207 shape = K.int_shape(w_img)
208 assert len(shape) == 4 and shape[-1] in [1, 3, 4]
209 tf_summary.image(mapped_weight_name, w_img)
210
211 if self.write_grads:
212 for weight in layer.trainable_weights:
213 mapped_weight_name = weight.name.replace(':', '_')
214 grads = model.optimizer.get_gradients(model.total_loss, weight)
215
216 def is_indexed_slices(grad):
217 return type(grad).__name__ == 'IndexedSlices'
218
219 grads = [
220 grad.values if is_indexed_slices(grad) else grad
221 for grad in grads
222 ]
223 tf_summary.histogram('{}_grad'.format(mapped_weight_name), grads)
224
225 if hasattr(layer, 'output'):
226 if isinstance(layer.output, list):
227 for i, output in enumerate(layer.output):
228 tf_summary.histogram('{}_out_{}'.format(layer.name, i), output)
229 else:
230 tf_summary.histogram('{}_out'.format(layer.name), layer.output)
231
232 def set_model(self, model):
233 """Sets Keras model and creates summary ops."""

Callers 1

set_modelMethod · 0.95

Calls 7

is_indexed_slicesFunction · 0.85
replaceMethod · 0.80
histogramMethod · 0.80
transposeMethod · 0.80
reshapeMethod · 0.80
get_gradientsMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected