LSTM , CNN network weights doesnt get updated

sh_sh: <p>pleeeeaaaase someone help me!!!<br>
hi there<br>
i’m trying to make a cnn (more specific Gat which is basically an attention network for graphs) evoloving by the help of an LSTMcell.<br>
the problem is when I put the LSTM output in pytorch GATV2 weights the weights doesnt get updated in Gat for next epoch, although it gets updated in the lstm.<br>
the gat weights are torch.nn.parameter type and the lstm output that i want to put in that weight is torch,floattensor type.</p>
<pre><code class="lang-auto">self.conv1 = GATv2Conv(num_node_features, hidden_channels[0])
        self.conv2 = GATv2Conv(hidden_channels[0], 2)
       
        hiddenSize=num_node_features*hidden_channels[0]
       
        self.LSTM1 = nn.LSTMCell(num_node_features,num_node_features)
     
        self.h1=nn.Parameter(nn.init.xavier_uniform(torch.Tensor( hidden_channels[0], num_node_features).type(torch.FloatTensor)), requires_grad=True)
        self.c1=nn.Parameter(nn.init.xavier_uniform(torch.Tensor( hidden_channels[0], num_node_features).type(torch.FloatTensor)), requires_grad=True)
      
</code></pre>
<p>the problem is self.conv1.lin_l.weight doesnt get updated from 2nd epoch after conv1, although it changes after lstm1</p>
<pre><code class="lang-auto">def forward(self, data):
        
        x = self.conv1(data.x, data.edge_index)
          
        print("weight gat",self.conv1.lin_l.weight)

     
        A=(self.LSTM1((self.conv1.lin_l.weight),(self.h1,self.c1)))
       
        self.conv1.lin_l.weight=torch.nn.parameter.Parameter(A[0])
        self.c1=torch.nn.parameter.Parameter(A[1])
        self.h1=torch.nn.parameter.Parameter(A[0])
      
        print("weight after lstm",self.conv1.lin_l.weight)
        x = x.relu()
        x = F.dropout(x, p=0.8, training=self.training) 
        x = self.conv2(x, data.edge_index)
</code></pre>
<p>and here is the usage of the model</p>
<pre><code class="lang-auto"> model.train()
    train_loss = 0
    for data in train_loader:
        data = data.to(device)
        optimizer.zero_grad()
        out = model(data)
        loss = criterion(out[data.train_mask], data.y[data.train_mask])
        _, pred = out[data.train_mask].max(dim=1)
        loss.backward()
        
        train_loss += loss.item() * data.num_graphs
        optimizer.step()
    train_loss /= len(train_loader.dataset)
    

</code></pre>
<p>I guess there might be a problem with assigning the data and types or with the relu or softmax or back propagation. can anyone guess why the weights of gatv2 doesnt get updated?<br>
ps: I’m using google colab for execution</p>

ptrblck: <p>This line of code looks wrong:</p>
<pre><code class="lang-python">self.conv1.lin_l.weight=torch.nn.parameter.Parameter(A[0])
</code></pre>
<p>as you are assigning a new parameter to the <code>.weight</code> attribute, which would be missing in the <code>optimizer</code>. I don’t know how and were <code>self.conv1</code> is used, but changing the parameters after they were used also sounds generally wrong.</p>
<p>In any case, if you want to manipulate the weight before they are used, you could use <code>.copy_()</code> and warp the manipulation into a <code>with torch.no_grad()</code> block.</p>

sh_sh: <p>thanks alot for helping<br>
i want to change the wieghts by using lstm so the output of lstm will be my new weight for the first layer (self.conv1) but after i change the weights of conv1 the weights doesnt get updated in conv1 anymore<br>
i’ve tried your copy and deepcopy and  with torch.no_grad() but still weight doesnt get updated<br>
ps: i dont know if it can be bc the weights are not  graph leaves and not created explicitly by the user  or not!</p>

ptrblck: <aside class="quote no-group" data-username="sh_sh" data-post="3" data-topic="154419">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sh_sh/48/45994_2.png" class="avatar"> sh_sh:</div>
<blockquote>
<p>so the output of lstm will be my new weight for the first layer</p>
</blockquote>
</aside>
<p>You could use the output activation as a parameter for another layer by e.g. using the functional API, but note that the optimizer will not be able to update an activation as it’s created in each forward pass using the parameters of the <code>LSTM</code> module.</p>

sh_sh: <p>thanks alot again you’re right i guess the problem is with the loss.backward and optimizer that doesnt update gat weights anymore, although i didn’t get the exact reason<br>
do you know any way to make the model i need in a way that i can have the weights updated?</p>

ptrblck: <p>No, I don’t know how this approach would properly work.<br>
Currently you are trying to optimize an output activation which is itself created using the parameters from the <code>LSTM</code> module (which are passed to the optimizer and updated).<br>
This would mean that even if the optimizer could update the output activation, these updates would be wiped in the next iteration since the updated <code>LSTM</code> parameters will create a new output activation.</p>