keras + TF自定义损失函数中的逐元素乘法

时间:2020-08-19 16:45:18

标签: tensorflow keras loss-function

我正在尝试使用Keras + TensorFlow编写自定义损失函数。尝试添加按元素的乘法运算时遇到错误:

def new_loss(y_true, y_pred):

    pi = np.pi

    d = pi/4 + (pi/4) * (y_pred - y_true)

    a = tf.keras.layers.Multiply()([K.sin(d), -K.log(K.cos(d))])

显然这是错误的,并且我遇到了错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  You must feed a value for placeholder tensor 'dense_2_target' with dtype float and shape [?,?]
     [[node dense_2_target (defined at Q:\PycharmProjects\loss_function\custom_loss.py:25) ]] [Op:__inference_keras_scratch_graph_164]

PS:由于我在Tensors上进行操作,是否真的需要在TensorFlow中编写这些操作?我可以将'y_true'和'y_pred'转换为Numpy数组吗?该怎么做?

0 个答案:

没有答案