对不起,我不知道你说的“令人困惑”是什么意思?
我尝试过使用小案例,希望它能代表您的情况。在三维张量和二维矩阵之间使用theano.tensor.dot的点积:
import numpy as np
import theano
import theano.tensor as T
a = T.tensor3('a', dtype='int64')
c = T.matrix('c',dtype='int64')
d = T.dot(a,c)
g = theano.function([a,c],d)
x = np.array([[[1,2],[1,3]],[[2,2],[1,1]]], dtype=int)
y = np.array([[1,2],[1,3]], dtype=int)
print g(x,y)
输出:
[[[ 3 8]
[ 4 11]]
[[ 4 10]
[ 2 5]]]
它像你的逻辑一样工作,矩阵c 只在第二维和第三维做点积。
更新
上面的第一个代码,您可以在您的情况下用于第一个操作 (A*W)。对不起,我没有仔细计算,当然在那个操作之后输出变成了三维张量。因此,要执行 (AW)*B,您必须使用不同的方法。为了在两个三维张量之间执行乘法,我通常使用扫描:
import numpy as np
import theano
import theano.tensor as T
a = T.tensor3('a', dtype='int64')
c = T.tensor3('c',dtype='int64')
d, b = theano.scan(lambda i: T.dot(a[i,:], c[i,:]),sequences=T.arange(2))
g = theano.function([a,c],d)
x = np.array([[[1,2],[1,3]],[[2,2],[1,1]]], dtype=int)
y = np.array([[[1,2],[1,3]],[[2,2],[1,1]]], dtype=int)
print g(x,y)
但是我知道还有另一种使用theano.tensor.batched_dot 的方法(theano.tensor.dot 我认为只适用于二维和一维数组)。在您的情况下,这样编写代码很简单:
e = T.batched_dot(a,c)
g = theano.function([a,c],e)
上面的代码给出了相同的结果。希望对您有所帮助。