Tensorflow data type should be integer instead of float64

Viewed 986

I am trying to predict a future mean temperature using Tensorflows linear classifier estimator. The target feature is of float64 type and the other features that are being used to predict the mean temperature are also float64.

Below is the method I am using to convert the features into tf numerical columns.

for feature_name in NUMERIC_COLUMNS:
    feature_columns.append(tf.feature_column.numeric_column(
        feature_name, dtype=tf.float64))

I create the linear estimator as shown

    feature_columns=feature_columns, n_classes=4)

When it comes to training the model I get the following error.

WARNING:tensorflow:Layer linear/linear_model is casting an input tensor from
dtype float64 to the layer's dtype of float32,
which is new behavior in TensorFlow 2.  The layer has dtype float32 
because it's dtype defaults to floatx.

Exception has occurred: ValueError
Labels dtype should be integer. Instead got <dtype: 'float64'>.

I am struggling to understand why this would be considering I explicitly stated I wanted all the features to be of type float64.

2 Answers

It was a simple mistake, I was using tf.estimator.LinearClassifier where in fact I should have been using tf.estimator.LinearRegressor as it was a float value I was trying to predict.

Solution was to change the tf estimator type and also remove n_classes from the model creation.

The error tells you that you can't use as an output class a float number. Which is completely fair cause you're doing classification and not regression.

Convert your labels into integers (it is not clear from the code what your target column is) and your code should run.

Related