01_tensor_autograd.py

Download
python 243 lines 6.1 KB
  1"""
  201. ํ…์„œ์™€ ์˜คํ† ๊ทธ๋ž˜๋“œ - PyTorch ๋ฒ„์ „
  3
  4PyTorch์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์ธ ํ…์„œ ์—ฐ์‚ฐ๊ณผ ์ž๋™ ๋ฏธ๋ถ„์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค.
  5NumPy ๋ฒ„์ „(examples/numpy/01_tensor_basics.py)๊ณผ ๋น„๊ตํ•ด ๋ณด์„ธ์š”.
  6"""
  7
  8import torch
  9import numpy as np
 10
 11print("=" * 60)
 12print("PyTorch ํ…์„œ์™€ ์˜คํ† ๊ทธ๋ž˜๋“œ")
 13print("=" * 60)
 14
 15
 16# ============================================
 17# 1. ํ…์„œ ์ƒ์„ฑ
 18# ============================================
 19print("\n[1] ํ…์„œ ์ƒ์„ฑ")
 20print("-" * 40)
 21
 22# ๋ฆฌ์ŠคํŠธ์—์„œ ์ƒ์„ฑ
 23tensor1 = torch.tensor([1, 2, 3, 4])
 24print(f"๋ฆฌ์ŠคํŠธ โ†’ ํ…์„œ: {tensor1}")
 25print(f"  shape: {tensor1.shape}, dtype: {tensor1.dtype}")
 26
 27# ํŠน์ˆ˜ ํ…์„œ
 28zeros = torch.zeros(3, 4)
 29ones = torch.ones(2, 3)
 30rand = torch.randn(2, 3)  # ํ‘œ์ค€ ์ •๊ทœ ๋ถ„ํฌ
 31arange = torch.arange(0, 10, 2)
 32
 33print(f"zeros(3,4): shape {zeros.shape}")
 34print(f"randn(2,3):\n{rand}")
 35
 36# dtype ์ง€์ •
 37float_tensor = torch.tensor([1, 2, 3], dtype=torch.float32)
 38print(f"float32 ํ…์„œ: {float_tensor}")
 39
 40
 41# ============================================
 42# 2. NumPy์™€ ๋ณ€ํ™˜
 43# ============================================
 44print("\n[2] NumPy ๋ณ€ํ™˜")
 45print("-" * 40)
 46
 47# NumPy โ†’ PyTorch
 48np_arr = np.array([1.0, 2.0, 3.0])
 49torch_from_np = torch.from_numpy(np_arr)
 50print(f"NumPy โ†’ PyTorch: {torch_from_np}")
 51
 52# ์ฃผ์˜: ๋ฉ”๋ชจ๋ฆฌ ๊ณต์œ ๋จ
 53np_arr[0] = 100
 54print(f"NumPy ์ˆ˜์ • ํ›„ PyTorch: {torch_from_np}")  # ๊ฐ™์ด ๋ณ€๊ฒฝ๋จ
 55
 56# PyTorch โ†’ NumPy
 57pt_tensor = torch.tensor([4.0, 5.0, 6.0])
 58np_from_torch = pt_tensor.numpy()
 59print(f"PyTorch โ†’ NumPy: {np_from_torch}")
 60
 61
 62# ============================================
 63# 3. ํ…์„œ ์—ฐ์‚ฐ
 64# ============================================
 65print("\n[3] ํ…์„œ ์—ฐ์‚ฐ")
 66print("-" * 40)
 67
 68a = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
 69b = torch.tensor([[5, 6], [7, 8]], dtype=torch.float32)
 70
 71# ์š”์†Œ๋ณ„ ์—ฐ์‚ฐ
 72print(f"a + b:\n{a + b}")
 73print(f"a * b (์š”์†Œ๋ณ„):\n{a * b}")
 74
 75# ํ–‰๋ ฌ ๊ณฑ์…ˆ
 76print(f"a @ b (ํ–‰๋ ฌ ๊ณฑ):\n{a @ b}")
 77print(f"torch.matmul(a, b):\n{torch.matmul(a, b)}")
 78
 79# ํ†ต๊ณ„
 80print(f"a.sum(): {a.sum()}")
 81print(f"a.mean(): {a.mean()}")
 82print(f"a.max(): {a.max()}")
 83
 84
 85# ============================================
 86# 4. ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…
 87# ============================================
 88print("\n[4] ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…")
 89print("-" * 40)
 90
 91x = torch.tensor([[1], [2], [3]])  # (3, 1)
 92y = torch.tensor([10, 20, 30])     # (3,)
 93
 94result = x + y  # (3, 3)์œผ๋กœ ์ž๋™ ํ™•์žฅ
 95print(f"x shape: {x.shape}")
 96print(f"y shape: {y.shape}")
 97print(f"x + y shape: {result.shape}")
 98print(f"x + y:\n{result}")
 99
100
101# ============================================
102# 5. ์ž๋™ ๋ฏธ๋ถ„ (Autograd) ๊ธฐ์ดˆ
103# ============================================
104print("\n[5] ์ž๋™ ๋ฏธ๋ถ„ (Autograd)")
105print("-" * 40)
106
107# requires_grad=True๋กœ ๋ฏธ๋ถ„ ์ถ”์  ํ™œ์„ฑํ™”
108x = torch.tensor([2.0], requires_grad=True)
109print(f"x: {x}, requires_grad: {x.requires_grad}")
110
111# ์ˆœ์ „ํŒŒ
112y = x ** 2 + 3 * x + 1  # y = xยฒ + 3x + 1
113print(f"y = xยฒ + 3x + 1 = {y.item()}")
114
115# ์—ญ์ „ํŒŒ
116y.backward()
117
118# ๊ธฐ์šธ๊ธฐ ํ™•์ธ (dy/dx = 2x + 3 = 2*2 + 3 = 7)
119print(f"dy/dx at x=2: {x.grad.item()}")
120print("๊ฒ€์ฆ: dy/dx = 2x + 3 = 2*2 + 3 = 7 โœ“")
121
122
123# ============================================
124# 6. ๋ณต์žกํ•œ ํ•จ์ˆ˜์˜ ์ž๋™ ๋ฏธ๋ถ„
125# ============================================
126print("\n[6] ๋ณต์žกํ•œ ํ•จ์ˆ˜ ๋ฏธ๋ถ„")
127print("-" * 40)
128
129# f(x) = xยณ + 2xยฒ - 5x + 3
130# f'(x) = 3xยฒ + 4x - 5
131# f'(2) = 12 + 8 - 5 = 15
132
133x = torch.tensor([2.0], requires_grad=True)
134f = x**3 + 2*x**2 - 5*x + 3
135
136f.backward()
137print(f"f(x) = xยณ + 2xยฒ - 5x + 3")
138print(f"f(2) = {f.item()}")
139print(f"f'(2) = {x.grad.item()}")
140print("๊ฒ€์ฆ: f'(x) = 3xยฒ + 4x - 5 = 12 + 8 - 5 = 15 โœ“")
141
142
143# ============================================
144# 7. ๋‹ค๋ณ€์ˆ˜ ํ•จ์ˆ˜์˜ ๋ฏธ๋ถ„ (Gradient)
145# ============================================
146print("\n[7] ๋‹ค๋ณ€์ˆ˜ ํ•จ์ˆ˜ ๋ฏธ๋ถ„")
147print("-" * 40)
148
149# f(x, y) = xยฒ + yยฒ + xy
150# โˆ‚f/โˆ‚x = 2x + y
151# โˆ‚f/โˆ‚y = 2y + x
152
153x = torch.tensor([3.0], requires_grad=True)
154y = torch.tensor([4.0], requires_grad=True)
155
156f = x**2 + y**2 + x*y
157f.backward()
158
159print(f"f(x, y) = xยฒ + yยฒ + xy")
160print(f"f(3, 4) = {f.item()}")
161print(f"โˆ‚f/โˆ‚x at (3,4) = {x.grad.item()}")  # 2*3 + 4 = 10
162print(f"โˆ‚f/โˆ‚y at (3,4) = {y.grad.item()}")  # 2*4 + 3 = 11
163
164
165# ============================================
166# 8. ๊ธฐ์šธ๊ธฐ ์ดˆ๊ธฐํ™”
167# ============================================
168print("\n[8] ๊ธฐ์šธ๊ธฐ ์ดˆ๊ธฐํ™”")
169print("-" * 40)
170
171x = torch.tensor([1.0], requires_grad=True)
172
173# ์ฒซ ๋ฒˆ์งธ ์—ญ์ „ํŒŒ
174y1 = x * 2
175y1.backward()
176print(f"์ฒซ ๋ฒˆ์งธ grad: {x.grad}")
177
178# ๊ธฐ์šธ๊ธฐ๊ฐ€ ๋ˆ„์ ๋จ!
179y2 = x * 3
180y2.backward()
181print(f"๋ˆ„์ ๋œ grad: {x.grad}")  # 2 + 3 = 5
182
183# ์ดˆ๊ธฐํ™” ํ›„ ๋‹ค์‹œ
184x.grad.zero_()  # ์ค‘์š”!
185y3 = x * 4
186y3.backward()
187print(f"์ดˆ๊ธฐํ™” ํ›„ grad: {x.grad}")
188
189
190# ============================================
191# 9. GPU ์—ฐ์‚ฐ
192# ============================================
193print("\n[9] GPU ์—ฐ์‚ฐ")
194print("-" * 40)
195
196device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
197print(f"์‚ฌ์šฉ ๋””๋ฐ”์ด์Šค: {device}")
198
199if torch.cuda.is_available():
200    print(f"GPU ์ด๋ฆ„: {torch.cuda.get_device_name(0)}")
201    print(f"GPU ๋ฉ”๋ชจ๋ฆฌ: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
202
203    # GPU๋กœ ํ…์„œ ์ด๋™
204    x_cpu = torch.randn(1000, 1000)
205    x_gpu = x_cpu.to(device)
206
207    # GPU์—์„œ ์—ฐ์‚ฐ
208    y_gpu = x_gpu @ x_gpu
209
210    # ๊ฒฐ๊ณผ๋ฅผ CPU๋กœ
211    y_cpu = y_gpu.cpu()
212    print(f"GPU ํ–‰๋ ฌ ๊ณฑ์…ˆ ์™„๋ฃŒ: {y_cpu.shape}")
213else:
214    print("GPU ์‚ฌ์šฉ ๋ถˆ๊ฐ€, CPU ๋ชจ๋“œ๋กœ ์‹คํ–‰")
215
216
217# ============================================
218# 10. no_grad ์ปจํ…์ŠคํŠธ
219# ============================================
220print("\n[10] no_grad ์ปจํ…์ŠคํŠธ")
221print("-" * 40)
222
223x = torch.tensor([1.0], requires_grad=True)
224
225# ์ผ๋ฐ˜ ์—ฐ์‚ฐ (๊ธฐ์šธ๊ธฐ ์ถ”์ )
226y = x * 2
227print(f"์ผ๋ฐ˜ ์—ฐ์‚ฐ: requires_grad = {y.requires_grad}")
228
229# no_grad ๋‚ด๋ถ€ (๊ธฐ์šธ๊ธฐ ์ถ”์  ์•ˆ ํ•จ)
230with torch.no_grad():
231    z = x * 2
232    print(f"no_grad ๋‚ด๋ถ€: requires_grad = {z.requires_grad}")
233
234# detach๋กœ ๋ถ„๋ฆฌ
235w = x.detach() * 2
236print(f"detach ํ›„: requires_grad = {w.requires_grad}")
237
238
239print("\n" + "=" * 60)
240print("PyTorch ํ…์„œ์™€ ์˜คํ† ๊ทธ๋ž˜๋“œ ์™„๋ฃŒ!")
241print("NumPy ๋ฒ„์ „๊ณผ ๋น„๊ต: examples/numpy/01_tensor_basics.py")
242print("=" * 60)