Skip to content
Advertisement

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

Can somebody explain, how does TensorFlow’s eager mode work? I am trying to build a simple regression as follows:

JavaScript

Gradient output: [None, None, None, None, None, None] The error is following:

JavaScript

Edit

I updated my code. Now, the problem comes in gradients calculation, it is returning zero. I have checked the loss value that is non-zero.

Advertisement

Answer

Part 1: The problem is indeed the datatype of your input. By default your keras model expects float32 but you are passing a float64. You can either change the dtype of the model or change the input to float32.

To change your model:

JavaScript

To change your input: y = y.astype('float32')

Part 2: You need to call the function that computes your model (i.e. model(data)) under tf.GradientTape(). For example, you can replace your compute_loss method with the following:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement