๐ ์ผ๋ผ์ค ์ฐฝ์์์๊ฒ ๋ฐฐ์ฐ๋ ๋ฅ๋ฌ๋ (ํ๋์์ ์๋ , ๋ฐํด์ , ๊ธธ๋ฒ) ์ฐธ๊ณ
Tensor๋ Neural Network ํ์ต์ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ๋จ์์ด๋ค. Numeric(์์นํ) ๋ฐ์ดํฐ๋ฅผ ๋ด๊ธฐ ์ํ Container์ด๋ค. ํ ์๋ ์์์ ์ฐจ์ ํน์ Rank(์ถ)์ ๊ฐ์ง๋ค.
ํ๋์ ์ซ์๋ง ๋ด๊ณ ์๋ ํ ์๋ฅผ 0D Tensor, ์ค์นผ๋ผ๋ผ๊ณ ํ๋ค. ์ค์นผ๋ผ ํ ์๋ ์ถ์ด 0๊ฐ์ด๋ค.
>>> x = np.array(7)
>>> x.ndim
0
1D Tensor, ๋ฒกํฐ๋ ์ซ์์ ๋ฐฐ์ด์ ์๋ฏธํ๋ค. ๋ฒกํฐ๋ 1๊ฐ์ ์ถ์ ๊ฐ์ง๋ค.
>>> x = np.array([1, 2, 3, 4, 5])
>>> x.ndim
1
์์ ๋ฒกํฐ๋ 5๊ฐ์ ์์๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ 5D ๋ฒกํฐ, 5์ฐจ์ ๋ฒกํฐ๋ผ๊ณ ํ ์ ์๋ค. 5D ํ
์์ ํท๊ฐ๋ฆฌ์ง ๋ง์
2D Tensor, ํ๋ ฌ์ ๋ฒกํฐ์ ๋ฐฐ์ด์ด๋ค. 2๊ฐ์ ์ถ์ ๊ฐ์ง๊ณ ์๊ณ row(ํ)์ column(์ด)์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
>>> x = np.array([[1, 3, 5, 7, 9],
[2, 4, 6, 8, 10],
[11, 12, 13, 14, 15]])
>>> x.ndim
2
ํ๋ ฌ์ ํ๋์ ๋ฐฐ์ด๋ก ํฉ์น ๊ฒ์ด 3D Tensor์ด๋ค.
>>> x = np.array([[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]],
[[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]],
[[41, 42, 43, 44, 45],
[46, 47, 48, 49, 50],
[51, 52, 53, 54, 55]]])
>>> x.ndim
3
3D Tensor๋ฅผ ํ๋์ ๋ฐฐ์ด๋ก ํฉ์น๋ฉด 4D Tensor๊ฐ ๋๊ณ , 4D Tensor๋ฅผ ๋ค์ ํ๋์ ๋ฐฐ์ด๋ก ํฉ์น๋ฉด 5D Tensor๊ฐ ๋๋ค.
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] += y[i, j]
# ์์๋ณ ๋ง์
z = x + y
์์ ํ ์๊ฐ ํฐ ํ ์์ ํฌ๊ธฐ์ ๋ง์ถ์ด์ง๋ ๊ฒ
# x๋ 2D ๋ํ์ด ๋ฐฐ์ด
# y๋ ๋ํ์ด ๋ฐฐ์ด
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] += y[j]
import numpy as np
z = np.dot(x, y)
ํน์ ํฌ๊ธฐ์ ๋ง๊ฒ ์ด๊ณผ ํ์ ์ฌ๋ฐฐ์ด
x = x.reshape(1, 2)
ํ๊ณผ ์ด์ ๋ฐ๊พธ๋ ์ ์น(transposition)
x = np.transpose(x)