Skip to content

Instantly share code, notes, and snippets.

@nestorsgarzonc
Created October 3, 2020 19:10
Show Gist options
  • Select an option

  • Save nestorsgarzonc/5cca1f3b29678c6dc31c5100b7803c40 to your computer and use it in GitHub Desktop.

Select an option

Save nestorsgarzonc/5cca1f3b29678c6dc31c5100b7803c40 to your computer and use it in GitHub Desktop.
Visualice activations in convolutions
from keras.layers import Conv2D,Flatten
model = Sequential()
# Add a convolutional layer of 32 filters of size 3x3
model.add(Conv2D(32, kernel_size = 3, input_shape = (28, 28, 1), activation = 'relu'))
# Add a convolutional layer of 16 filters of size 3x3
model.add(Conv2D(16, kernel_size =3, activation = 'relu'))
# Flatten the previous layer output
model.add(Flatten())
# Add as many outputs as classes with softmax activation
model.add(Dense(10, activation = 'softmax'))
#TRAIN...
# Obtain a reference to the outputs of the first layer
first_layer_output = model.layers[0].output
# Build a model using the model's input and the first layer output
first_layer_model = Model(inputs = model.layers[0].input, outputs = first_layer_output)
# Use this model to predict on X_test
activations = first_layer_model.predict(X_test)
# Plot the activations of first digit of X_test for the 15th filter
axs[0].matshow(activations[0,:,:,14], cmap = 'viridis')
# Do the same but for the 18th filter now
axs[1].matshow(activations[0,:,:,17], cmap = 'viridis')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment