首页 > 其他分享 >卷积神经网络(李沐老师课程)

卷积神经网络(李沐老师课程)

时间:2024-09-10 14:22:42浏览次数:10  
标签:卷积 self torch shape print 神经网络 李沐 conv2d

卷积神经网络(李沐老师课程)

回顾MLP单层(上述列子需要14GB GPU)

找寻图片上的人在哪里

找寻图片上的人的两个基本原则

从全连接层出发到卷积

卷积层

二维交叉相关

二维卷积层

案列

交叉相关和卷积

代码的实现

import torch
from torch import nn
from d2l import torch as d2l
​
​
def corr2d(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()
    return Y
​
​
"""
验证上述二维互相关运算的输出
"""
x = torch.tensor([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
corr = corr2d(x, K)
print(corr)
​
"""
实现二维卷积层
"""
​
​
class Conv2D(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 corr2d(x, self.weight) + self.bias
​
​
"""
检测图像中不同颜色的边缘
"""
x = torch.ones((6, 8))
x[:, 2:6] = 0
print(x)
​
k = torch.tensor([[1.0, -1.0]])
"""
输出Y中的1代表从白色到黑色的边缘,-1代表从黑色到白色的边缘
"""
y = corr2d(x, k)
print(y)
"""
以上卷积核只能检测垂直边缘
"""
print(corr2d(x.t(), k))
​
"""
学习由X到Y生成的卷积核
"""
conv2d =nn.Conv2d(1,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):
    y_hat = conv2d(x)
    l = (y_hat-y)**2
    conv2d.zero_grad()
    l.sum().backward()
    conv2d.weight.data[:]-=3e-2*conv2d.weight.grad
    if (i+1)%2==0:
        print(f"batch {i+1},loss {l.sum():.3f}")
​
"""
所学的卷积核的权重张量
"""
print(conv2d.weight.data.reshape((1, 2)))

标签:卷积,self,torch,shape,print,神经网络,李沐,conv2d
From: https://blog.csdn.net/2401_87085787/article/details/142098121

相关文章