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中“a”的形状为 (7, 4, 15),“b”的形状为 (7, 4, 5)。“c”,定义为“a”和“b”的点积:
c = np.einsum('ijk,ijl->ilk',a,b)
我正在寻找此示例的 theano 实现来计算“c”。
有任何想法吗?