I got TypeError: Expected float32, but got auto of type 'str'. error while fitting the sequential model. I checked my inputs both are numpy.ndarray.
type(xtrain),type(ytrain)
(numpy.ndarray, numpy.ndarray)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape = (28,28)))
model.add(tf.keras.layers.Dense(32,activation='relu'))
model.add(tf.keras.layers.Dense(32,activation='relu'))
model.add(tf.keras.layers.Dense(10,activation=tf.keras.activations.softmax))
model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy,optimizer =
tf.keras.optimizers.Adam(learning_rate=.0001),metrics = ['accuracy'])
model.fit(x =xtrain,y = ytrain,epochs=100)
Epoch 1/100
TypeError Traceback (most recent call last) in () ----> 1 model.fit(x =xtrain,y = ytrain,epochs=100)
1 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1127 except Exception as e: # pylint:disable=broad-except 1128 if hasattr(e, "ag_error_metadata"): -> 1129 raise e.ag_error_metadata.to_exception(e) 1130 else: 1131 raise
TypeError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 752, in __init__ **
from_logits=from_logits)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 227, in __init__
super().__init__(reduction=reduction, name=name)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 88, in __init__
losses_utils.ReductionV2.validate(reduction)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/losses_utils.py", line 82, in validate
if key not in cls.all():
TypeError: Expected float32, but got auto of type 'str'.
CodePudding user response:
The error may be in this part of the code:
model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy,optimizer = tf.keras.optimizers.Adam(learning_rate=.0001),metrics = ['accuracy'])
Try changing the loss parameter from tf.keras.losses.SparseCategoricalCrossentropy
to tf.keras.losses.SparseCategoricalCrossentropy()
.
For some clarity, the difference between the two is that with tf.keras.losses.SparseCategoricalCrossentropy
you are not passing and instance of the class, with tf.keras.losses.SparseCategoricalCrossentropy()
you are.
CodePudding user response:
IIUC, your xtrain
or ytrain
is not float
you need to convert them:
(use copy=False for converting as in-place
and without copy array and don't use more memory and convert them):
xtrain = xtrain.astype('float32', copy=False)
ytrain = ytrain.astype('float32', copy=False)