Skip to content

Commit 767b9a8

Browse files
authored
Some syntax edits for build and headers
1 parent 95f9b58 commit 767b9a8

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

recipes_source/recipes/saving_multiple_models_in_one_file.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
"""
22
Saving and loading multiple models in one file using PyTorch
33
============================================================
4-
54
Saving and loading multiple models can be helpful for reusing models
65
that you have previously trained.
76
87
Introduction
98
------------
10-
119
When saving a model comprised of multiple ``torch.nn.Modules``, such as
1210
a GAN, a sequence-to-sequence model, or an ensemble of models, you must
1311
save a dictionary of each model’s state_dict and corresponding
1412
optimizer. You can also save any other items that may aid you in
1513
resuming training by simply appending them to the dictionary.
16-
1714
To load the models, first initialize the models and optimizers, then
1815
load the dictionary locally using ``torch.load()``. From here, you can
1916
easily access the saved items by simply querying the dictionary as you
2017
would expect.
21-
2218
In this recipe, we will demonstrate how to save multiple models to one
2319
file using PyTorch.
2420
2521
Setup
2622
-----
27-
2823
Before we begin, we need to install ``torch`` if it isn’t already
2924
available.
3025
26+
::
27+
28+
pip install torch
29+
3130
"""
3231

33-
pip install torch
3432

3533

3634
######################################################################
@@ -43,7 +41,7 @@
4341
# 4. Save multiple models
4442
# 5. Load multiple models
4543
#
46-
# **1) Import necessary libraries for loading our data**
44+
# 1. Import necessary libraries for loading our data
4745
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4846
#
4947
# For this recipe, we will use ``torch`` and its subsidiaries ``torch.nn``
@@ -56,7 +54,7 @@
5654

5755

5856
######################################################################
59-
# **2) Define and intialize the neural network**
57+
# 2. Define and intialize the neural network
6058
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6159
#
6260
# For sake of example, we will create a neural network for training
@@ -88,7 +86,7 @@ def forward(self, x):
8886

8987

9088
######################################################################
91-
# **3) Initialize the optimizer**
89+
# 3. Initialize the optimizer
9290
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9391
#
9492
# We will use SGD with momentum to build an optimizer for each model we
@@ -100,7 +98,7 @@ def forward(self, x):
10098

10199

102100
######################################################################
103-
# **4) Save multiple models**
101+
# 4. Save multiple models
104102
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
105103
#
106104
# Collect all relevant information and build your dictionary.
@@ -118,7 +116,7 @@ def forward(self, x):
118116

119117

120118
######################################################################
121-
# **5) Load multiple models**
119+
# 4. Load multiple models
122120
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
123121
#
124122
# Remember to first initialize the models and optimizers, then load the
@@ -161,4 +159,4 @@ def forward(self, x):
161159
#
162160
# - TBD
163161
# - TBD
164-
#
162+
#

0 commit comments

Comments
 (0)