卷积层和池化层的简单实现

Malina ·
更新时间:2024-11-13
· 634 次阅读

卷积层的简单实现 X = torch.rand(4, 2, 3, 5) print(X.shape) conv2d = nn.Conv2d(in_channels=2, out_channels=3, kernel_size=(3, 5), stride=1, padding=(1, 2)) Y = conv2d(X) print('Y.shape: ', Y.shape) print('weight.shape: ', conv2d.weight.shape) print('bias.shape: ', conv2d.bias.shape) torch.Size([4, 2, 3, 5]) Y.shape: torch.Size([4, 3, 3, 5]) weight.shape: torch.Size([3, 2, 3, 5]) bias.shape: torch.Size([3]) 池化层的简单实现 X = torch.arange(32, dtype=torch.float32).view(1, 2, 4, 4) pool2d = nn.MaxPool2d(kernel_size=3, padding=1, stride=(2, 1)) Y = pool2d(X) print(X) print(Y) tensor([[[[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [12., 13., 14., 15.]], [[16., 17., 18., 19.], [20., 21., 22., 23.], [24., 25., 26., 27.], [28., 29., 30., 31.]]]]) tensor([[[[ 5., 6., 7., 7.], [13., 14., 15., 15.]], [[21., 22., 23., 23.], [29., 30., 31., 31.]]]])
作者:Siro阿希



池化 卷积

需要 登录 后方可回复, 如果你还没有账号请 注册新账号