Skip to content

Commit b08d881

Browse files
committed
Rebuild
1 parent ec75202 commit b08d881

File tree

147 files changed

+8538
-8888
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+8538
-8888
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
"""
2-
Zeroing out gradients in PyTorch
2+
PyTorch์—์„œ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ
33
================================
4-
It is beneficial to zero out gradients when building a neural network.
5-
This is because by default, gradients are accumulated in buffers (i.e,
6-
not overwritten) whenever ``.backward()`` is called.
4+
์‹ ๊ฒฝ๋ง์„ ๊ตฌ์ถ•ํ•  ๋•Œ๋Š” ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ
5+
``.backward()`` ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค ๋ณ€ํ™”๋„๊ฐ€ ๋ฒ„ํผ์— ์Œ“์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (๋ฎ์–ด์“ฐ์ง€ ์•Š๋Š”๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.)
76
8-
Introduction
7+
๊ฐœ์š”
98
------------
10-
When training your neural network, models are able to increase their
11-
accuracy through gradient decent. In short, gradient descent is the
12-
process of minimizing our loss (or error) by tweaking the weights and
13-
biases in our model.
14-
15-
``torch.Tensor`` is the central class of PyTorch. When you create a
16-
tensor, if you set its attribute ``.requires_grad`` as ``True``, the
17-
package tracks all operations on it. This happens on subsequent backward
18-
passes. The gradient for this tensor will be accumulated into ``.grad``
19-
attribute. The accumulation (or sum) of all the gradients is calculated
20-
when .backward() is called on the loss tensor.
21-
22-
There are cases where it may be necessary to zero-out the gradients of a
23-
tensor. For example: when you start your training loop, you should zero
24-
out the gradients so that you can perform this tracking correctly.
25-
In this recipe, we will learn how to zero out gradients using the
26-
PyTorch library. We will demonstrate how to do this by training a neural
27-
network on the ``CIFAR10`` dataset built into PyTorch.
28-
29-
Setup
9+
์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ, ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์„ ๊ฑฐ์ณ ๋ชจ๋ธ ์ •ํ™•๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ์‚ฌ ํ•˜๊ฐ•๋ฒ•์€ ๊ฐ„๋‹จํžˆ
10+
์„ค๋ช…ํ•ด ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜์™€ ํŽธํ–ฅ์„ ์•ฝ๊ฐ„์”ฉ ์ˆ˜์ •ํ•˜๋ฉด์„œ ์†์‹ค(๋˜๋Š” ์˜ค๋ฅ˜)๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.
11+
12+
``torch.Tensor`` ๋Š” PyTorch ์˜ ํ•ต์‹ฌ์ด ๋˜๋Š” ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค. ํ…์„œ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ
13+
``.requires_grad`` ์†์„ฑ์„ ``True`` ๋กœ ์„ค์ •ํ•˜๋ฉด, ํ…์„œ์— ๊ฐ€ํ•ด์ง„ ๋ชจ๋“  ์—ฐ์‚ฐ์„ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.
14+
๋’ค๋”ฐ๋ฅด๋Š” ๋ชจ๋“  ์—ญ์ „ํŒŒ ๋‹จ๊ณ„์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์ด ํ…์„œ์˜ ๋ณ€ํ™”๋„๋Š” ``.grad`` ์†์„ฑ์— ๋ˆ„์ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
15+
๋ชจ๋“  ๋ณ€ํ™”๋„์˜ ์ถ•์  ๋˜๋Š” ํ•ฉ์€ ์†์‹ค ํ…์„œ์—์„œ ``.backward()``๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค.
16+
17+
ํ…์„œ์˜ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํ•™์Šต ๊ณผ์ • ๋ฐ˜๋ณต๋ฌธ์„
18+
์‹œ์ž‘ํ•  ๋•Œ, ๋ˆ„์ ๋˜๋Š” ๋ณ€ํ™”๋„๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์ถ”์ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ€ํ™”๋„๋ฅผ ์šฐ์„  0์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
19+
์ด ๋ ˆ์‹œํ”ผ์—์„œ๋Š” PyTorch ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ด…๋‹ˆ๋‹ค.
20+
PyTorch์— ๋‚ด์žฅ๋œ ``CIFAR10`` ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•˜์—ฌ ์‹ ๊ฒฝ๋ง์„ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค.
21+
22+
์„ค์ •
3023
-----
31-
Since we will be training data in this recipe, if you are in a runable
32-
notebook, it is best to switch the runtime to GPU or TPU.
33-
Before we begin, we need to install ``torch`` and ``torchvision`` if
34-
they arenโ€™t already available.
24+
์ด ๋ ˆ์‹œํ”ผ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•™์Šต์‹œํ‚ค๋Š” ๋‚ด์šฉ์ด ํฌํ•จ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋…ธํŠธ๋ถ ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด
25+
๋Ÿฐํƒ€์ž„์„ GPU ๋˜๋Š” TPU๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ, ``torch`` ์™€
26+
``torchvision`` ํŒจํ‚ค์ง€๊ฐ€ ์—†๋‹ค๋ฉด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
3527
3628
::
3729
@@ -42,24 +34,22 @@
4234

4335

4436
######################################################################
45-
# Steps
37+
# ๋‹จ๊ณ„(Steps)
4638
# -----
4739
#
48-
# Steps 1 through 4 set up our data and neural network for training. The
49-
# process of zeroing out the gradients happens in step 5. If you already
50-
# have your data and neural network built, skip to 5.
40+
# 1๋‹จ๊ณ„๋ถ€ํ„ฐ 4๋‹จ๊ณ„๊นŒ์ง€๋Š” ํ•™์Šต์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์™€ ์‹ ๊ฒฝ๋ง์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. 5๋‹จ๊ณ„์—์„œ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ
41+
# ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค. ์ด๋ฏธ ์ค€๋น„ํ•œ ๋ฐ์ดํ„ฐ์™€ ์‹ ๊ฒฝ๋ง์ด ์žˆ๋‹ค๋ฉด, 5๋‹จ๊ณ„๋กœ ๊ฑด๋„ˆ๋›ฐ์–ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค.
5142
#
52-
# 1. Import all necessary libraries for loading our data
53-
# 2. Load and normalize the dataset
54-
# 3. Build the neural network
55-
# 4. Define the loss function
56-
# 5. Zero the gradients while training the network
43+
# 1. ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import ํ•˜๊ธฐ
44+
# 2. ๋ฐ์ดํ„ฐ์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์ •๊ทœํ™”ํ•˜๊ธฐ
45+
# 3. ์‹ ๊ฒฝ๋ง ๊ตฌ์ถ•ํ•˜๊ธฐ
46+
# 4. ์†์‹ค ํ•จ์ˆ˜ ์ •์˜ํ•˜๊ธฐ
47+
# 5. ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ ๋ณ€ํ™”๋„ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ
5748
#
58-
# 1. Import necessary libraries for loading our data
49+
# 1. ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ import ํ•˜๊ธฐ
5950
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6051
#
61-
# For this recipe, we will just be using ``torch`` and ``torchvision`` to
62-
# access the dataset.
52+
# ์ด ๋ ˆ์‹œํ”ผ์—์„œ๋Š” ๋ฐ์ดํ„ฐ์…‹์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ``torch`` ์™€ ``torchvision`` ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
6353
#
6454

6555
import torch
@@ -74,11 +64,11 @@
7464

7565

7666
######################################################################
77-
# 2. Load and normalize the dataset
67+
# 2. ๋ฐ์ดํ„ฐ์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์ •๊ทœํ™”ํ•˜๊ธฐ
7868
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7969
#
80-
# PyTorch features various built-in datasets (see the Loading Data recipe
81-
# for more information).
70+
# PyTorch ๋Š” ๋‹ค์–‘ํ•œ ๋‚ด์žฅ ๋ฐ์ดํ„ฐ์…‹์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. (Loading Data ๋ ˆ์‹œํ”ผ๋ฅผ ์ฐธ๊ณ ํ•ด
71+
# ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
8272
#
8373

8474
transform = transforms.Compose(
@@ -100,11 +90,11 @@
10090

10191

10292
######################################################################
103-
# 3. Build the neural network
93+
# 3. ์‹ ๊ฒฝ๋ง ๊ตฌ์ถ•ํ•˜๊ธฐ
10494
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10595
#
106-
# We will use a convolutional neural network. To learn more see the
107-
# Defining a Neural Network recipe.
96+
# ์ปจ๋ณผ๋ฃจ์…˜ ์‹ ๊ฒฝ๋ง์„ ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Defining a Neural Network ๋ ˆ์‹œํ”ผ๋ฅผ
97+
# ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”.
10898
#
10999

110100
class Net(nn.Module):
@@ -128,10 +118,10 @@ def forward(self, x):
128118

129119

130120
######################################################################
131-
# 4. Define a Loss function and optimizer
121+
# 4. ์†์‹ค ํ•จ์ˆ˜๊ณผ ์˜ตํ‹ฐ๋งˆ์ด์ € ์ •์˜ํ•˜๊ธฐ
132122
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133123
#
134-
# Letโ€™s use a Classification Cross-Entropy loss and SGD with momentum.
124+
# ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•œ Cross-Entropy ์†์‹ค ํ•จ์ˆ˜์™€ ๋ชจ๋ฉ˜ํ…€์„ ์„ค์ •ํ•œ SGD ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
135125
#
136126

137127
net = Net()
@@ -140,36 +130,36 @@ def forward(self, x):
140130

141131

142132
######################################################################
143-
# 5. Zero the gradients while training the network
133+
# 5. ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚ค๋Š” ๋™์•ˆ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ
144134
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145135
#
146-
# This is when things start to get interesting. We simply have to loop
147-
# over our data iterator, and feed the inputs to the network and optimize.
136+
# ์ด์ œ๋ถ€ํ„ฐ ํฅ๋ฏธ๋กœ์šด ๋ถ€๋ถ„์„ ์‚ดํŽด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
137+
# ์—ฌ๊ธฐ์„œ ํ•  ์ผ์€ ๋ฐ์ดํ„ฐ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ์‹ ๊ฒฝ๋ง์— ์ž…๋ ฅ์„ ์ฃผ๊ณ 
138+
# ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
148139
#
149-
# Notice that for each entity of data, we zero out the gradients. This is
150-
# to ensure that we arenโ€™t tracking any unnecessary information when we
151-
# train our neural network.
140+
# ๋ฐ์ดํ„ฐ์˜ ์—”ํ„ฐํ‹ฐ ๊ฐ๊ฐ์˜ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์— ์œ ์˜ํ•˜์‹ญ์‹œ์˜ค.
141+
# ์‹ ๊ฒฝ๋ง์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ถ”์ ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.
152142
#
153143

154-
for epoch in range(2): # loop over the dataset multiple times
144+
for epoch in range(2): # ์ „์ฒด ๋ฐ์ดํ„ฐ์…‹์„ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ธฐ
155145

156146
running_loss = 0.0
157147
for i, data in enumerate(trainloader, 0):
158-
# get the inputs; data is a list of [inputs, labels]
148+
# ์ž…๋ ฅ ๋ฐ›๊ธฐ: ๋ฐ์ดํ„ฐ๋Š” [inputs, labels] ํ˜•ํƒœ์˜ ๋ฆฌ์ŠคํŠธ
159149
inputs, labels = data
160150

161-
# zero the parameter gradients
151+
# ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ
162152
optimizer.zero_grad()
163153

164-
# forward + backward + optimize
154+
# ์ˆœ์ „ํŒŒ + ์—ญ์ „ํŒŒ + ์ตœ์ ํ™”
165155
outputs = net(inputs)
166156
loss = criterion(outputs, labels)
167157
loss.backward()
168158
optimizer.step()
169159

170-
# print statistics
160+
# ํ†ต๊ณ„ ์ถœ๋ ฅ
171161
running_loss += loss.item()
172-
if i % 2000 == 1999: # print every 2000 mini-batches
162+
if i % 2000 == 1999: # ๋ฏธ๋‹ˆ๋ฐฐ์น˜ 2000๊ฐœ ๋งˆ๋‹ค ์ถœ๋ ฅ
173163
print('[%d, %5d] loss: %.3f' %
174164
(epoch + 1, i + 1, running_loss / 2000))
175165
running_loss = 0.0
@@ -178,16 +168,16 @@ def forward(self, x):
178168

179169

180170
######################################################################
181-
# You can also use ``model.zero_grad()``. This is the same as using
182-
# ``optimizer.zero_grad()`` as long as all your model parameters are in
183-
# that optimizer. Use your best judgement to decide which one to use.
171+
# ``model.zero_grad()`` ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
172+
# ์ด๋Š” ์˜ตํ‹ฐ๋งˆ์ด์ €์— ๋ชจ๋“  ๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํฌํ•จ๋˜๋Š” ํ•œ ``optimizer.zero_grad()`` ๋ฅผ
173+
# ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒƒ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ตœ์„ ์˜ ์„ ํƒ์„ ํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.
184174
#
185-
# Congratulations! You have successfully zeroed out gradients PyTorch.
175+
# ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ์ด์ œ PyTorch์—์„œ ๋ณ€ํ™”๋„๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
186176
#
187-
# Learn More
177+
# ๋” ์•Œ์•„๋ณด๊ธฐ
188178
# ----------
189179
#
190-
# Take a look at these other recipes to continue your learning:
180+
# ๋‹ค๋ฅธ ๋ ˆ์‹œํ”ผ๋ฅผ ๋‘˜๋Ÿฌ๋ณด๊ณ  ๊ณ„์† ๋ฐฐ์›Œ๋ณด์„ธ์š”:
191181
#
192-
# - `Loading data in PyTorch <https://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html>`__
193-
# - `Saving and loading models across devices in PyTorch <https://pytorch.org/tutorials/recipes/recipes/save_load_across_devices.html>`__
182+
# - :doc:`/recipes/recipes/loading_data_recipe`
183+
# - :doc:`/recipes/recipes/save_load_across_devices`

0 commit comments

Comments
ย (0)