import torch
from d2l import torch as d2l
X,W_xh = torch.normal(0,1,(3,1)),torch.normal(0,1,(1,4))
H,W_hh = torch.normal(0,1,(3,4)),torch.normal(0,1,(4,4))
torch.matmul(X,W_xh) + torch.matmul(H,W_hh)
tensor([[ 2.7082, -1.9897, -0.5827, 2.6741],
[-4.2047, 2.0132, 1.7765, -0.2202],
[-0.8636, 0.8902, 0.6410, 1.5466]])
现在,我们沿列(轴1)拼接矩阵X和H,沿行(轴0)拼接矩阵W_xh和W_hh
这两个拼接分别产⽣形状(3, 5)和形状(5, 4)的矩阵。再将这两个拼接的矩阵相乘,我们得到与上⾯相同形状(3, 4)的输出矩阵
torch.matmul(torch.cat((X, H), 1), torch.cat((W_xh, W_hh), 0))
tensor([[ 2.7082, -1.9897, -0.5827, 2.6741],
[-4.2047, 2.0132, 1.7765, -0.2202],
[-0.8636, 0.8902, 0.6410, 1.5466]])