【问题标题】:Tensorflow data type should be integer instead of float64TensorFlow 数据类型应该是整数而不是 float64
【发布时间】:2020-03-20 15:21:45
【问题描述】:

我正在尝试使用 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 类型。

【问题讨论】:

  • 这是整个错误信息吗?请提供minimal reproducible example
  • @AMC 我已经更新了这个,我相信它现在可以重现了。如果您需要更多信息,请告诉我。

标签: python tensorflow data-science


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    将您的标签转换为整数(从代码中不清楚您的目标列是什么)并且您的代码应该运行。

    【讨论】:

    • 我正在尝试做回归我想我可能使用了错误的实现。
    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多