Inside my nn.Module class I am creating nn.Parameters() such as:

self.B =self.reset_parameters(nn.Parameter(torch.Tensor(self.projection_dim,10),requires_grad=True)).unsqueeze(0).repeat(num_heads,1,1) #I am using the reset parameters from torch source code pytorch/linear.py at master · pytorch/pytorch · GitHub 1

Apparently, when I run the training loop I get that error.
My goal is to train two matrices A and B (like a feedforward neuralnet) in order to project a given x as xAB, it has to compute AB and then xAB for complexity reasons.

Traceback (most recent call last):
File “main.py”, line 304, in 
main(args)
File “main.py”, line 291, in main
training(model,criterion,optimizer,scheduler,train_loader,valid_loader,args)
File “main.py”, line 221, in training
loss.backward()
File “C:\Users\lucas\anaconda3\envs\tcc\lib\site-packages\torch\tensor.py”, line 185, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File “C:\Users\lucas\anaconda3\envs\tcc\lib\site-packages\torch\autograd_init_.py”, line 125, in backward
Variable._execution_engine.run_backward(
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.

When I use retain_graph I get out of memory from cuda.
The parameters self.A and self.B don’t show up in model.parameters() as well.
I’ve seen people training positional embedding like this and never had to retain_graph, so I wonder what could be wrong. Thanks