【发布时间】: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' 的类型是: 任何帮助表示赞赏。
【问题讨论】:
-
这里有一个简单的解决方案,可以将中间层放入损失计算中:stackoverflow.com/questions/62454500/…
标签: python tensorflow keras