|
| 1 | +## 19-卷积层 |
| 2 | + |
| 3 | +#### 本讲文字介绍部分请参考沐神在线书籍~:https://zh-v2.d2l.ai/chapter_convolutional-neural-networks/why-conv.html |
| 4 | + |
| 5 | +#### 代码 |
| 6 | +```python |
| 7 | +import torch |
| 8 | +from torch import nn |
| 9 | + |
| 10 | +def corr2d(X,K): #X为输入,K为核矩阵 |
| 11 | + h,w=K.shape #h得到K的行数,w得到K的列数 |
| 12 | + Y=torch.zeros((X.shape[0]-h+1,X.shape[1]-w+1)) #用0初始化输出矩阵Y |
| 13 | + for i in range(Y.shape[0]): #卷积运算 |
| 14 | + for j in range(Y.shape[1]): |
| 15 | + Y[i,j]=(X[i:i+h,j:j+w]*K).sum() |
| 16 | + return Y |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +```python |
| 21 | +#样例点测试 |
| 22 | +X=torch.tensor([[0,1,2],[3,4,5],[6,7,8]]) |
| 23 | +K=torch.tensor([[0,1],[2,3]]) |
| 24 | +corr2d(X,K) |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + tensor([[19., 25.], |
| 31 | + [37., 43.]]) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +```python |
| 37 | +#实现二维卷积层 |
| 38 | +class Conv2d(nn.Module): |
| 39 | + def _init_(self,kernel_size): |
| 40 | + super()._init_() |
| 41 | + self.weight=nn.Parameter(torch.rand(kerner_size)) |
| 42 | + self.bias=nn.Parameter(torch.zeros(1)) |
| 43 | + def forward(self,x): |
| 44 | + return corr2d(x,self.weight)+self.bias |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +```python |
| 49 | +X=torch.ones((6,8)) |
| 50 | +X[:,2:6]=0 |
| 51 | +X |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + tensor([[1., 1., 0., 0., 0., 0., 1., 1.], |
| 58 | + [1., 1., 0., 0., 0., 0., 1., 1.], |
| 59 | + [1., 1., 0., 0., 0., 0., 1., 1.], |
| 60 | + [1., 1., 0., 0., 0., 0., 1., 1.], |
| 61 | + [1., 1., 0., 0., 0., 0., 1., 1.], |
| 62 | + [1., 1., 0., 0., 0., 0., 1., 1.]]) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +```python |
| 68 | +K=torch.tensor([[-1,1]]) #这个K只能检测垂直边缘 |
| 69 | +Y=corr2d(X,K) |
| 70 | +Y |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + tensor([[ 0., -1., 0., 0., 0., 1., 0.], |
| 77 | + [ 0., -1., 0., 0., 0., 1., 0.], |
| 78 | + [ 0., -1., 0., 0., 0., 1., 0.], |
| 79 | + [ 0., -1., 0., 0., 0., 1., 0.], |
| 80 | + [ 0., -1., 0., 0., 0., 1., 0.], |
| 81 | + [ 0., -1., 0., 0., 0., 1., 0.]]) |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +```python |
| 87 | +corr2d(X.t(),K) |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + tensor([[0., 0., 0., 0., 0.], |
| 94 | + [0., 0., 0., 0., 0.], |
| 95 | + [0., 0., 0., 0., 0.], |
| 96 | + [0., 0., 0., 0., 0.], |
| 97 | + [0., 0., 0., 0., 0.], |
| 98 | + [0., 0., 0., 0., 0.], |
| 99 | + [0., 0., 0., 0., 0.], |
| 100 | + [0., 0., 0., 0., 0.]]) |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +```python |
| 106 | +conv2d = nn.Conv2d(1, 1, kernel_size=(1, 2), bias=False) |
| 107 | + |
| 108 | +X = X.reshape((1, 1, 6, 8)) |
| 109 | +Y = Y.reshape((1, 1, 6, 7)) |
| 110 | + |
| 111 | +for i in range(10): |
| 112 | + Y_hat = conv2d(X) |
| 113 | + l = (Y_hat - Y)**2 |
| 114 | + conv2d.zero_grad() |
| 115 | + l.sum().backward() |
| 116 | + conv2d.weight.data[:] -= 3e-2 * conv2d.weight.grad |
| 117 | + if (i + 1) % 2 == 0: |
| 118 | + print(f'batch {i+1}, loss {l.sum():.3f}') |
| 119 | +``` |
| 120 | + |
| 121 | + batch 2, loss 3.852 |
| 122 | + batch 4, loss 1.126 |
| 123 | + batch 6, loss 0.386 |
| 124 | + batch 8, loss 0.145 |
| 125 | + batch 10, loss 0.057 |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +```python |
| 130 | +conv2d.weight.data.reshape((1, 2)) |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + tensor([[-1.0173, 0.9685]]) |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +```python |
| 142 | + |
| 143 | +``` |
0 commit comments