How to create a tensor from another tensor like tf.constant and number?
Question:
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
?
Answers:
Call the .numpy()
method to get the tensor value
tf.constant([a.numpy(), 2])
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)>
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.
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
?
Call the .numpy()
method to get the tensor value
tf.constant([a.numpy(), 2])
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)>
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.