Issue
I am trying to use matmul in tensorflow like this-
tf.matmul(y_true, logy)
where
y_true = <class 'numpy.ndarray'> (1000, 1, 1)
logy = <class 'tensorflow.python.framework.ops.EagerTensor'> (1000, 1)
I am getting this error-
<class 'numpy.ndarray'> (1000, 1, 1)
<class 'tensorflow.python.framework.ops.EagerTensor'> (1000, 1)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-39-4cc645f4bb75> in <module>()
25 with tf.GradientTape() as tape:
26 y_predicted = h(x_train,w)
---> 27 costF = loss_function(y_predicted, y)
28
29 gradients = tape.gradient(costF, w)
5 frames
<ipython-input-39-4cc645f4bb75> in loss_function(y_pred, y_true)
18 print(type(y_true), y_true.shape)
19 print(type(logy), logy.shape)
---> 20 p1 = tf.matmul(y_true, logy)
21 p2 = tf.matmul(1-y_true, logyInv)
22 return -tf.reduce_mean(p1 + p2)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
204 """Call target, and fall back on dispatchers if there is a TypeError."""
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, output_type, name)
3606 else:
3607 return gen_math_ops.batch_mat_mul_v2(
-> 3608 a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)
3609
3610 # Neither matmul nor sparse_matmul support adjoint, so we conjugate
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_math_ops.py in batch_mat_mul_v2(x, y, adj_x, adj_y, name)
1507 return _result
1508 except _core._NotOkStatusException as e:
-> 1509 _ops.raise_from_not_ok_status(e, name)
1510 except _core._FallbackException:
1511 pass
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
6939 message = e.message + (" name: " + name if name is not None else "")
6940 # pylint: disable=protected-access
-> 6941 six.raise_from(core._status_to_exception(e.code, message), None)
6942 # pylint: enable=protected-access
6943
/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 anyone please help?
Solution
As @Karl Knechtel already said, you have a mismatch in your shapes. Correct it and see if the error goes away.
If not, it's because you're trying to perform an operation with operands of different types (int64 and double). The message says what's wrong:
InvalidArgumentError: cannot compute BatchMatMulV2 as input #1(zero-based) was expected to be a int64 tensor but is a double tensor [Op:BatchMatMulV2]
.
It says that your first argument is int64 and your second argument is double. Cast either of your arrays:
y_true.astype(np.double)
or
logy = tf.cast(logy, tf.int64)
Answered By - Ladislav Ondris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.