I try to optimize some manually created parameters to see how the grad system work. But the parameters never changed for the same input, here is the code:
#include <string>
#include <iostream>
#include <torch/torch.h>

torch::Tensor forward_and_optimize_parameter(
    torch::Tensor&& input,
    std::vector<torch::Tensor>& parameters,
    torch::optim::SGD& optim) {
  auto loss = torch::sum(torch::pow(input + parameters.front(), 2));
  optim.zero_grad();
  loss.backward();
  optim.step(); // I expect the parameters being changed after this, but it's not, why?
  return loss;
}

int main() {
  auto options = torch::TensorOptions().requires_grad(true);
  std::vector<torch::Tensor> parameters{torch::rand(5, options)};
  torch::optim::SGD optimizer(parameters, /*lr=*/1);

  for (size_t i = 0; i <= 10; i++) {
    auto loss = forward_and_optimize_parameter(torch::ones({5}), parameters, optimizer);
    std::cout<<loss<<std::endl; // the loss stays the same for the same input, the optimize is not working?
  }

  return 0;
}