Skip to content Skip to sidebar Skip to footer

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. In the second in which they discuss numeric columns, the sample code gener

Solution 1:

tl;dr to silence this warning, manually cast your input to float32

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

or with 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. 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. Tensorflow doesn't like that, because casting (changing the dtype) is costly. Tensorflow will generally throw an error when manipulating tensors of different dtypes (e.g., comparing float32 logits and float64 labels).

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

Is that it will automatically cast the input dtype to float32. Tensorflow 1.X probably threw an exception in this situation, although I can't say I've ever used it.

Post a Comment for "Tensorflow 2.0 Warnings - Dense_features Is Casting An Input Tensor From Dtype Float64 To The Layer's Dtype Of Float32"