๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Deep Learning/PyTorch

(2)
์ž๋™ ๋ฏธ๋ถ„(Automatic differentiation) ์‚ฌ์šฉ๋ฒ• ํŒŒ์ดํ† ์น˜์˜ ์ž๋™ ๋ฏธ๋ถ„(Auto differentiation)์„ ์ด์šฉํ•œ ๋ณ€ํ™”๋„(Gradient) ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฃฐ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ์ž๋™ ๋ฏธ๋ถ„ ์ค€๋น„ 2. ๋ณ€ํ™”๋„ ๊ณ„์‚ฐ 1. ์ž๋™ ๋ฏธ๋ถ„ ์ค€๋น„ In: import torch x = torch.ones(2, 2, requires_grad = True) print(x) Out: tensor([[1., 1.], [1., 1.]], requires_grad=True) โ–ท torch.ones()์— ํ…์„œ ํฌ๊ธฐ์— ๋Œ€ํ•œ ์ธ์ž์™€ requires_grad ์ธ์ž๋ฅผ ์ฃผ์–ด ํ…์„œ๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. ๊ฒฐ๊ณผ ์ฐฝ์— requires_grad=True๊ฐ€ ๋‚˜ํƒ€๋‚œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Š” ์ดํ›„ ์—ญ์ „ํŒŒ ๊ณผ์ •์„ ์ˆ˜ํ–‰ ํ›„, ํ•ด๋‹น ํ…์„œ์˜ ๋ณ€ํ™”๋„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. In: y = ..
ํ…์„œ(Tensor) ์‚ฌ์šฉ๋ฒ• ํŒŒ์ดํ† ์น˜์˜ ํ…์„œ(Tensor)์˜ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. ๋‹ค๋ฃฐ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ํ…์„œ์˜ ์ƒ์„ฑ 2. ํ…์„œ์˜ ์—ฐ์‚ฐ 3. ํ…์„œ์˜ ๋ณ€ํ™˜ 1. ํ…์„œ์˜ ์ƒ์„ฑ In: import torch x = torch.rand(5, 3) print(x) Out: tensor([[0.1501, 0.8814, 0.4848], [0.0723, 0.9468, 0.1327], [0.8581, 0.8050, 0.4441], [0.4888, 0.0157, 0.6959], [0.9666, 0.4729, 0.1983]]) โ–ท torch.rand()๋ฅผ ์ด์šฉํ•˜์—ฌ 0๊ณผ 1 ์‚ฌ์ด์˜ ์ž„์˜์˜ ์ˆ˜๊ฐ€ ์›์†Œ์ธ 5×3 ํ–‰๋ ฌ์ด ๋งŒ๋“ค์—ˆ๋‹ค. ํ•จ์ˆ˜ ์•ˆ์˜ ๋‘ ์ธ์ž๋Š” ํ–‰๊ณผ ์—ด์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. In: x = torch.rand(5, 3, 3) print(x) Out:..