import torch
from torch import nn
def corr2d(X, K): #@save
"""计算二维互相关运算"""
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
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))
self.weight = torch.rand(kernel_size,requires_grad=True)
self.bias = torch.zeros(1,requires_grad=True)
def forward(self, x):
return corr2d(x, self.weight) + self.bias
# 构造一个二维卷积层,它具有1个输出通道和形状为(1,2)的卷积核
conv2d = nn.Conv2d(1,1, kernel_size=(1, 2), bias=False)
# 这个二维卷积层使用四维输入和输出格式(批量大小、通道、高度、宽度),
# 其中批量大小和通道数都为1
X = torch.ones((6, 8))
X[:, 2:6] = 0
K = torch.tensor([[1.0, -1.0]])
Y = corr2d(X, K)
X = X.reshape((1, 1, 6, 8))
Y = Y.reshape((1, 1, 6, 7))
lr = 3e-2 # 学习率
for i in range(10):
Y_hat = conv2d(X)
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}')
import torch
from torch import autograd
x1 = torch.rand(3,4)
x2 = torch.rand(3,4)
x1.requires_grad_()#等价于x1.requires_grad=True
x2.requires_grad_()#等价于x2.requires_grad=True
print(x1)
print(x2)
# 对x中的所有元素求和,得到的结果是标量
z=x1*x2
y = torch.sum(z)
print(y)
grads = autograd.grad(y, (x1,x2))
print(grads[0])
print(grads[1])