Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.
like image 954
Integration Avatar asked Oct 17 '22 14:10

Integration


1 Answers

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

like image 143
Xiangyu Avatar answered Oct 21 '22 01:10

Xiangyu