Tensorflow数据类型应为整数而不是float64

时间:2020-03-20 15:21:45

标签: python tensorflow data-science

我正在尝试使用Tensorflows线性分类器估计器预测未来的平均温度。目标特征为float64类型,用于预测平均温度的其他特征也为float64。

下面是我用来将要素转换为tf数字列的方法。

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

我创建了如图所示的线性估计器

    feature_columns=feature_columns, n_classes=4)

在训练模型时,出现以下错误。

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'>.

我正在努力理解为什么要考虑这一点,我明确表示我希望所有功能都为float64类型。

2 个答案:

答案 0 :(得分:2)

这是一个简单的错误,我使用了tf.estimator.LinearClassifier,实际上我应该使用tf.estimator.LinearRegressor,因为这是我试图预测的浮点值。

解决方案是更改tf估计器类型,并从模型创建中删除n_classes

答案 1 :(得分:0)

该错误告诉您不能将浮点数用作输出类。这是完全公平的,因为您要进行分类而不是回归。

将标签转换为整数(代码中不清楚目标列是什么),然后代码应运行。