0

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?

raviTeja
  • 338
  • 1
  • 7
  • 21

1 Answers1

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))
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • So in tensorflow inorder to carryout mathematical operation the tensors type should be same. Is my understanding correct? – raviTeja Jun 08 '20 at 08:39
  • In most cases, yes. For exact details, refer to the tensorflow documentation on the functions you use. – Susmit Agrawal Jun 08 '20 at 08:52