简体   繁体   English

Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为 float32 层的 dtype

[英]Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32

I am reading over the Tensorflow 2.0 google website tutorial , where they discuss the Feature Columns API.我正在阅读 Tensorflow 2.0 google website tutorial ,他们在那里讨论了 Feature Columns API。 In the second in which they discuss numeric columns, the sample code generates the warning below.在他们讨论数字列的第二个中,示例代码生成以下警告。 The warning seems to be about casting some data, but the message does not exactly explain how to fix the problem--that is, where should the user explicitly cast the data so that this warning is avoided.:该警告似乎是关于投射一些数据,但该消息并没有准确解释如何解决问题——也就是说,用户应该在哪里明确地投射数据,以避免出现此警告。:

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.

I am trying to figure out how to fix this warning, because it is showing up on some of my own code too.我想弄清楚如何解决这个警告,因为它也出现在我自己的一些代码中。 The code to generate this warning is:生成此警告的代码是:

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

Any suggestions about how to fix this?有关如何解决此问题的任何建议?

tl;dr to silence this warning, manually cast your input to float32 tl; dr要消除此警告,请手动将您的输入转换为float32

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

or with numpy :或使用numpy

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

Explanation解释

By default, Tensorflow uses floatx , which defaults to float32 , because it's more than enough for deep learning.默认情况下,Tensorflow 使用floatx ,它默认为float32 ,因为它对于深度学习来说已经足够了。 You can verify this:您可以验证这一点:

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

The input you provided, is of dtype float64 , so there is a mismatch between Tensorflow's default dtype for weights, and the input.您提供的输入是 dtype float64 ,因此 Tensorflow 的默认权重 dtype 与输入之间存在不匹配。 Tensorflow doesn't like that, because casting (changing the dtype) is costly. Tensorflow 不喜欢那样,因为转换(更改 dtype)成本很高。 Tensorflow will generally throw an error when manipulating tensors of different dtypes (eg, comparing float32 logits and float64 labels). Tensorflow 在操作不同 dtype 的张量时通常会抛出错误(例如,比较float32 logits 和float64标签)。

The new behavior it's talking about:它正在谈论的新行为

Layer my_model_1 is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2层 my_model_1 将输入张量从 dtype float64 转换为层的 dtype 为 float32,这是 TensorFlow 2 中的新行为

Is that it will automatically cast the input dtype to float32 .是它会自动将输入数据类型转换为float32 Tensorflow 1.X probably threw an exception in this situation, although I can't say I've ever used it.在这种情况下,Tensorflow 1.X 可能会抛出异常,尽管我不能说我曾经使用过它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:tensorflow:Layer my_model 正在将输入张量从 dtype float64 转换为 float32 层的 dtype,这是 TensorFlow 2 中的新行为 - WARNING:tensorflow:Layer my_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2 Tensor转换请求dtype float32的dtype float64 - Tensor conversion requested dtype float64 for Tensor with dtype float32 TensorFlow:将float64张量强制转换为float32 - TensorFlow: cast a float64 tensor to float32 如何将 dtype= complex64 的 3D 张量转换为 tensorflow 中 dtype = float32 的 3D 张量 - How to convert a 3D tensor of dtype= complex64 to 3D tensor of dtype = float32 in tensorflow TF 2.0:如何将 Tensor(&quot;mul_1:0&quot;, shape=(1000, 64), dtype=float32) 转换为 Numpy 数组 - TF 2.0: How to convert Tensor("mul_1:0", shape=(1000, 64), dtype=float32) to Numpy array Tensorflow 图像生成器通过 dtype=string 的张量而不是 dtype=float32 的张量来丢失 function - Tensorflow Image Generator passing Tensor with dtype=string instead of Tensor with dtype=float32 to loss function 使用dtype float32(lambda输入)的Tensor的Tensor转换请求dtype字符串 - Tensor conversion requested dtype string for Tensor with dtype float32 (lambda input) TypeError:添加的层必须是 class 层的实例。 找到:Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python - TypeError: The added layer must be an instance of class Layer. Found: Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python python - ValueError: Tensor Tensor(&quot;dense_2/Softmax:0&quot;, shape=(?, 43), dtype=float32) 不是这个图的一个元素 - python - ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 43), dtype=float32) is not an element of this graph 图断开连接:无法在“input_1”层获取张量 Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) at layer "input_1"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM