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]])
# print(corr2d(X, K))
# # tensor([[19., 25.],
# #         [37., 43.]])

# 定义二维卷积层
class Conv2D(nn.Module):
    def __init__(self, kernel_size):    #kernel_size为指定的卷积核尺寸
        super().__init__()
        # 随机初始化卷积核权重参数
        self.weight = nn.Parameter(torch.rand(kernel_size))
        # 将偏执初始化为0
        self.bias = nn.Parameter(torch.zeros(1))
    # 前向传播函数:将图像与卷积核进行卷积操作
    def forward(self, x):
        return corr2d(x, self.weight) + self.bias

# 学习卷积层的权重(卷积核)
# 学习卷积核之前需要构建出来数据集(X,Y)
# 构造X(X为6行8列,其中2~5列为0,其余列为1)
X = torch.ones((6, 8))
X[:, 2:6] = 0
# 待学习的卷积核为K
K = torch.tensor([[1.0, -1.0]])
# X和K进行卷积操作得到输出为Y(Y为6行7列,其中第1列为1,第5列为-1,其余列为0)
Y = corr2d(X, K)

# 下面开始学习卷积核
# 用PyTorch的API来构造一个二维卷积层,它具有1个输出通道和形状为(12)的卷积核
# in_channels:输入图像的通道数,out_channels:输出图像的通道数,kernel_size:卷积核的尺寸,bias:是否需要学习偏执量
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)) #二维卷积层使用的四维输出格式:(批量大小、通道、高度、宽度)
lr = 3e-2  # 学习率

# 训练过程
for i in range(10):
    # 预测输出Y_hat
    Y_hat = conv2d(X)
    # 预测损失l
    l = (Y_hat - Y) ** 2
    # 梯度下降
    conv2d.zero_grad()
    l.sum().backward()
    conv2d.weight.data[:] -= lr * conv2d.weight.grad
    # 打印当前训练的次数和预测损失
    if (i + 1) % 2 == 0:
        print(f'epoch {i+1}, loss {l.sum():.3f}')
print("学习到的卷积核为:")
print(conv2d.weight.data.reshape((1, 2)))

# epoch 2, loss 11.735
# epoch 4, loss 3.833
# epoch 6, loss 1.407
# epoch 8, loss 0.549
# epoch 10, loss 0.220
# 学习到的卷积核为:
# tensor([[ 1.0377, -0.9415]])

 

posted on 2022-10-23 21:47  yc-limitless  阅读(55)  评论(0)    收藏  举报