8.nn_conv

卷积操作的目的其实就是提取特征

  1. import torch
  2. import torch.nn.functional as F
  3. input = torch.tensor([[1,2,0,3,1],
  4. [0,1,2,3,1],
  5. [1,2,1,0,0],
  6. [5,2,3,1,1],
  7. [2,1,0,1,1]])
  8. kernel = torch.tensor([[1,2,1],
  9. [0,1,0],
  10. [2,1,0]])
  11. print(input.shape)
  12. print(kernel.shape)
  13. #reshape(input: Tensor, shape: _size) -> Tensor
  14. input = torch.reshape(input,(1,1,5,5))
  15. kernel = torch.reshape(kernel,(1,1,3,3))
  16. print(input.shape)
  17. print(kernel.shape)
  18. # torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor
  19. # input – input tensor of shape (minibatch , in_channels , iH , iW)
  20. # weight – filters of shape (out_channels , in_channels/groups , kH , kW)
  21. print('output1------------------------------')
  22. output1 = F.conv2d(input,kernel,stride=1)
  23. print(output1)
  24. print(output1.shape)
  25. print('output2------------------------------')
  26. output2 = F.conv2d(input,kernel,stride=2)
  27. print(output2)
  28. print(output2.shape)
  29. print('output3------------------------------')
  30. output3 = F.conv2d(input,kernel,stride=2,padding=1)
  31. print(output3)
  32. print(output3.shape)

conv2d中的参数:

input需要四个参数(四维),前两个参数是 nimibatch 和 输入通道数

weight需要四个参数(四维),前两个参数是 输出通道数 和 输入通道数/groups(groups默认是1)

输出为:

  1. torch.Size([5, 5])
  2. torch.Size([3, 3])
  3. torch.Size([1, 1, 5, 5])
  4. torch.Size([1, 1, 3, 3])
  5. output1------------------------------
  6. tensor([[[[10, 12, 12],
  7. [18, 16, 16],
  8. [13, 9, 3]]]])
  9. torch.Size([1, 1, 3, 3])
  10. output2------------------------------
  11. tensor([[[[10, 12],
  12. [13, 3]]]])
  13. torch.Size([1, 1, 2, 2])
  14. output3------------------------------
  15. tensor([[[[ 1, 4, 8],
  16. [ 7, 16, 8],
  17. [14, 9, 4]]]])
  18. torch.Size([1, 1, 3, 3])

以output1举例,输入为5×5(四维) ,经过步长为1的3*3卷积核(四维)后 ,输出为4维的3×3