我正在尝试使用 Tensorflow 后端在 Keras 中训练深度学习算法。我正在尝试执行以下操作:
x = tf.reshape(theta, [-1])[K.argmax(image)]
Run Code Online (Sandbox Code Playgroud)
其中image是输入,eta是坐标。我试图展平 theta,但出现错误
Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got <tf.Tensor 'loss_42/dense_264_loss/ArgMax:0' shape=(25,) dtype=int64>
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在 Keras 中构建具有三种不同损失函数的深度学习模型。第一个损失函数是典型的均方误差损失。另外两个损失函数是我自己构建的,它发现了输入图像和输出图像的计算之间的差异(这段代码是我正在做的事情的简化版本)。
def p_autoencoder_loss(yTrue,yPred):
def loss(yTrue, y_Pred):
return K.mean(K.square(yTrue - yPred), axis=-1)
def a(image):
return K.mean(K.sin(image))
def b(image):
return K.sqrt(K.cos(image))
a_pred = a(yPred)
a_true = a(yTrue)
b_pred = b(yPred)
b_true = b(yTrue)
empirical_loss = (loss(yTrue, yPred))
a_loss = K.mean(K.square(a_true - a_pred))
b_loss = K.mean(K.square(b_true - b_pred))
final_loss = K.mean(empirical_loss + a_loss + b_loss)
return final_loss
Run Code Online (Sandbox Code Playgroud)
然而,当我用这个损失函数进行训练时,它的收敛性并不好。我想尝试的是分别最小化三个损失函数,而不是将它们添加到一个损失函数中。
我基本上想在这里做第二个选项Tensorflow: Multiple loss functions vs Multiple training ops but in Keras form。我还希望损失函数彼此独立。有没有一种简单的方法可以做到这一点?