我已经阅读了这里的文档,并且我理解了大致的想法。我能够可视化中间层的权重。但是,我无法可视化激活。这是我所拥有的:
我训练了我的模型并将权重保存在一个名为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 中激活中间层?