Model parameters not updating in training loop

rutledgea20: <p>I am attempting to train an LSTM model to do time series prediction in libtorch. Here is how I set up the model:</p>
<pre><code class="lang-auto">struct LSTMPredictorImpl : torch::nn::Module {
    LSTMPredictorImpl(int n_hidden = 51) :
        lstm1(torch::nn::LSTMCell(1, n_hidden)),
        lstm2(torch::nn::LSTMCell(n_hidden, n_hidden)),
        linear1(torch::nn::Linear(n_hidden, 1))
    {
        register_module("lstm1", lstm1);
        register_module("lstm2", lstm2);
        register_module("linear1", linear1);
        this-&gt;n_hidden = n_hidden;
    }
    torch::Tensor forward(torch::Tensor x, int future = 0) {
        int n_samples = x.sizes()[0];
        std::vector&lt;torch::Tensor&gt; outputs;
        std::tuple&lt;torch::Tensor, torch::Tensor&gt; hc_t1(
            torch::zeros({n_samples, n_hidden}), 
            torch::zeros({n_samples, n_hidden}));
        std::tuple&lt;torch::Tensor, torch::Tensor&gt; hc_t2(
            torch::zeros({n_samples, n_hidden}), 
            torch::zeros({n_samples, n_hidden}));

        torch::Tensor output;
        std::vector&lt;torch::Tensor&gt; separated = x.split(1, 1);
        for (torch::Tensor input_t : separated) {
            hc_t1 = lstm1(input_t, hc_t1);
            hc_t2 = lstm2(std::get&lt;0&gt;(hc_t1), hc_t2);
            output = linear1(std::get&lt;0&gt;(hc_t2));
            outputs.push_back(output);
        }
        for (int i = 0; i &lt; future; i++) {
            hc_t1 = lstm1(output, hc_t1);
            hc_t2 = lstm2(std::get&lt;0&gt;(hc_t1), hc_t2);
            output = linear1(std::get&lt;0&gt;(hc_t2));
            outputs.push_back(output);
        }
        torch::TensorList intermediate = torch::TensorList(separated);
        torch::Tensor final_output = torch::cat(intermediate, 1);
        final_output.requires_grad_(true);
        return final_output;
    }
    torch::nn::LSTMCell lstm1, lstm2;
    torch::nn::Linear linear1;
    int n_hidden;
};
TORCH_MODULE(LSTMPredictor);
</code></pre>
<p>And here is my training loop:</p>
<pre><code class="lang-auto">void time_series_train(torch::Tensor data) {
    auto data_sizes = data.sizes();
    torch::Tensor train_input = data.index({
        torch::indexing::Slice(3),
        torch::indexing::Slice(0, -1)
    });
    torch::Tensor train_target = data.index({
        torch::indexing::Slice(3),
        torch::indexing::Slice(1)
    });
    torch::Tensor test_input = data.index({
        torch::indexing::Slice(0, 3),
        torch::indexing::Slice(0, -1)
    });
    torch::Tensor test_target = data.index({
        torch::indexing::Slice(0, 3),
        torch::indexing::Slice(1)
    });

    LSTMPredictor predictor;
    torch::optim::LBFGS LSTM_optimizer(
        predictor-&gt;parameters(), torch::optim::LBFGSOptions(0.8)
    );

    auto cost = [&amp;](){
        LSTM_optimizer.zero_grad();
        auto out = predictor-&gt;forward(train_input);
        auto loss = torch::mse_loss(out, train_target);
        cout &lt;&lt; "loss: " &lt;&lt; loss.item() &lt;&lt; endl;
        loss.backward();
        return loss;
    };
    
    int n_training_steps = 10;
    for (int i = 0; i &lt; n_training_steps; i++) {
        printf("step: %d\n", i);
        LSTM_optimizer.step(cost);
    }
}
</code></pre>
<p>When running the training loop, the output just looks like this:</p>
<blockquote>
<p>step: 0<br>
loss: 0.291023<br>
step: 1<br>
loss: 0.291023<br>
step: 2<br>
loss: 0.291023<br>
step: 3<br>
loss: 0.291023<br>
step: 4<br>
loss: 0.291023<br>
step: 5<br>
loss: 0.291023<br>
step: 6<br>
loss: 0.291023<br>
step: 7<br>
loss: 0.291023<br>
step: 8<br>
loss: 0.291023<br>
step: 9<br>
loss: 0.291023</p>
</blockquote>
<p>I think it may have something to do with me putting:</p>
<pre><code class="lang-auto">final_output.requires_grad_(true);
</code></pre>
<p>in the forward pass function, but not including that causes an autograd error to be thrown during loss.backward().</p>
<p>Please let me know what I’m doing wrong, thank you.</p>

rutledgea20: <p>Well I feel quite dumb… I’ve made multiple posts due to weird behavior on these forums over the past few days having to do with autograd and now this not training issue, all of it is because the line</p>
<pre><code class="lang-auto">torch::TensorList intermediate = torch::TensorList(separated);
</code></pre>
<p>Should have been</p>
<pre><code class="lang-auto">torch::TensorList intermediate = torch::TensorList(outputs);
</code></pre>
<p>After fixing this i no longer need to change any requires_grad values and my model trains as it should.</p>
<p>Apologies.</p>