Integration
Integration

Reputation: 347

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'

Tensorflow 1.10 on Google Colab (python 2.7) or my local system (python 3.6) Using sample code from https://www.tensorflow.org/guide/keras Code is

import numpy as np
import tensorflow as tf
from tensorflow import keras

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))
dataset1 = tf.data.Dataset.from_tensor_slices((data, labels))
dataset1 = dataset1.batch(32)
dataset1 = dataset1.repeat()

model = keras.Sequential()
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(dataset1, epochs=10, steps_per_epoch=30)

Throws the following error:

    Error TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.

packages/tensorflow/python/framework/op_def_library.pyc in _apply_op_helper(self, op_type_name, name, **keywords)
    544                   "%s type %s of argument '%s'." %
    545                   (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name,
--> 546                    inferred_from[input_arg.type_attr]))
    547 
    548           types = [values.dtype]

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.

Upvotes: 6

Views: 16616

Answers (2)

Xiangyu
Xiangyu

Reputation: 844

I ran into the same problem and I assume the default data type used in the model is float32 while that of numpy is float64, and from_tensor_slices retains that type. To fix it, just change your code:

data = np.random.random((1000,32))
labels = np.random.random((1000,10))

to

data = np.random.random((1000,32)).astype(np.float32)
labels = np.random.random((1000,10)).astype(np.float32)

But I do think as a piece of sample code in its tutorial, tensorflow should make sure it runs.

Update: There is a closed issue related to this: https://github.com/tensorflow/tensorflow/issues/22207

Upvotes: 4

Tony Zheng
Tony Zheng

Reputation: 11

I met a similar problem. I use this code snippet.

model.compile(optimizer=opt,
          loss=keras.losses.categorical_crossentropy)

I guess the loss function in Keras only requires 'float' type (I didn't check the source code). To solve this problem, I add one more Layer at the end of the output layer.

decoder_outputs = Lambda(lambda x: K.cast(x, 'float32'), name='change_to_float')(decoder_outputs)

Hope this will help.

Upvotes: 1

Related Questions