Fail to train nn.Parameter()

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/ at master · pytorch/pytorch · GitHub

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 “”, line 304, in
File “”, line 291, in main
File “”, line 221, in training
File “C:\Users\lucas\anaconda3\envs\tcc\lib\site-packages\torch\”, line 185, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File “C:\Users\lucas\anaconda3\envs\tcc\lib\site-packages\torch\”, line 125, in 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

Not sure what is the problem extractly, but try to do this:

self.B = nn.Parameter(torch.Tensor(self.projection_dim,10))
(The way you use reset_parameters is wierd, I don’t even know why it works. You should just run self.reset_parameters at the end of init)

in the forward use,

The way I reset parameters is the same from the nn.Linear

def reset_parameters(self,W,B=None):
W=torch.nn.init.kaiming_uniform_(W, a=math.sqrt(3))
if B is not None:
fan_in, _ = torch.nn.init.calculate_fan_in_and_fan_out(W)
bound = 1 / math.sqrt(fan_in)
(B, -bound, bound)
return W,B
return W

I will try without the reset_parameters and as soon as my gpus are free, thank you.

What about not taking and returning anything, do exact what nn.Linear does.

In a certain way I am trying to use a projection just like nn.Linear and then projecting again with another matrix, but I would like to control the order of the computing, for time complexity reasons. x (W_1 W_2) instead of (x W_1) W_2

I understand that. But reset_parameter should just initialize the parameter in place, instead of taking some input and return some output.

When you define a variable in nn.Module, nn.Module will do something secretly, so the best way to define a parameter is: = nn.Parameter(some tensor)
instead of = nn.Parameter(soem tensor).unsqueeze(xxx).repeat(xxxx)

1 Like