如何在keras模型的自定义损失函数中使用tensorflow.distributions

时间:2019-05-04 20:52:41

标签: tensorflow keras

对于我用tf2.0 keras定义的深度学习模型,我需要编写一个自定义损失函数。

由于这将取决于诸如熵和普通log_prob之类的东西,因此如果我可以使用tf.distributions.Normal并分别使用两个模型输出分别作为mu和sigma,那确实会使我的生活不那么痛苦。

但是,一旦将其放入损失函数中,我就会得到Keras错误,该函数未定义梯度。

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

当我在某处阅读时,我尝试将调用包含在tf.contrib.eager.Variable中。没有帮助。

使用它们的窍门是什么?从基本架构上我看不出为什么我不能以混合形式使用它们的原因。

#this is just an example which does not really give a meaningful result.
import tensorflow as tf
import tensorflow.keras as K
import numpy as np

def custom_loss_fkt(extra_output):
    def loss(y_true,y_pred):
        dist = tf.distributions.Normal(loc=y_pred,scale=extra_output)
        d = dist.entropy()
        return K.backend.mean(d)
    return loss

input_node = K.layers.Input(shape=(1,))
dense = K.layers.Dense(8,activation='relu')(input_node)
#dense = K.layers.Dense(4,activation='relu')(dense)
out1 = K.layers.Dense(4,activation='linear')(dense)
out2 = K.layers.Dense(4,activation ='linear')(dense)
model = K.Model(inputs = input_node, outputs = [out1,out2])
model.compile(optimizer = 'adam', loss = [custom_loss_fkt(out2),custom_loss_fkt(out1)])
model.summary()
x = np.zeros((1,1))
y1 = np.array([[0.,0.1,0.2,0.3]])
y2 = np.array([[0.1,0.1,0.1,0.1]])
model.fit(x,[y1,y2],epochs=1000,verbose=0)
print(model.predict(x))

0 个答案:

没有答案