Skip to content

Commit bb04c46

Browse files
committed
Rebuild
1 parent 425c3e9 commit bb04c46

File tree

4 files changed

+59
-64
lines changed

4 files changed

+59
-64
lines changed

โ€Ždocs/_downloads/7d5771891cc8e2f733352e4fc8fc63b6/saving_loading_models.py

+27-29
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
1414
1) `torch.save <https://pytorch.org/docs/stable/torch.html?highlight=save#torch.save>`__:
1515
์ง๋ ฌํ™”๋œ ๊ฐ์ฒด๋ฅผ ๋””์Šคํฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” Python์˜
16-
`pickle <https://docs.python.org/3/library/pickle.html>`__ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค
16+
`pickle <https://docs.python.org/3/library/pickle.html>`__ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค.
1717
์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๊ฐ์ฒด์˜ ๋ชจ๋ธ, Tensor ๋ฐ ์‚ฌ์ „์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
1818
1919
2) `torch.load <https://pytorch.org/docs/stable/torch.html?highlight=torch%20load#torch.load>`__:
2020
`pickle <https://docs.python.org/3/library/pickle.html>`__\ ์„ ์‚ฌ์šฉํ•˜์—ฌ
21-
์ €์žฅ๋œ ๊ฐ์ฒด ํŒŒ์ผ๋“ค์„ ์—ญ์ง๋ ฌํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์žฅ์น˜์—
22-
๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
23-
(`์žฅ์น˜๊ฐ„ ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ <#device>`__ ์ฐธ๊ณ )
21+
์ €์žฅ๋œ ๊ฐ์ฒด ํŒŒ์ผ๋“ค์„ ์—ญ์ง๋ ฌํ™”ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์žฅ์น˜์— ๋ถˆ๋Ÿฌ์˜ฌ
22+
๋•Œ์—๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
23+
(`์žฅ์น˜ ๊ฐ„ ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ <#device>`__ ์ฐธ๊ณ )
2424
2525
3) `torch.nn.Module.load_state_dict <https://pytorch.org/docs/stable/generated/torch.nn.Module.html?highlight=load_state_dict#torch.nn.Module.load_state_dict>`__:
2626
์—ญ์ง๋ ฌํ™”๋œ *state_dict* ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
@@ -145,29 +145,29 @@
145145
# model.eval()
146146
#
147147
# .. note::
148-
# PyTorch ๋ฒ„์ „ 1.6์—์„œ๋Š” ์ƒˆ๋กœ์šด ZipํŒŒ์ผ-๊ธฐ๋ฐ˜์˜ ํŒŒ์ผ ํฌ๋งท์„ ์‚ฌ์šฉํ•˜๋„๋ก
149-
# ``torch.save`` ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ``torch.load`` ๋Š” ์˜ˆ์ „ ๋ฐฉ์‹์˜ ํŒŒ์ผ๋“ค์„
148+
# PyTorch ๋ฒ„์ „ 1.6์—์„œ๋Š” ``torch.save`` ๊ฐ€ ์ƒˆ๋กœ์šด ZipํŒŒ์ผ-๊ธฐ๋ฐ˜์˜ ํŒŒ์ผ
149+
# ํฌ๋งท์„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ``torch.load`` ๋Š” ์˜ˆ์ „ ๋ฐฉ์‹์˜ ํŒŒ์ผ๋“ค์„
150150
# ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ์ด์œ ์—์„œ๋“  ``torch.save`` ๊ฐ€ ์˜ˆ์ „
151151
# ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ``_use_new_zipfile_serialization=False`` ์„
152152
# kwarg๋กœ ์ „๋‹ฌํ•˜์„ธ์š”.
153153
#
154-
# ์ถ”๋ก ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ๋Š” ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ํ•™์Šต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
154+
# ์ถ”๋ก ์„ ์œ„ํ•ด ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ๋Š” ๊ทธ ๋ชจ๋ธ์˜ ํ•™์Šต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์ €์žฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
155155
# ``torch.save()`` ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ์˜ *state_dict* ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์ค‘์— ๋ชจ๋ธ์„
156156
# ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ€์žฅ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”, ๋ชจ๋ธ ์ €์žฅ ์‹œ ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
157157
#
158158
# PyTorch์—์„œ๋Š” ๋ชจ๋ธ์„ ์ €์žฅํ•  ๋•Œ ``.pt`` ๋˜๋Š” ``.pth`` ํ™•์žฅ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด
159159
# ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์ž…๋‹ˆ๋‹ค.
160160
#
161-
# ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์—๋Š” ๋ฐ˜๋“œ์‹œ ``model.eval()`` ์„ ํ˜ธ์ถœํ•˜์—ฌ ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜
162-
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ผ๊ด€์„ฑ
163-
# ์—†๊ฒŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
161+
# ์ถ”๋ก ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ``model.eval()`` ์„ ํ˜ธ์ถœํ•˜์—ฌ ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜
162+
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š์œผ๋ฉด ์ผ๊ด€์„ฑ ์—†๋Š”
163+
# ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
164164
#
165165
# .. Note ::
166166
#
167167
# ``load_state_dict()`` ํ•จ์ˆ˜์—๋Š” ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹Œ, ์‚ฌ์ „ ๊ฐ์ฒด๋ฅผ
168168
# ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์„ธ์š”. ๋”ฐ๋ผ์„œ ์ €์žฅ๋œ *state_dict* ๋ฅผ ``load_state_dict()``
169169
# ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,
170-
# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ๋Š” ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
170+
# ``model.load_state_dict(PATH)`` ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
171171
#
172172
# .. Note ::
173173
#
@@ -192,7 +192,7 @@
192192
#
193193
# .. code:: python
194194
#
195-
# # ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” ์–ด๋”˜๊ฐ€์— ๋ฐ˜๋“œ์‹œ ์„ ์–ธ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
195+
# # ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” ์–ด๋”˜๊ฐ€์— ๋ฐ˜๋“œ์‹œ ์„ ์–ธ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
196196
# model = torch.load(PATH)
197197
# model.eval()
198198
#
@@ -213,25 +213,23 @@
213213
# ์ •๊ทœํ™”๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ์ผ๊ด€์„ฑ
214214
# ์—†๊ฒŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
215215
#
216-
# Export/Load Model in TorchScript Format
216+
# TorchScript ํฌ๋งท์œผ๋กœ ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ/๊ฐ€์ ธ์˜ค๊ธฐ
217217
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
218218
#
219-
# One common way to do inference with a trained model is to use
220-
# `TorchScript <https://pytorch.org/docs/stable/jit.html>`__, an intermediate
221-
# representation of a PyTorch model that can be run in Python as well as in a
222-
# high performance environment like C++. TorchScript is actually the recommended model format
223-
# for scaled inference and deployment.
219+
# ํ›ˆ๋ จ๋œ ๋ชจ๋ธ๋กœ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” `TorchScript <https://pytorch.org/docs/stable/jit.html>`__ ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
220+
# TorchScript๋Š” ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์ด๋‚˜ C++์™€ ๊ฐ™์€ ๊ณ ์„ฑ๋Šฅ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”
221+
# ํŒŒ์ดํ† ์น˜ ๋ชจ๋ธ์˜ ์ค‘๊ฐ„ ํ‘œํ˜„(IR; Intermediate Representation)์ž…๋‹ˆ๋‹ค.
222+
# TorchScript๋Š” ํ™•์žฅ๋œ ์ถ”๋ก  ๋ฐ ๋ฐฐํฌ์— ๊ถŒ์žฅ๋˜๋Š” ๋ชจ๋ธ ํ˜•์‹์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.
224223
#
225224
# .. note::
226-
# Using the TorchScript format, you will be able to load the exported model and
227-
# run inference without defining the model class.
225+
# TorchScript ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๊ณ ๋„ ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์„ ์ฝ์–ด ์˜ค๊ฑฐ๋‚˜ ์ถ”๋ก ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
228226
#
229227
# **Export:**
230228
#
231229
# .. code:: python
232230
#
233-
# model_scripted = torch.jit.script(model) # Export to TorchScript
234-
# model_scripted.save('model_scripted.pt') # Save
231+
# model_scripted = torch.jit.script(model) # TorchScript ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ
232+
# model_scripted.save('model_scripted.pt') # ์ €์žฅํ•˜๊ธฐ
235233
#
236234
# **Load:**
237235
#
@@ -240,14 +238,14 @@
240238
# model = torch.jit.load('model_scripted.pt')
241239
# model.eval()
242240
#
243-
# Remember that you must call ``model.eval()`` to set dropout and batch
244-
# normalization layers to evaluation mode before running inference.
245-
# Failing to do this will yield inconsistent inference results.
241+
# ์ถ”๋ก  ์‹คํ–‰ ์ „, ๋“œ๋กญ์•„์›ƒ ๋ฐ ๋ฐฐ์น˜(batch) ์ •๊ทœํ™” ๋ ˆ์ด์–ด๋ฅผ ํ‰๊ฐ€ ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ``model.eval()`` ์„ ํ˜ธ์ถœํ•ด์•ผ
242+
# ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ˜ธ์ถœ ๊ณผ์ •์ด ์—†์œผ๋ฉด ์ผ๊ด€์„ฑ ์—†๋Š” ์ถ”๋ก  ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
246243
#
247-
# For more information on TorchScript, feel free to visit the dedicated
248-
# `tutorials <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__.
249-
# You will get familiar with the tracing conversion and learn how to
250-
# run a TorchScript module in a `C++ environment <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__.
244+
# TorchScript์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ •๋ณด๋Š” ์ „์šฉ
245+
# `์ž์Šต์„œ <https://tutorials.pytorch.kr/beginner/Intro_to_TorchScript_tutorial.html>`__ ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
246+
# `C++ ํ™˜๊ฒฝ <https://tutorials.pytorch.kr/advanced/cpp_export.html>`__ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํŠธ๋ ˆ์ด์‹ฑ(Tracing) ๋ณ€ํ™˜์„
247+
# ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ C++ ํ™˜๊ฒฝ์—์„œ TorchScript ๋ชจ๋“ˆ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ตํž ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
248+
251249

252250

253251
######################################################################

0 commit comments

Comments
ย (0)