【发布时间】:2022-08-20 01:11:47
【问题描述】:
我想使用张量中的值来创建另一个张量,但出现以下错误:
>>> 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()`
如何使用张量 a 中的值?
-
您可以通过多种方式使用张量。或许你想看看concat
标签: python tensorflow