Cannot convert 'auto' to EagerTensor of dtype float

Viewed 2842

I created a dataset with 27 columns. I created an Autoencoder for anomaly detection purposes as below:

input_layer = Input(shape=(27,))

x = layers.Dense(20,activation='relu')(input_layer)
x = layers.Dense(14,activation='relu')(x)
x = layers.Dense(8, activation='relu')(x)
x = layers.Dense(14, activation='relu')(x)
x = layers.Dense(20,activation='relu')(x)

output = layers.Dense(27,activation='relu')(x)

AE = keras.models.Model(inputs=input_layer, outputs=output)

optimizer = keras.optimizers.Adam() 

loss_fn = keras.losses.MeanSquaredError

iterator = iter(train_dataset) 
a = iterator.get_next() 
out = AE(a,training=True)
loss_value = loss_fn(a, out)

Why the following error appears?

TypeError: Cannot convert 'auto' to EagerTensor of dtype float
1 Answers

Disclaimer

This answer assumes that we have initially written:

import tensorflow
from tensorflow import keras

Note that without any example data, it's difficult to be sure this actually works in your case. But I solved a problem yielding the same error by learning about the following.

Solution 1: using the MeanSquaredError class

The issue happens because keras.losses.MeanSquaredError is a class, according to the tensorflow website. Thus, you have to instantiate it first with parenthesis (), not alias it as if it were a function.

Thus, the following line fixes the problem:

loss_fn = keras.losses.MeanSquaredError()

Solution 2: using the MSE function

Another option is to alias keras.losses.MSE, which is an alias of keras.metrics.mean_squared_error, according to this site. If you view the source, you can see that this is a function outside of any class definition.

Thus, the following line fixes the problem:

loss_fn = keras.losses.MSE

Important: the two solutions yield different results

The output of the two above solutions are not the same if the inputs have more than one dimension. The first one yields a scalar, the second one an array. In order for them to be the same, you would have to either:

  • Pass a NONE type reduction when instantiating the class in solution 1. The loss value is now an array.
loss_fn = keras.losses.MeanSquaredError(reduction=keras.losses.Reduction.NONE)

OR

  • Additionally take the mean value from the output of the loss function in solution 2. The loss value is now a scalar.
loss_value = tensorflow.reduce_mean(loss_fn(a, out))
Related