How to fix MatMul Op has type float64 that does not match type float32 TypeError?
For Tensorflow 2
You can cast one of the tensor, like this for example:
_X = tf.cast(_X, dtype='float64')
Although It's an old question but I would like you include that I came across the same problem. I resolved it using dtype=tf.float64
for parameter initialization and for creating X and Y placeholders as well.
Here is the snap of my code.
X = tf.placeholder(shape=[n_x, None],dtype=tf.float64)
Y = tf.placeholder(shape=[n_y, None],dtype=tf.float64)
and
parameters['W' + str(l)] = tf.get_variable('W' + str(l), [layers_dims[l],layers_dims[l-1]],dtype=tf.float64, initializer = tf.contrib.layers.xavier_initializer(seed = 1))
parameters['b' + str(l)] = tf.get_variable('b' + str(l), [layers_dims[l],1],dtype=tf.float64, initializer = tf.zeros_initializer())
Declaring all placholders and parameters with float64 datatype will resolve this issue.
The tf.matmul()
op does not perform automatic type conversions, so both of its inputs must have the same element type. The error message you are seeing indicates that you have a call to tf.matmul()
where the first argument has type tf.float32
, and the second argument has type tf.float64
. You must convert one of the inputs to match the other, for example using tf.cast(x, tf.float32)
.
Looking at your code, I don't see anywhere that a tf.float64
tensor is explicitly created (the default dtype
for floating-point values in the TensorFlow Python API—e.g. for tf.constant(37.0)
—is tf.float32
). I would guess that the errors are caused by the np.loadtxt(filename)
calls, which might be loading an np.float64
array. You can explicitly change them to load np.float32
arrays (which are converted to tf.float32
tensors) as follows:
initial = np.loadtxt(filename).astype(np.float32)