RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed pytorch
How do I clear the gradient before calling the .backward() for the second time.

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.

a = torch.tensor([2.0], requires_grad = True)
b = torch.tensor([2.0], requires_grad = True)
d = torch.tensor([2.0], requires_grad = True)
c=a*b
c.backward()
e = d*e
e.backward(retain_graph=True)

I tried doing this: c.zero_grad() but I got the error c has no method zero_grad()

As the error message reads, you need to specify the retain_graph=True option on the first .backward call, not the second:
c.backward(retain_graph=True)
e = d*c
e.backward()

If you do not retain the graph, the second backward pass will not be able to reach the nodes c, a, and b because the activations will have been cleared by the first backward pass.
