Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to generate the layer heat map in Keras 2.1
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)
@sarim-zafar

This comment has been minimized.

Copy link

sarim-zafar commented Nov 18, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.