# Deep copying PyTorch modules

**URL:** <https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514>\
**Category:** Uncategorized\
**Created:** [February 10, 2018, 12:08am UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514 "2018-02-10T00:08:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juliohm/32/3419_2.png) [@juliohm](https://discuss.pytorch.org/u/juliohm)\
**Post date:** [February 10, 2018, 12:08am UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514/1 "2018-02-10T00:08:50Z")

</div>

Hi, I am new to PyTorch, and was wondering if there is an API defined for deep copying Modules? I have a function that accepts a Module as input, and trains it. Because I don’t want to keep track of object’s state, I am planning to just deep copy the trained model and save it somewhere else in a database.

Is it safe to just do `copy.deepcopy(mymodel)`?

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [February 10, 2018, 7:18am UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514/2 "2018-02-10T07:18:29Z")

</div>

If I had to deep copy a model, I would do something like this…

```auto
model_copy = type(mymodel)() # get a new instance
model_copy.load_state_dict(mymodel.state_dict()) # copy weights and stuff

```

I can’t see how that would help you “save it somewhere else in a database”. I assume you have read the [docs on serialising models](http://pytorch.org/docs/0.3.0/notes/serialization.html) and the recommendations and warnings on that page.

---

<div class="post-metadata">

**Author:** ![Jayant\_Parashar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jayant_parashar/32/21173_2.png) [@Jayant\_Parashar](https://discuss.pytorch.org/u/Jayant_Parashar)\
**Post date:** [February 29, 2020, 9:08pm UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514/3 "2020-02-29T21:08:23Z")

</div>

In type(mymodel)(), are arguments also copied by default from mymodel instance? Or do we need to make sure the arguments match the mymodel instance?

---

<div class="post-metadata">

**Author:** ![asalimih](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asalimih/32/18991_2.png) [@asalimih](https://discuss.pytorch.org/u/asalimih)\
**Post date:** [November 8, 2020, 2:08am UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514/4 "2020-11-08T02:08:34Z")

</div>

> [@jpeg729](#):
>
> ```auto
> model_copy = type(mymodel)() # get a new instance
> model_copy.load_state_dict(mymodel.state_dict()) # copy weights and stuff
> 
> ```

if there are arguments in the ` __init__ ` function of `mymodel` you must pass them too. `type(mymodel)(args)`

---

<div class="post-metadata">

**Author:** ![Gayal\_Shamane](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gayal_shamane/32/15224_2.png) [@Gayal\_Shamane](https://discuss.pytorch.org/u/Gayal_Shamane)\
**Post date:** [February 16, 2021, 10:45pm UTC](https://discuss.pytorch.org/t/deep-copying-pytorch-modules/13514/5 "2021-02-16T22:45:07Z")

</div>

what if I do

**model\_copy.load\_state\_dict(mymodel.state\_dict()).to(device=‘cpu’)**?

All I want is to use an instance of a model as an input to another function while training. Here I do not need any gradient flow for this opperation.
