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)