Tensorflow float64 error while running in eager execution

60 views Asked by At

I'm using TF 2.13.0 and I'm getting an error only when eager execution is enabled. Is there a workaround?

The error is

tensorflow.python.framework.errors_impl.InvalidArgumentError: TensorArray dtype is float64 but Op is trying to write dtype float32

The code is

import tensorflow as tf

# when the next line is uncommented, we get an error
tf.config.run_functions_eagerly(True)

@tf.function(input_signature=[tf.TensorSpec(shape=None,dtype=tf.float64)])
def TrySine(dev):
    mytensor = tf.map_fn(fn=lambda t,dev=dev: tf.math.sin(dev*3.14/180.0), elems=tf.ones(shape=(8,),dtype='float64'))
    return mytensor


output = TrySine(dev=5.0)

print(output)


1

There are 1 answers

3
Nicolas Gervais - Open to Work On BEST ANSWER

Generally speaking, tensorflow doesn't like calculations with different dtypes. It will typically either throw warnings or errors.

Unless you have a solid reason for using float64, I suggest you stick to float32, as is the standard in deep learning.

import tensorflow as tf

# when the next line is uncommented, we get an error
tf.config.run_functions_eagerly(True)


@tf.function(input_signature=[tf.TensorSpec(shape=None,dtype=tf.float32)])
def TrySine(dev):
    mytensor = tf.map_fn(fn=lambda t,dev=dev: tf.math.sin(dev*3.14/180.0), elems=tf.ones(shape=(8,),dtype='float32'))
    return mytensor


output = TrySine(dev=5.0)

print(output)

But if you really want to use float64, you can make sure your input constant is in this dtype:

@tf.function(input_signature=[tf.TensorSpec(shape=None,dtype=tf.float64)])
def TrySine(dev):
    mytensor = tf.map_fn(fn=lambda t,dev=dev: tf.math.sin(dev*3.14/180.0), elems=tf.ones(shape=(8,),dtype='float64'))
    return mytensor


output = TrySine(dev=tf.constant(0.5, dtype=tf.float64))

print(output)