What does InvalidArgumentError in tensorflow 2 mean?

半世苍凉 提交于 2021-02-11 13:17:39

问题


I am new tensorflow. I am trying to implement Linear Regression with custom training, following this tutorial.

But when I try to compute W*x + b I am getting this error

tf.add(tf.matmul(W,x),b)

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

I initialized W and b

W = tf.Variable(np.random.rand(1,9))

b = tf.Variable([1],dtype = tf.float32)

x = tf.Variable(np.random.rand(9,100))

But when I changed the initialisation of b to

b = tf.Variable(np.random.rand(1))

I did not get any error. What is the reason for this?


回答1:


The result of np.random.rand(1,9) (and other initializations) is of type np.float64. Using this with tf.Variable gives a tensor of type tf.float64.

The parameters to Tensorflow's add must be of the same type. The result of matmul is of type tf.float64 and b is of type tf.float32. You need to cast one to the other's type.

In Tensorflow, you can either do this (recommended, going by convention):

# Can be done in a single line too
matmul_result = tf.matmul(W,x)
matmul_result = tf.cast(matmul_result, tf.float32)
tf.add(matmul_result, b)

Or you can do this:

tf.add(tf.matmul(W,x), tf.cast(b, tf.float64))

You can also directy change the type of numpy's array:

W = tf.Variable(np.random.rand(1,9).astype(np.float32))


来源:https://stackoverflow.com/questions/62244261/what-does-invalidargumenterror-in-tensorflow-2-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!