S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

Python Tensorflow - InvalidArgumentError: cannot compute BatchMatMulV2 as input

I am new in tensorflow. I am doing this-

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)))

For this, I am getting this error-

---------------------------------------------------------------------------
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]

Can anybody please help?

Upvotes: 1

Views: 158

Answers (1)

FancyXun
FancyXun

Reputation: 1298

The dtype of y_pred and y_true are different, just convert the type before

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)))

Upvotes: 1

Related Questions