2

I want to use the value in a tensor to create another tensor, but I got the following error:

>>> a = tf.constant(3)
>>> a
Out[51]: <tf.Tensor: shape=(), dtype=int32, numpy=3>
>>> tf.constant([a, 2])
Traceback (most recent call last):
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3369, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-53-7af9a5175a59>", line 1, in <cell line: 1>
    tf.constant([a, 2])
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 267, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 279, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 304, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 102, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: TypeError: Scalar tensor has no `len()`
Traceback (most recent call last):
  File "/Users/belter/miniconda3/envs/deside_obj/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1170, in __len__
    raise TypeError("Scalar tensor has no `len()`")
TypeError: Scalar tensor has no `len()`

How can I use the value in tensor a?

1
  • 2
    You can use tensors in a lot of ways. Perhaps you want to look at concat Commented Jul 5, 2022 at 11:57

3 Answers 3

4

I don't think tf.stack is the best option for this simple operation, and indeed we want to avoid tf.Tensor.numpy() to retain compatibility with graph mode.

Instead, you can use tf.convert_to_tensor, which, citing the documentation, accepts Tensor objects, numpy arrays, Python lists, and Python scalars, so most things you would ever want to throw at it:

In [1]: import tensorflow as tf
In [2]: a = tf.constant(3)
In [3]: tf.convert_to_tensor([a, 2])
Out[3]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 2], dtype=int32)>
    

This also works in graph mode. Adapting the demonstration from another answer:

In [4]: @tf.function
   ...: def join_tns_num(tensor, num):
   ...:     return tf.convert_to_tensor([tensor, tf.constant(num)])
   ...: 
In [5]: join_tns_num(a, 42)
Out[5]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([ 3, 42], dtype=int32)>

So, if you encounter ValueError: TypeError: Scalar tensor has no `len()`, check whether replacing tf.constant by tf.convert_to_tensor is the answer.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use tf.stack.

import tensorflow as tf

@tf.function
def join_tns_num(tensor, num):
    return tf.stack([tensor, tf.constant(num)], axis=0)

Check function:

>>> join_tns_num(tf.constant(3), 2)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 2], dtype=int32)>

Comments

0

Call the .numpy() method to get the tensor value


tf.constant([a.numpy(), 2])

2 Comments

What if I need to use it in a tf.function?
In a tf.function you dont have to get the value of the tensor, instead if you want to do some operation that involving a tensor and a non tensor, let's say in your case when you wanna concat a tf.constant and an integer to make a tf.constant , you have to cast the int variable first to tf.constant and then pass both to tf.concat() . So inside of your tf.function it will look like this return tf.concat([a, tf.constant([2])], 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.