一、元素级操作广播机制(对应位置相加)
pytorch维度不同的张量(tensor)相加规则_张量相加
二、矩阵乘法广播机制
import torch
x = torch.randn(2,3,4,5)
print(x)
w = torch.randn(2,1,5,6) # 维度需自右对齐,后两位维度满足为矩阵乘法维度规则,其余的维度要么完全对齐相同,要么维度为1进行广播
print(w)
print(torch.matmul(x,w))
print(torch.matmul(x[0],w[0]))
print(torch.matmul(x[1],w[1]))