Computing Gradients with respect to Input Features given model parameters

arpitdm: <p>I’m new to Pytorch and I’ve been going through the tutorials but I feel like I don’t properly understand the modules for computing gradients. Specifically, I am trying to understand if there are methods to compute the gradient of a model with respect to specific inputs. Consider the following minimum working example: a simple Neural Network with a single hidden layer.</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F

class NeuralNet(nn.Module):
    def __init__(self, n_features, n_hidden, n_classes, dropout):
        super(NeuralNet, self).__init__()

        self.fc1 = nn.Linear(n_features, n_hidden)
        self.sigmoid = nn.Sigmoid()
        self.fc2 = nn.Linear(n_hidden, n_classes)
        self.dropout = dropout

    def forward(self, x):
        x = self.sigmoid(self.fc1(x))
        x = F.dropout(x, self.dropout, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)
</code></pre>
<p>I instantiate the model and an optimizer as follows:</p>
<pre><code class="lang-auto">import torch.optim as optim
model = NeuralNet(n_features=args.n_features,
            n_hidden=args.n_hidden,
            n_classes=args.n_classes,
            dropout=args.dropout)
optimizer_w = optim.SGD(model.parameters(), lr=0.001)
</code></pre>
<p>During training, I compute the gradients of the negative log-likelihood loss w.r.t. the model parameters by doing the following:</p>
<pre><code class="lang-auto">def train(epoch):
    t = time.time()
    model.train()
    optimizer.zero_grad()
    output = model(features, adj)
    loss_train = F.nll_loss(output[idx_train], labels[idx_train])
    acc_train = accuracy(output[idx_train], labels[idx_train])
    loss_train.backward()
    optimizer_w.step()

for epoch in range(args.epochs):
    train(epoch)
</code></pre>
<p>For an experiment, I am interested in “updating” some of the input features (say the last <code>d</code> features). Given the model above, and the same <code>loss_train</code>, can I define an <code>optimizer_f</code> w.r.t to features and not <code>model.parameters()</code>, and correspondingly compute <code>loss_train.backward()</code>, perform an <code>optimizer_f.step()</code>?<br>
Assume that the model parameters have been computed as described above in the standard manner so that all required values for the differentiation are available.</p>

MariosOreo: <p>Hi,</p>
<aside class="quote no-group" data-username="arpitdm" data-post="1" data-topic="44965">
<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/arpitdm/48/12015_2.png" class="avatar"> arpitdm:</div>
<blockquote>
<p>can I define an <code>optimizer_f</code> w.r.t to features and not <code>model.parameters()</code> , and correspondingly compute <code>loss_train.backward()</code> , perform an <code>optimizer_f.step()</code> ?</p>
</blockquote>
</aside>
<p>Yes you can do that, and <a href="https://pytorch.org/docs/stable/optim.html#per-parameter-options" rel="noopener nofollow ugc">here</a> is a small example from docs. You can pass the parameter (you want to update) to the optimizer, and others will fixed when you call <code>optimizer_f.step()</code>.</p>

arpitdm: <p>Hi,</p>
<p>Thanks for your response. I need to clarify what I am trying to do.</p>
<p>In the sample code above, during each iteration of <code>train(epoch)</code>, after updating the weights of the model as described, I want to use these weights or <code>model.parameters()</code> to “update” some of the features. i.e. <code>features[:, f:]</code> for some value <code>f &lt; features.shape[1]</code>:</p>
<pre><code class="lang-auto">def train(epoch):
    # train model
    # update weights

    # update the last (features.shape[1] - f) features &lt;--

for epoch in range(args.epochs):
    train(epoch)
</code></pre>
<p>The last <code>(features.shape[1] - f)</code> features need to be updated by computing the gradient of the error w.r.t. these features (given the recently computed model weights/parameters) and taking a gradient step.</p>