如何使用一个张量的元素沿另一个张量的元素应用标量乘法?

时间:2017-04-04 21:43:52

标签: python tensorflow theano theano.scan

给定两个张量A(mxnxq)和B(mxnx 1),如何创建一个循环遍历A行的函数,将B(nx 1)的每个元素视为标量并将它们应用于A(nxq)的子矩阵的向量(qx 1)?

例如,A是(6000,1000,300)形状。 B是(6000,1000,1)。循环通过A的6000个“切片”,对于A(,1000,300)的1000个子矩阵的每个向量,应用来自B(,1000,1)的子矩阵的向量的每个元素的标量乘法。 / p>

我的措辞可能非常糟糕。当问题出现时,我会相应地调整措辞。

旁注:我正在使用Python,所以Theano可能是最好的吗?

1 个答案:

答案 0 :(得分:1)

使用tf.mul,如下所示:

import tensorflow as tf
a = tf.constant([[[1,2,1,2],[3,4,1,2],[5,6,10,12]],[[7,8,1,2],[9,10,1,1],[11,12,0,3]]])
b= tf.constant([[[7],[8],[9]],[[1],[2],[3]]])
res=tf.mul(a,b)
sess=tf.Session()
print(sess.run(res)) 

打印:

[[[  7  14   7  14]
  [ 24  32   8  16]
  [ 45  54  90 108]]

 [[  7   8   1   2]
  [ 18  20   2   2]
  [ 33  36   0   9]]]
相关问题