【问题标题】:Python Tensorflow - InvalidArgumentError: cannot compute BatchMatMulV2 as inputPython Tensorflow - InvalidArgumentError:无法计算 BatchMatMulV2 作为输入
【发布时间】:2021-09-02 02:47:26
【问题描述】:

我是张量流的新手。我正在这样做-

def loss_function(y_pred, y_true):
    return -tf.reduce_mean(tf.matmul(y_true, tf.math.log(y_pred)) + tf.matmul(1-y_true, tf.math.log(1-y_pred)))

为此,我收到此错误-

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-28-84f43999b37a> in <module>()
     20   with tf.GradientTape() as tape:
     21     y_predicted = h(x_train,w)
---> 22     costF = loss_function(y_predicted, y)
     23 
     24   gradients = tape.gradient(costF, w)

5 frames
/usr/local/lib/python3.7/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: cannot compute BatchMatMulV2 as input #1(zero-based) was expected to be a int64 tensor but is a double tensor [Op:BatchMatMulV2]

有人可以帮忙吗?

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    y_pred和y_true的dtype不同,只是转换之前的类型

    def loss_function(y_pred, y_true):
        y_pred = tf.cast(y_pred, tf.float64)
        y_true = tf.cast(y_true, tf.float64)
        return -tf.reduce_mean(tf.matmul(y_true, tf.math.log(y_pred)) + tf.matmul(1-y_true, tf.math.log(1-y_pred)))
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      相关资源
      最近更新 更多