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

I am trying this demo:https://github.com/sophchoe/Hybrid-Quantum-Classical-MNIST-Classfication-Model/blob/main/MNIST_Pennylane_Keras.ipynb. But a problem is:

I have tried various data types but have been unable to resolve them.

@Maria_Schuld
@Andre_Tavares
@bventura
@CatalinaAlbornoz
@davidos
@Emil_Zak

Hey @RX1,

Have you tried casting X_train as a float32 tensor? See tf.cast()

I have tried all the data types possible, but the problem still remains

In the code example you provided, you’re casting X_train as a double tensor, whereas Y_train is a float tensor. Can you try casting X_train as a float tensor? I.e. X_train = tf.cast(X_train, dtype=tf.float32).

you can try this demo

It doesn’t work. You can try this demo https://github.com/sophchoe/Hybrid-Quantum-Classical-MNIST-Classfication-Model/blob/main/MNIST_Pennylane_Keras.ipynb

@RX1 can you show me the output of your code when you define X_train as X_train = tf.cast(X_train, dtype=tf.float32)?

I’m facing the same problem

Hey @VQX! It looks like your error message is cut off — I can’t see the entire message! Can you attach the whole thing?

Hi @VQX,

You can try adding the following line to your code at the very beginning of your notebook, just after importing TensorFlow as tf:

tf.keras.backend.set_floatx('float32')

If this doesn’t work then maybe try

tf.keras.backend.set_floatx('float64')

And convert your dtype to float64.

Please let me know if this solves your issue!

If it doesn’t work:

If you follow this tutorial as-is, does it work?

And can you please post the output of qml.about()?

tf.keras.backend.set_floatx('float32')
tf.keras.backend.set_floatx('float64')
I tried both , but didn’t worked
The tutorial does works if I run it as it is!

Here’s the output for qml.about():

Hi @VQX,

I’m not being able to replicate your error. Can you please share your full code, including the data you’re using, to see if I can help you find the problem? My guess is that this is something related to the data types.

Hey @VQX @CatalinaAlbornoz
Have you solved this issue? I’m also facing this exactly same problem. Full code is in this demo Continous-Variable-Quantum-MNIST-Classifiers/2_qumode_classifier.ipynb at main · sophchoe/Continous-Variable-Quantum-MNIST-Classifiers · GitHub

Hi @Nick_Dickens,
Can you please share the output of qml.about() and your full error traceback? I’m running the demo on Xanadu Cloud and it runs well.

tf.keras.backend.set_floatx('float64')

This line of code solved my error

Hi @VQX, I’m glad this solved your problem! Thank you for sharing here what worked for you :slight_smile:

@VQX @CatalinaAlbornoz Thank you all for solving my probem, the method of VQX is valid.

That’s great to hear @Nick_Dickens !