【发布时间】:2021-02-16 21:40:51
【问题描述】:
我正在尝试在 keras 中编写一个自定义损失函数,我需要通过中间层的输出(其形状为 (batch_size, 1))。
我需要的操作只是将 MSE 的每个批次元素加权(乘以)一个因子,即 weight_tensor 中的相应批次元素。
我尝试了以下
def loss_aux_wrapper(weight_tensor):
def loss_aux(y_true, y_pred):
K.print_tensor(weight_tensor, message='weight = ')
_shape = K.shape(y_true)
return K.reshape(K.batch_dot(K.batch_flatten(mse(y_true, y_pred)), weight_tensor, axes=[1,1]),_shape)
return loss_aux
但我明白了
tensorflow.python.framework.errors_impl.InvalidArgumentError: In[0] mismatch In[1] shape: 4096 vs. 1: [32,1,4096] [32,1,1] 0 0 [[node loss/aux_motor_output_loss/MatMul (defined at /code/icub_sensory_enhancement/scripts/models.py:327) ]] [Op:__inference_keras_scratch_graph_6548]
K.print_tensor 没有输出任何东西,我相信是因为它是在编译时调用的?
非常感谢任何建议!
【问题讨论】:
标签: tensorflow keras backend loss eager-execution