出错
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [20,1335] vs. [26700]
[[node PCCLoss/sub_1 (defined at D:\ProjectWork\Tensorflow2.0Work
自定义损失函数
class PCCLoss(losses.Loss):
def __init__(self, n):
super(PCCLoss,self).__init__()
self.n = n
def call(self,y_true,y_pred):
# n = y_pred.shape[-1]
sum_xy = tf.math.reduce_sum(y_true * y_pred, axis=1)
sum_x = tf.math.reduce_sum(tf.reshape(tf.math.reduce_sum(y_true, axis=1), (-1,1)), axis=1)
sum_y = tf.math.reduce_sum(tf.reshape(tf.math.reduce_sum(y_pred, axis=1), (-1,1)

这篇博客介绍了在TensorFlow中遇到的自定义损失函数导致的形状不匹配错误,错误源于`reduce_sum`操作的维度设置不当。通过将`axis=1`改为`axis=-1`,确保了在数据流过程中仍按行进行运算,从而解决了问题。博主还分享了修正后的代码,展示了如何正确实现皮尔逊相关系数作为损失函数,并给出了训练模型时的报错信息及修复后的运行结果。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



