When I was learning convolutional neural networks, I saw statements that I had not seen before, so I learned conv2d. zero_ Grad(), I am curious about what object can call this statement, so I changed the two-dimensional convolutional layer to a linear layer on the basis of normal operation, but it always reports an error: the error message is Trying to back through the graph a second time
import torch
from torch import nn
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.ones((6, 8))
X[:, 2:6] = 0
K = torch.tensor([[1.0, -1.0]])
Y = corr2d(X, K)
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))
lr = 3e-2
for i in range(10):
Y_hat = conv2d(X)
print(Y_hat)
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}")
# Can operate normally
import torch
from torch import nn
X = torch.rand(size = (2,5),requires_grad = True)
true_w = torch.tensor([1,2,3,4,5.]).reshape((X.shape[1],-1))
true_b = torch.zeros(X.shape[0]).reshape((-1,1))
Y = torch.matmul(X,true_w) + true_b
Y += torch.rand(size=(Y.shape))
linear1 = nn.Linear(5,1)
lr = 0.01
for i in range(10):
Y_hat = linear1(X)
l = (Y_hat-Y)**2
print(l)
linear1.zero_grad()
l.sum().backward()
linear1.weight.data[:] -= lr*linear1.weight.grad
if(i + 1) % 2 == 0:
print(f"epoch{i+1},loss{l.sum():.3f}")
# Trying to backward through the graph a second time