Created
July 15, 2019 12:05
-
-
Save MathiasGruber/8f73dffbae32d2bf922f5d99b6c3624c to your computer and use it in GitHub Desktop.
Given a keras model & some input, show output statistics for each layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow.keras.backend as K | |
def get_activations(model, model_inputs): | |
print('----- activations -----') | |
activations = [] | |
inp = model.input | |
model_multi_inputs_cond = True | |
if not isinstance(inp, list): | |
# only one input! let's wrap it in a list. | |
inp = [inp] | |
model_multi_inputs_cond = False | |
outputs = [] | |
names =[] | |
for layer in model.layers: | |
if not any(n in layer.name for n in ['input', 'model']): | |
outputs.append(layer.output) | |
names.append(layer.name) | |
funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs] # evaluation functions | |
if model_multi_inputs_cond: | |
list_inputs = [] | |
list_inputs.extend(model_inputs) | |
list_inputs.append(0.) | |
else: | |
list_inputs = [model_inputs, 0.] | |
# Learning phase. 0 = Test mode (no dropout or batch normalization) | |
# layer_outputs = [func([model_inputs, 0.])[0] for func in funcs] | |
layer_outputs = [func(list_inputs)[0] for func in funcs] | |
for i, layer_activations in enumerate(layer_outputs): | |
activations.append(layer_activations) | |
for a in layer_activations: | |
print('Layer: {}, Shape: {}, Mean: {:.2f}, Std: {:.2f}'.format( | |
names[i], a.shape, np.mean(a), np.std(a) | |
)) | |
return activations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment