考虑我有一组向量A
,我想将A
中的每个向量与张量T
相乘,最终得到一个张量y
,其中每个切片矩阵是v
中A
与T
之间的向量X
相乘的结果:
如果tf.reduce_sum(tf.expand_dims(v,2)*T,1)
只包含一个向量,则以下代码有效(感谢matmul function for vector with tensor multiplication in tensorflow中的答案):
X
但如果A
由几个向量组成,乘法代码怎么样?
例如,我对2
(T
向量)和A = tf.constant([1,2,3,4], shape=[2,2], dtype=tf.float32) #v1 =[1,2], v2=[3,4]
T = tf.constant([1,2,3,4,5,6,7,8], shape=[2,2,2], dtype=tf.float32)
具有以下值:
A
我想通过将T
乘以[[[ 7. 10.]
[19. 22.]]
[[15. 22.]
[43. 50.]]]
得到以下输出:
y = AX +b
这个问题的应用是线性回归X
中的批量梯度下降,其中我有一组向量但不是训练权重矩阵T
,我想训练一个张量y
,因此输出T
将是一个张量,其中张量中的每个矩阵都是输入向量乘以v
的输出。
请注意,通常,当我们将维度1*n
的向量T
与维度m*n*k
的张量m*k/m*1*k
相乘时,我们希望得到矩阵/张量维度m
。这意味着我们的张量具有n*k
个具有维度v
的矩阵,并且en
乘以每个矩阵,并且所得到的向量堆叠在一起。
答案 0 :(得分:1)
尝试tf.tensordot
:
dpkg: rvm: dependency problems, but removing anyway as you requested:
ruby-full depends on ruby; however:
Package ruby is not configured yet.
Package rvm which provides ruby is to be removed.
rake depends on ruby | ruby-interpreter; however:
Package ruby is not configured yet.
Package rvm which provides ruby is to be removed.
Package ruby-interpreter is not installed.
ruby-test-unit depends on ruby | ruby-interpreter; however:
Package ruby is not configured yet.
Package rvm which provides ruby is to be removed.
Package ruby-interpreter is not installed.
(Reading database ... 344123 files and directories currently installed.)
Removing rvm (1.29.3-1) ...
groupdel: cannot remove the primary group of user 'josh'
dpkg: error processing package rvm (--remove):
subprocess installed post-removal script returned error exit status 8
Errors were encountered while processing:
rvm
E: Sub-process /usr/bin/dpkg returned an error code (1)
或者,
tf.tensordot(A, T, axes=[-1, -2])
tf.squeeze(tf.tensordot(A[:, None], T, axes=[2, 1]))
这会给出
eval