【问题标题】:Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为该层的 float32 dtype
【发布时间】:2025-11-29 05:30:01
【问题描述】:

我正在阅读 Tensorflow 2.0 谷歌网站 tutorial,在那里他们讨论了 Feature Columns API。在他们讨论数字列的第二部分中,示例代码生成了下面的警告。该警告似乎是关于强制转换一些数据,但该消息并未准确解释如何解决该问题 - 即用户应在何处显式转换数据以避免此警告。:

WARNING:tensorflow:Layer dense_features 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.

If you intended to run this layer in float32, you can safely ignore this 
warning. If in doubt, this warning is likely only an issue if you are porting 
a TensorFlow 1.X model to TensorFlow 2.

To change all layers to have dtype float64 by default, call 
`tf.keras.backend.set_floatx('float64')`. To change just this layer, pass 
dtype='float64' to the layer constructor. If you are the author of this layer, 
you can disable autocasting by passing autocast=False to the base Layer 
constructor.

我正试图弄清楚如何解决这个警告,因为它也出现在我自己的一些代码中。生成此警告的代码是:

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import pandas as pd

import tensorflow as tf

from tensorflow import feature_column
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split

URL = 'https://storage.googleapis.com/applied-dl/heart.csv'
dataframe = pd.read_csv(URL)
train, test = train_test_split(dataframe, test_size=0.2)
train, val = train_test_split(train, test_size=0.2)
print(len(train), 'train examples')
print(len(val), 'validation examples')
print(len(test), 'test examples')

def df_to_dataset(dataframe, shuffle=True, batch_size=32):
  dataframe = dataframe.copy()
  labels = dataframe.pop('target')
  ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
  if shuffle:
    ds = ds.shuffle(buffer_size=len(dataframe))
  ds = ds.batch(batch_size)
  return ds

batch_size = 5 # A small batch sized is used for demonstration purposes
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

# We will use this batch to demonstrate several types of feature columns
example_batch = next(iter(train_ds))[0]

# A utility method to create a feature column
# and to transform a batch of data
def demo(feature_column):
  feature_layer = layers.DenseFeatures(feature_column)
  print(feature_layer(example_batch).numpy())

age = feature_column.numeric_column("age")
demo(age) # <-- SHOULD TRIGGER OR DISPLAY THE WARNING

关于如何解决此问题的任何建议?

【问题讨论】:

  • 似乎警告说需要做什么:要将所有层更改为默认具有dtype float64,请致电tf.keras.backend.set_floatx('float64')。要仅更改此图层,请将 dtype='float64' 传递给图层构造函数。 ?
  • @thushv89 我没有尝试过,但它似乎有点忽略了潜在的问题。所以不知何故,一些值被转换为错误的浮点类型,而这个使用tf.keras.backend.set_floatx() 的修复就像一个包罗万象的处理方法。但是首先如何分配错误的类型。如何手动更改该类型——例如,如果我不使用这个包罗万象的功能,我将在哪里更改该类型。
  • 不改成layers.DenseFeatures(feature_column, dtype='float64')工作吗?
  • 不。那没有用。我仍然得到同样的错误。我实际上尝试了“float64”和“int64”,但在这两种情况下我仍然得到同样的错误——TypeError: Cannot convert 1.0 to EagerTensor of dtype int64。所以似乎错误发生在其他地方或将数据解析到特征列中。
  • @thushv89 感谢您的帮助。很高兴这个问题至少得到了解决:)。

标签: python tensorflow keras


【解决方案1】:

tl;dr 要消除此警告,请将您的输入手动转换为 float32

X = tf.cast(X, tf.float32) 
y = tf.cast(y, tf.float32)

numpy:

X = np.array(X, dtype=np.float32)
y = np.array(y, dtype=np.float32)

说明

默认情况下,Tensorflow 使用floatx,默认为float32,因为它对于深度学习来说绰绰有余。您可以验证这一点:

import tensorflow as tf
tf.keras.backend.floatx()
Out[3]: 'float32'

您提供的输入是 dtype float64,因此 Tensorflow 的权重默认 dtype 与输入不匹配。 Tensorflow 不喜欢这样,因为强制转换(更改 dtype)成本很高。 Tensorflow 在处理不同 dtype 的张量时通常会抛出错误(例如,比较 float32 logits 和 float64 标签)。

它所谈论的新行为

层 my_model_1 将输入张量从 dtype float64 转换为该层的 dtype float32,这是 TensorFlow 2 中的新行为

它是否会自动将输入dtype转换为float32。 Tensorflow 1.X 在这种情况下可能会抛出异常,尽管我不能说我曾经使用过它。

【讨论】:

    最近更新 更多