1
votes

The following lines work:

t1 = tf.constant([-2.0, -1.0, 0, 1.0, 2.0], dtype=tf.complex128)
tf.sqrt(t1)
print(t1)

Now if I use

t1 = tf.range(-2.0, 3.0, 1, dtype=tf.complex128)
tf.sqrt(t1)
print(t1)

I get the usual error :

Node:{{node Range}} All kernels registered for op Range :
device='CPU'; Tidx in [DT_FLOAT] device='CPU'; Tidx in [DT_DOUBLE]
device='CPU'; Tidx in [DT_INT32] device='CPU'; Tidx in [DT_INT64]
device='GPU'; Tidx in [DT_FLOAT] device='GPU'; Tidx in [DT_DOUBLE]
device='GPU'; Tidx in [DT_INT32] device='GPU'; Tidx in [DT_INT64]
device='XLA_CPU_JIT'; Tidx in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_BFLOAT16, DT_HALF] device='XLA_GPU_JIT'; Tidx in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_BFLOAT16, DT_HALF]
device='XLA_CPU'; Tidx in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_BFLOAT16, DT_HALF] device='XLA_GPU'; Tidx in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_BFLOAT16, DT_HALF] [Op:Range]

What am I doing wrong?

1

1 Answers

1
votes

According to this error, it is saying that input to the tf.range should be one of double, float, int are registered for op Range on device: CPU and GPU. But your input is complex128, hence it return NotFoundError.

If we haven't specified any dtype (i.e. dtype is None), it inferred dtype from dtype_hierarchy: dtypes.int32, dtypes.int64, dtypes.float32, dtypes.float64 as per as per arg.dtype.

Workaround to create complex128 tensor using tf.range is as below with the help of tf.cast

t1 = tf.range(-2.0, 3.0, 1)
# casts tensor to a complex128 dtype.
t2 = tf.cast(t1, dtype=tf.complex128)
# apply sqrt function
tf.sqrt(t2)
# print output
print(t1)
print(t2)

Output:

tf.Tensor([-2. -1.  0.  1.  2.], shape=(5,), dtype=float32)
tf.Tensor([-2.+0.j -1.+0.j  0.+0.j  1.+0.j  2.+0.j], shape=(5,), dtype=complex128)