【问题标题】:keras custom loss function call hidden layer dense operationskeras 自定义损失函数调用隐藏层密集操作
【发布时间】:2019-01-01 15:22:57
【问题描述】:

我正在尝试在使用中间层输出的 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' 的类型是: 任何帮助表示赞赏。

【问题讨论】:

标签: python tensorflow keras


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。如何使用权重?在那种情况下,它应该是一种复制密集层操作的方法吗?看看我编辑的问题
  • 你的网络的输入/输出是什么,你希望损失是什么?
  • 输入一个数组,输出另一个数组。损失应该取最后一个 y_pred 的编码器层,然后将其乘以一个因子,然后应用解码器。这个新的 y_pred_fac 将用于计算均方误差损失。所以问题是访问每一层的实际最后一个权重值并使用它们。
  • “访问实际的最后重量值”-据我所知,这在 Keras 损失函数中是不可能的。 “乘以一个因子” - 一个常数? “均方误差损失” - y_pred_fac 和 y_pred 还是 y_true?
  • 不可能吗?这个 w_dec0 = decoder.layers[1].get_weights()[0] 怎么样?在损失函数内部不起作用?
猜你喜欢
  • 2019-01-27
  • 1970-01-01
  • 2018-07-22
  • 2020-12-19
  • 2017-12-18
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
相关资源
最近更新 更多