【问题标题】:How to multiply one of the dimension of the multi dimension tensor in Tensorflow?如何在Tensorflow中乘以多维张量的一维?
【发布时间】:2019-02-28 05:38:23
【问题描述】:

我有一个如下的张量:

y = tf.placeholder(tf.float32, [None, 3],name="output")

我想乘以最后一个 3 维张量。
我试过这个:

outputs_with_multiplier = y
outputs_with_multiplier[-1] = tf.multiply(outputs_with_multiplier[-1],tf.constant(2.0))

我收到以下错误:

  outputs_with_multiplier[-1] = tf.multiply(outputs_with_multiplier[-1],tf.constant(2.0))
TypeError: 'Tensor' object does not support item assignment

我检查了以下问题以供参考,但没有发现它们有帮助,可能是因为我没有理解它们。
1) Tensorflow - matmul of input matrix with batch data
2)Tensor multiplication in Tensorflow

请帮助我将张量维度相乘,使其工作顺利。

例如,如果这是我的y = [[1,2,3],[2,3,4],[3,4,5],[2,5,7],[8,9,10],[0,3,2]],那么我想成为outputs_with_multiplier = [[1,2,6],[2,3,8],[3,4,10],[2,5,14],[8,9,20],[0,3,4]]

如果有任何解决方案,请告诉我。

【问题讨论】:

  • 有答案吗?请让我知道我可以做些什么来改进它。

标签: python tensorflow


【解决方案1】:

您不能进行项目分配,但可以创建一个新的Tensor。关键是前 2 列乘以 1,第 3 列乘以 2。

x = tf.placeholder(tf.float32, [None, 3], name="output")
y = tf.constant([[1.0, 1.0, 2.0]])
z = tf.multiply(x, y)

sess = tf.Session()
sess.run(z, feed_dict={x: [[1,2,3],[2,3,4],[3,4,5],[2,5,7],[8,9,10],[0,3,2]]})

【讨论】:

  • 它会产生我想要的东西吗?让我检查一下,如果它有效,我会接受它。
  • 当我尝试实现逻辑时得到这个错误:ValueError: Tensor("Const:0", shape=(1, 3), dtype=float32) must be from the same graph as Tensor("prediction:0", shape=(?, 3), dtype=float32).
  • 这一定是什么原因?
  • 请告诉我好吗?
猜你喜欢
  • 2020-08-26
  • 2018-11-01
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
相关资源
最近更新 更多