Skip to content

Commit df2be6a

Browse files
tastymineralsholly1238
andauthored
Add an additional "Note" (#1000)
Add information about potential pitfall of not serializing the model state and keeping it as a reference during training. Co-authored-by: holly1238 <[email protected]>
1 parent f13d5e1 commit df2be6a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

beginner_source/saving_loading_models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@
183183
# ``load_state_dict()`` function. For example, you CANNOT load using
184184
# ``model.load_state_dict(PATH)``.
185185
#
186+
# .. Note ::
187+
#
188+
# If you only plan to keep the best performing model (according to the
189+
# acquired validation loss), don't forget that ``best_model_state = model.state_dict()``
190+
# returns a reference to the state and not its copy! You must serialize
191+
# ``best_model_state`` or use ``best_model_state = deepcopy(model.state_dict())`` otherwise
192+
# your best ``best_model_state`` will keep getting updated by the subsequent training
193+
# iterations. As a result, the final model state will be the state of the overfitted model.
186194
#
187195
# Save/Load Entire Model
188196
# ^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)