keras自定义损失函数调用隐藏层密集操作

时间:2018-07-24 22:06:50

标签: python tensorflow keras

我正在尝试在keras中定义一个自定义损失函数,该函数使用中间层输出,然后对其进行处理(例如,乘以2(然后再返回模型以产生最终输出。因此假设模型

input_dim = X_train.shape[1]
encoding_dim = 14


#encoder
input_tensor = Input(shape=(input_dim, ))

encoderOut = Dense(encoding_dim, activation="tanh", 
                activity_regularizer=regularizers.l1(10e-5))(input_tensor)
encoderOut = Dense(int(encoding_dim / 2), activation="relu")(encoderOut)

encoder = Model(input_tensor, encoderOut)


#decoder
decoder_input = Input(shape=(int(encoding_dim / 2),))
decoderOut = Dense(int(encoding_dim / 2), activation='tanh',name='decoder_input')(decoder_input)
decoderOut = Dense(input_dim, activation='relu',name='decoder_output')(decoderOut)

decoder = Model(decoder_input, decoderOut)

#autoencoder
autoInput = Input(shape=(input_dim, ))
encoderOut = encoder(autoInput)
decoderOut = decoder(encoderOut)
autoencoder = Model(inputs=autoInput, outputs=decoderOut)

我的损失函数是

def L2Loss(y_true,y_pred):
    get_layer_output_enc = K.function([encoder.layers[0].input, K.learning_phase()], [encoder.layers[2].output])
    out= get_layer_output_enc([y_true])[0]*10

不幸的是,当我运行它时,我得到了:

    517             None, None,
    518             compat.as_text(c_api.TF_Message(self.status.status)),
--> 519             c_api.TF_GetCode(self.status.status))
    520     # Delete the underlying status object from memory otherwise it stays alive
    521     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: You must feed a value for placeholder tensor 'model_89_target_28' with dtype float and shape [?,?]
     [[Node: model_89_target_28 = Placeholder[dtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

或者,我尝试重现密集层操作以提取权重:

    layer_output_enc = encoder.layers[2].output#get_layer_output_enc([y_true])[0]*10
    w_dec0 = decoder.layers[1].get_weights()[0]
    b_dec0 = decoder.layers[1].get_weights()[1]
    print type(layer_output_enc),'--',layer_output_enc.shape
    layer_output_enc = backend.cast(layer_output_enc,'float64')#tf.convert_to_tensor(layer_output_enc)
    out_dec0 = K.dot(layer_output_enc,w_dec0)+b_dec0
    print out_dec0.shape
    out2 = K.tanh(out_dec0)

但是我又得到了错误:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

这很奇怪,因为我现在的“ layer_output_enc”是类型: 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

您不能在Keras模型的损失函数中调用模型,只能使用输入张量y_truey_pred。因此,损失函数无法访问中间层。我有相同的需求,发现的棘手解决方案是将输出张量与中间层连接起来,作为模型的新输出。直接使用tensorflow可能要简单得多。