Python Keras自定义目标函数中的Tensorflow占位符
我需要为Keras实现一个自定义目标函数,其中我需要一个额外的tensorflow占位符用于计算。在tensorflow中,我有如下内容:Python Keras自定义目标函数中的Tensorflow占位符,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,我需要为Keras实现一个自定义目标函数,其中我需要一个额外的tensorflow占位符用于计算。在tensorflow中,我有如下内容: pre_cost1 = tf.multiply((self.input_R - self.Decoder) , self.input_mask_R) cost1 = tf.square(self.l2_norm(pre_cost1)) 其中,input\u mask\R是tensorflow占位符。对于Keras损失函数,input_R和Decoder分别
pre_cost1 = tf.multiply((self.input_R - self.Decoder) , self.input_mask_R)
cost1 = tf.square(self.l2_norm(pre_cost1))
其中,input\u mask\R是tensorflow占位符。对于Keras损失函数,input_R和Decoder分别是与y_true和y_pred相对应的占位符。我将Keras损失函数实现为
def custom_objective(y_true, y_pred):
pre_cost1 = tf.multiply((y_true - y_pred))
cost1 = tf.square(l2_norm(pre_cost1))
return cost1
我需要在keras的损失函数中添加输入掩码的附加信息。(它需要是tensorflow占位符,因为它是输入的掩码,每行输入数据的掩码不同)。使用keras后端:
import keras.backend as K
大多数张量函数都存在,例如:
input_mask_R = K.placeholder(shape=(yourshape))
但也许,由于您需要预定义的遮罩,您需要的是:
input_mask_R = K.constant(arrayWithValues, shape=(yourshape))
你也可以用K.multiply
和K.square
进行乘法和平方运算。这样,如果你曾经想过改变后端,一切都会好起来。(我也不确定Keras是否会处理对tensorflow函数的直接调用…)
请参阅文档:培训期间输入掩码是否发生变化?或者它是为每个输入样本预定义的?它是为每个样本预定义的。你能发布模型吗?至少有一个你正在测试的基本的吗?是的,从我的测试中keras handle
tf.square
作为K.square