Python 张量流多重标量乘法
我有一个带有Python 张量流多重标量乘法,python,tensorflow,Python,Tensorflow,我有一个带有[batch\u size,x,y]的三维张量和一个向量[batch\u size] 我想将第I个矩阵[x,y]与给定向量的第I个条目进行标量相乘 Tensorflow中是否有内置函数,或者我是否必须使用tf.while_loop?如果有内置函数,我不知道,但您也不需要使用while循环。您可以执行基本的数组操作。e、 g: a=tf.random_uniform([3,5,8]) b=tf.random_uniform([3]) c=tf.expand_dims(tf.expand
[batch\u size,x,y]
的三维张量和一个向量[batch\u size]
我想将第I个矩阵[x,y]
与给定向量的第I个条目进行标量相乘
Tensorflow中是否有内置函数,或者我是否必须使用
tf.while_loop
?如果有内置函数,我不知道,但您也不需要使用while循环。您可以执行基本的数组操作。e、 g:
a=tf.random_uniform([3,5,8])
b=tf.random_uniform([3])
c=tf.expand_dims(tf.expand_dims(b, -1),1)
c=tf.tile(c,[1,5,8])
d=tf.multiply(a,c)
sess=tf.Session()
sess.run([a,b,c,d])
它应该可以工作。我不知道是否有内置函数,但您也不需要使用while循环。您可以执行基本的数组操作。e、 g:
a=tf.random_uniform([3,5,8])
b=tf.random_uniform([3])
c=tf.expand_dims(tf.expand_dims(b, -1),1)
c=tf.tile(c,[1,5,8])
d=tf.multiply(a,c)
sess=tf.Session()
sess.run([a,b,c,d])
它应该可以工作。你可以通过广播来实现这一点。您需要首先重塑向量的形状
a = tf.constant([[[1,1],[2,2]],[[3,3],[4,4]]])
b = tf.constant([2,3])
c = tf.reshape(b, [-1,1,1])
d = a * c
>>> sess.run(d)
array([[[ 2, 2],
[ 4, 4]],
[[ 9, 9],
[12, 12]]], dtype=int32)
你可以通过广播来做到这一点。您需要首先重塑向量的形状
a = tf.constant([[[1,1],[2,2]],[[3,3],[4,4]]])
b = tf.constant([2,3])
c = tf.reshape(b, [-1,1,1])
d = a * c
>>> sess.run(d)
array([[[ 2, 2],
[ 4, 4]],
[[ 9, 9],
[12, 12]]], dtype=int32)