# Keras: visualizing the output of an intermediate layer

I have read the docs here and I understand the general idea. I am able to visualize the weights of the intermediate layers. However, I'm having trouble visualize the activations. Here's what I have:

I trained my model and saved the weights in a file called weights_file.

Thanks to this jupyter notebook, I got the values of the weights. First I defined my model:

def mlp_model(hid_dim=10):

model = Sequential()
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

return model

model_created = mlp_model(hid_dim=15)


To get the weights, I did this:

W = model_created.layers[0].kernel.get_value(borrow=True)
W = np.squeeze(W)
print("W shape : ", W.shape) #(153, 15)

W_out = model_created.layers[1].kernel.get_value(borrow=True)
W_out = np.squeeze(W_out)
print('W_out shape : ', W_out.shape) #(15, 8)


From there I could create Hinton diagrams using this. However, when I try to work with the activations:

get_first_output = theano.function([model_created.layers[0].input], [model_created.layers[1].output])
layer_out = get_first_output([X[0,:]])[0]


I get this error:

TypeError: ('Bad input argument to theano function with name "mlp1_visualize_weights.py:131" at index 0 (0-based).  \nBacktrace when that variable is created:\n\n  File "mlp1_visualize_weights.py", line 213, in <module>\n    mlp_repeat(X, Y, Xtest, Ytest, params_to_use, weights_file)\n  File "mlp1_visualize_weights.py", line 125, in mlp_repeat\n    model_created = mlp_model(hid_dim=hid_val, lr=lrate, reg_val=reg, momentum=moment, nest=nestval, optimizer=optim)\n  File "mlp1_visualize_weights.py", line 105, in mlp_model\n    model.add(Dense(units=hid_dim, input_dim=X.shape[1], kernel_initializer=\'he_uniform\', activation=\'relu\', W_regularizer=l2(reg_val), b_regularizer=l2(reg_val)))\n  File "/mnt/data/caugusta/pkgs/anaconda2/lib/python2.7/site-packages/keras/models.py", line 426, in add\n    dtype=layer.dtype, name=layer.name + \'_input\')\n  File "/mnt/data/caugusta/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1392, in Input\n    input_tensor=tensor)\n  File "/mnt/data/caugusta/pkgs/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1303, in __init__\n    name=self.name)\n  File "/mnt/data/caugusta/pkgs/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 184, in placeholder\n    x = T.TensorType(dtype, broadcast)(name)\n', 'TensorType(float32, matrix) cannot store accurately value [array([ 0.        ,  0.2037037 ,  0.20138889,  0.21100917,  0.62962963,\n        0.6875    ,  0.61206897,  0.44660194,  0.31168831,  0.17391304,\n        0. ...


I would like to look at just one input example, and find the activation and the weights from just that input example. Essentially I'm trying to figure out which features of the data each hidden unit is picking up.

Can anyone explain how to get the activations of intermediate layers in Keras?

Consider this network

model = Sequential()

convout1 = Activation('relu')
convout2 = MaxPooling2D()



Now you can visualize the activation using this function.

def layer_to_visualize(layer):
inputs = [K.learning_phase()] + model.inputs

_convout1_f = K.function(inputs, [layer.output])
def convout1_f(X):
# The [0] is to disable the training phase flag
return _convout1_f([0] + [X])

convolutions = convout1_f(img_to_visualize)
convolutions = np.squeeze(convolutions)

print ('Shape of conv:', convolutions.shape)

n = convolutions.shape[0]
n = int(np.ceil(np.sqrt(n)))

# Visualization of each filter of the layer
fig = plt.figure(figsize=(12,8))
for i in range(len(convolutions)):
ax.imshow(convolutions[i], cmap='gray')

# Specify the layer to want to visualize
layer_to_visualize(convout1)


If you want you can refer my jupyter notebook.

• Must you define the activation separately in order to get it? I mean, do you need model.add(Activation('relu')) or is there a way to get the activation from a regular call to Dense that has the activation built in like this: Dense(15, activation='relu')? Jul 17, 2017 at 17:05
• The answer to the question in my comment is it works fine regardless of how you defined the activation (after the Dense call or within it). Jul 17, 2017 at 23:48

if we suppose that your input image has the shape (1,256,256,3) then this code should work for you.

from keras import backend as K

#function to get activations of a layer
def get_activations(model, layer, X_batch):
get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,])
activations = get_activations([X_batch,0])
return activations

#Get activations using layername
def get_activation_from_layer(model,layer_name,layers,layers_dim,img):
acti = get_activations(model, layers[layer_name], img.reshape(1,256,256,3))[0].reshape(layers_dim[layer][0],layers_dim[layer_name][1],layers_dim[layer_name][2])
return np.sum(acti,axis=2)

#Map layer name with layer index
layers = dict()
index = None
for idx, layer in enumerate(model.layers):
layers[layer.name] = idx

#Map layer name with its dimension
layers_dim = dict()

for layer in model.layers:
layers_dim[layer.name] = layer.get_output_at(0).get_shape().as_list()[1:]