【问题标题】:How to create a tensor from another tensor like tf.constant and number?如何从另一个张量(如 tf.constant 和 number)创建一个张量?
【发布时间】: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


【解决方案1】:

您可以使用tf.stack

import tensorflow as tf

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

检查功能:

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

【讨论】:

    【解决方案2】:

    调用.numpy()方法获取张量值

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

    【讨论】:

    • 如果我需要在tf.function 中使用它怎么办?
    • tf.function 中,您不必获取张量的值,相反,如果您想做一些涉及张量和非张量的操作,假设您想将tf.constant 和一个整数连接到制作一个 tf.constant ,您必须先将 int 变量转换为 tf.constant ,然后将两者都传递给 tf.concat() 。所以在你的tf.function 里面它看起来像这样return tf.concat([a, tf.constant([2])], 0)
    【解决方案3】:

    我不认为tf.stack 是这种简单操作的最佳选择,实际上我们希望避免tf.Tensor.numpy() 以保持与图形模式的兼容性。

    相反,您可以使用tf.convert_to_tensor,引用文档,接受张量对象、numpy 数组、Python 列表和 Python 标量,所以大多数你想扔给它的东西:

    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)>
        
    

    这也适用于图形模式。从另一个答案改编演示:

    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)>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多