Last active
April 9, 2020 17:59
-
-
Save hanneshapke/0c34dabc97bc2f4fbd0836d9e30f6832 to your computer and use it in GitHub Desktop.
How to generate the layer heat map in Keras 2.1
This file contains hidden or 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
| def get_heatmap(model, layer_name, matrix, y_labels): | |
| # obtain probability of the label with the highest certainty | |
| network_output = model.get_output_at(0)[:, np.argmax(y_labels)] | |
| # obtain the output vector and its dimension of the convolutional layer we want to visualize | |
| conv_layer, layer_output_dim = get_conv_layer(model, layer_name) | |
| # Setting up the calculation of the gradients between the output and the conv layer. Will be executed in the iteration step | |
| grads = K.gradients(network_output, conv_layer.output)[0] | |
| # average the gradients across our samples (one sample) and all filters | |
| pooled_grads = K.mean(grads, axis=(0, 2)) | |
| # set up the computation graph | |
| iterate = K.function([model.get_input_at(0)], [pooled_grads, conv_layer.output[0]]) | |
| # execute the computation graph with our converted document (matrix) as an input | |
| pooled_grad_value, conv_layer_output_value = iterate([matrix]) | |
| # loop over every layer output vector element and multiply it by the gradient of the element | |
| for i in range(layer_output_dim): | |
| conv_layer_output_value[i] *= pooled_grad_value[i] | |
| # calculating the average output value for each output dimension across all filters | |
| heatmap = np.mean(conv_layer_output_value, axis=-1) | |
| return norm_heatmap(heatmap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When i try to use your code ... i get the following error
ValueError: Tensor Tensor("Mean:0", shape=(4498,), dtype=float32) is not an element of this graph.