I'm a college student just getting into Deep Learning, trying to create my first backward propagated model.
However, I keep getting the "Trying to backward through the graph a second time, but the saved intermediate results have already been freed." runtime error.
I have seen many others asking the same question here, but their sample code is often too advanced for me, and I don't understand the answers.
I have already tried adding convtraining.zero_grad()
and loss.sum().backward(retain_graph = True)
. Neither seem to work.
My own code is the following:
# Import torch.
import torch
import torch.nn as nn
# Define a sample image.
image = torch.tensor([[1, 1, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 0, 0, 0, 1]], dtype = torch.float).reshape(1,1,5,5)
# Define a sample kernel.
kernel = torch.tensor([[0,-1, 0],
[-1, 1, -1],
[0,-1, 0]], dtype = torch.float).reshape(1,1,3,3)
# Define a convolution layer with the TRUE kernel weights assigned.
convolution = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=(3, 3), bias=False)
convolution.weight = nn.Parameter(kernel)
# Define another convolution without kernel weights to PREDICT them.
convtraining = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=(3, 3), bias=False)
# Run the first layer.
output_true = convolution(image)
# Reshape its output to make it suitable for comparison.
output_true = output_true.reshape((1, 1, 3, 3))
# Run the second model 5 times.
for i in range(5):
output_prediction = convtraining(image)
# Calculate the loss by squaring the error.
loss = (output_prediction - output_true) ** 2
convtraining.zero_grad()
# Backward propagation.
loss.sum().backward()
# Adjust the kernel weights.
convtraining.weight.data[:] -= 3e-2 * convtraining.weight.grad
print(loss)
The strange thing is it worked before and I don't know what changed. Does anyone know what might be going wrong?