Keras:可视化中间层的输出

数据挖掘 喀拉斯
2021-10-06 18:22:02

我已经阅读了这里的文档,并且我理解了大致的想法。我能够可视化中间层的权重。但是,我无法可视化激活。这是我所拥有的:

我训练了我的模型并将权重保存在一个名为weights_file.

感谢这个jupyter notebook,我得到了权重的值。首先我定义了我的模型:

def mlp_model(hid_dim=10):

        model = Sequential()
        model.add(Dense(units=hid_dim, input_dim=X.shape[1], activation='relu'))
        model.add(Dense(Y.shape[1], activation='softmax'))
        model.load_weights(weights_file)
        model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

        return model

model_created = mlp_model(hid_dim=15)

为了获得权重,我这样做了:

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)

从那里我可以使用这个创建 Hinton 图但是,当我尝试使用激活时:

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

我收到此错误:

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. ...

我想只看一个输入示例,并从该输入示例中找到激活和权重。本质上,我试图找出每个隐藏单元正在获取的数据的哪些特征。

谁能解释如何在 Keras 中激活中间层?

2个回答

考虑这个网络

model = Sequential()

model.add(Convolution2D(32, 3, 3, input_shape=(1,28,28))) 
convout1 = Activation('relu')
model.add(convout1)
convout2 = MaxPooling2D()
model.add(convout2)

model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))

现在您可以使用此功能可视化激活。

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 = fig.add_subplot(n,n,i+1)
        ax.imshow(convolutions[i], cmap='gray')

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

如果您愿意,可以参考我的jupyter notebook

如果我们假设您的输入图像具有形状,(1,256,256,3)那么此代码应该适合您。

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:]

img1 = utils.load_img("image.png", target_size=(256, 256))

#define the layer you want to visualize
layer_name = "conv2d_22"
plt.imshow(get_activation_from_layer(model,layer_name,layers,layers_dim, img1), cmap="jet")