李沐_动手学深度学习_19_卷积层

一、遇到的磕磕绊绊:

1.一维数组 和 二维矩阵数据之间的区别:

李沐_动手学深度学习_19_卷积层_第1张图片二、用到的一些代码:
#备注,矩阵点乘 就是 A*B
import torch
from d2l import torch as d2l  #这个库是李沐自己写的,我去
from torch import nn

def corr(X,k):
    h,w = k.shape
    Y = torch.zeros((X.shape[0]-h+1,X.shape[1]-w+1))
    for i in range(Y.shape[0]):
        for j in range(Y.shape[1]):
            Y[i,j] = ((X[i:i+h,j:j+w])*k).sum() #这里证明了,先h 后w,谢谢
    return Y
X =torch.tensor([[0.0,1.0,2.0],[1.0,2.0,3.0],[2.0,3.0,4.0]])
K =torch.tensor([[0.0,1.0],[2.0,3.0]])
corr(X,K)
#手动利用nn.module的模板实现 二维卷积层:
class my_conv(nn.Module):
    def __init__(self,kernel_size):
        super().init()
        self.weight =nn.Parameter(torch.rand(kernel_size))
        self.bias = nn.Parameter(torch.zeros(1))
    def forward(self,x):
        return corr(x,self.weight)+self.bias


#利用卷积层 实现对图像的 颜色边缘进行检测:
X=torch.ones(6,8)
X[:,2:6] = 0
X
import matplotlib.pyplot as plt
show_x = X.numpy()
plt.imshow(show_x)
plt.show()
Kernel = torch.tensor([[1.0,-1.0]])
Y = corr(X,Kernel)
show_out = Y.numpy()
plt.imshow(show_out)
plt.show()
Y
#通过使用nn.conv2d 自己学出 可以分辨 颜色边界的 kernel
conv2d = nn.Conv2d(in_channels=1,out_channels=1,kernel_size=(1,2),bias = False)

X = X.reshape(1,1,6,8)
Y = Y.reshape(1,1,6,7)

for i in range(10):
    conv2d.zero_grad() #先清空前一次的grad
    Y_hat = conv2d(X)
    loss = (Y_hat-Y)**2  #手动计算MSE
    loss.sum().backward()
    #手动进行optim.step
    conv2d.weight.data[:] -=3e-2*conv2d.weight.grad
    
    #每个epoch进行必要的输出:
    print('epoch : ',i ," loss = ",loss.sum().item())
print(conv2d.weight.data.reshape(1,2))

你可能感兴趣的:(人工智能,深度学习,人工智能,算法)