两个 4D mat mul numpy 并且应该期望输出 5D

Two 4D mat mul numpy and should expect output 5D

我想用 3 个过滤器将注意力权重(5 个标签)应用到我的卷积中,可以帮助我如何应用 matmul。如果您也提供 tensorflow 版本,我们将不胜感激。

import numpy as np
conv = np.random.randint(10,size=[1,3,2,2], dtype=int) # [batches,filter,row,col]
attention = np.random.randint(5,size=[1,5,2,1], dtype=int) # [batches,label,row,col]
np.matmul(conv,attention).shape # expected output size [1,3,5,2,1] [batches,filter,label,row,col]

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (1,3,2,2)->(1,3,2,newaxis,2) (1,5,2,1)->(1,5,newaxis,1,2) 

根据 matmul 的文档:

If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.

Stacks of matrices are broadcast together as if the matrices were elements.

这意味着在您的情况下,除了最后两个维度之外的所有维度都需要匹配。如果您希望输出形状为 1, 3, 5, 2, 1,则需要在每个数组中显式插入一个空轴。您可以在创建时执行此操作:

import numpy as np
conv = np.random.randint(10, size=[1, 3, 1, 2, 2], dtype=int)
attention = np.random.randint(5, size=[1, 1, 5,2,1], dtype=int)
np.matmul(conv,attention).shape

或者,您可以通过将视图与适当的插入相乘来明确插入:

np.matmul(conv[:, :, np.newaxis, ...], attention[:, np.newaxis, ...]).shape