# Training a torch::jit::script::Module

**URL:** <https://discuss.pytorch.org/t/training-a-torch-module/58512>\
**Category:** jit\
**Created:** [October 17, 2019, 6:34pm UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512 "2019-10-17T18:34:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/markl/32/16511_2.png) [@markl](https://discuss.pytorch.org/u/markl)\
**Post date:** [October 17, 2019, 6:34pm UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512/1 "2019-10-17T18:34:44Z")

</div>

There is an example for training a `torch::nn::Module` [https://pytorch.org/cppdocs/frontend.html#end-to-end-example](https://pytorch.org/cppdocs/frontend.html#end-to-end-example) but when I try to use a `torch::jit::script::Module`, I cannot call `.parameters()` like in the example.

Is there a way to train a `torch::jit::script::Module` in C++?

---

<div class="post-metadata">

**Author:** ![driazati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/32/15393_2.png) [@driazati](https://discuss.pytorch.org/u/driazati)\
**Post date:** [October 17, 2019, 10:45pm UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512/2 "2019-10-17T22:45:46Z")

</div>

Weirdly unlike in Python, `jit::script::Module` doesn’t have the same API as `nn::Module`.

You should be able to access a `script::Module`'s parameters via something like:

```cpp
for (auto param_slot : my_module.get_parameters()) {
    auto my_tensor = param_slot.value().toTensor();
}

```

---

<div class="post-metadata">

**Author:** ![markl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/markl/32/16511_2.png) [@markl](https://discuss.pytorch.org/u/markl)\
**Post date:** [October 18, 2019, 1:29am UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512/3 "2019-10-18T01:29:41Z")

</div>

The `nn::Module::parameters()` returns a thing called a `slot_list_impl<>`.

Do you have advice on how I can make one of these from the tensors returned by the `script::Module::get_parameters()`?

---

<div class="post-metadata">

**Author:** ![driazati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/32/15393_2.png) [@driazati](https://discuss.pytorch.org/u/driazati)\
**Post date:** [October 23, 2019, 5:51pm UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512/4 "2019-10-23T17:51:57Z")

</div>

`slot_list_impl<NameValue>` is an iterator over the one of a `script::Module`'s internal lists of Parameters/Attributes. Can you elaborate on your use case for constructing one yourself? You should be able to mess with a `script::Module` by using `script::Module::register_parameter` to change any parameter value.

---

<div class="post-metadata">

**Author:** ![markl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/markl/32/16511_2.png) [@markl](https://discuss.pytorch.org/u/markl)\
**Post date:** [October 23, 2019, 7:32pm UTC](https://discuss.pytorch.org/t/training-a-torch-module/58512/5 "2019-10-23T19:32:15Z")

</div>

I was trying to adapt the training loop to work for `torch::jit::script::Module` so I needed the same interface that the optimizer expects. However, I realized that it accepts `std::vector<at::Tensor>`, so I actually don’t have to create `slot_list_impl` after all.

Maybe you can take a look at what I was trying to do here [jit::script::Module parameters are not updating when training](https://discuss.pytorch.org/t/jit-module-parameters-are-not-updating-when-training/58945/8)

I think for the code to actually work, I need to use `register_parameter` or `set_parameter` to push the updated tensor back into the `script::Module` after the optimizer has acted on it. Does that sound right? Or could there be a way that the optimizer can update the tensor values in place?
