【问题标题】:TypeError: List of Tensors when single Tensor expected - when using const with tf.random_normalTypeError:预期单个张量时的张量列表 - 将 const 与 tf.random_normal 一起使用时
【发布时间】:2017-05-26 17:02:10
【问题描述】:

我有以下 TensorFlow 代码:

tf.constant(tf.random_normal([time_step, batch_size], -1, 1))

我收到TypeError: List of Tensors when single Tensor expected。你能告诉我代码有什么问题吗?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    有人在another thread 上回答了这个问题。

    基本上,tf.constant() 将 NumPy 数组作为参数或某种数组或只是一个值。

    tf.random_normal() 返回一个张量,它不能成为tf.constant() 的参数。

    要解决此问题,请使用 tf.Variable() 而不是 tf.constant()

    查看链接中的答案。这个人解释得更好。

    【讨论】:

    • 但是如果你需要一个常量而不是一个变量呢?我的意思是你知道优化器修改(学习)了一个变量,你可能不想修改它,这就是我们有常量的原因,因为它不会改变。不能使用常量可能只是个问题,你知道吗?
    【解决方案2】:

    tf.constant 应该有一个常量参数 - value。这里value 可以是一个常量值或dtype 类型的值列表。您不能创建一个以另一个张量为值的常量张量。

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2019-10-29
      • 2021-04-18
      • 2020-08-05
      相关资源
      最近更新 更多