1D Array & Slicing
# https://www.tensorflow.org/api_guides/python/array_ops
import tensorflow as tf
import numpy as np
import pprint
tf.set_random_seed(777) # for reproducibility
pp = pprint.PrettyPrinter(indent=4)
sess = tf.InteractiveSession()
t = np.array([0., 1., 2., 3., 4., 5., 6.])
pp.pprint(t)
print(t.ndim) # rank = 1
print(t.shape) # shape = (7,)
print(t[0], t[1], t[-1]) # -1์ ๋งจ ๋์ ์๋ฏธ
print(t[2:5], t[4:-1])
print(t[:2], t[3:])
array([0., 1., 2., 3., 4., 5., 6.])
1
(7,)
0.0 1.0 6.0
[2. 3. 4.] [4. 5.]
[0. 1.] [3. 4. 5. 6.]
Shape
shape๋ ์ด ํ ์์ ๋ชจ์์ด ์ด๋ค์ง๋ฅผ ์๋ฏธํ๋ค.
t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]])
pp.pprint(t)
print(t.shape) # shape (4, 3)
shape๋ฅผ ํ๋จํ ๋๋ ๊ฐ์ฅ ์์ชฝ์ ๊ฐ๋ถํฐ ์ธ์ ์ค๋ฅธ์ชฝ ๋๋ถํฐ ์ฑ์ด๋ค.
Rank
t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]])
pp.pprint(t)
print(t.ndim) # rank 2
๊ดํธ์ ๊ฐ์๋ผ๊ณ ์๊ฐํ์!
Axis
[
[
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
],
[
[13,14,15,16],
[17,18,19,20],
[21,22,23,24]
]
]
]
rank์ ๊ฐ์๋งํผ ์ถ์ ๊ฐ์๊ฐ ์๊ธด๋ค. ์ด ๊ฒฝ์ฐ๋ 4์ด๋ค.
์ ์ผ ์์ชฝ์ ์๋ ๊ดํธ๋ถํฐ axis 3(-1์ด๋ผ๊ณ ๋ ํจ)
์ ์ผ ๋ฐ๊นฅ์ชฝ์ ์๋ ๊ดํธ๋ axis 0 ์ด๋ค.
Reduce mean
์์ ์์๋ intํ์ผ๋ก ํ ๊ฒฝ์ฐ float๋ก ์ถ๋ ฅ์๋๋ค!
๋ฐ๋ผ์ 1. 2. ์ด๋ฐ์์ผ๋ก ์จ์ฃผ์.
tf.reduce_mean([1, 2], axis=0).eval() # 1
x = [[1., 2.],
[3., 4.]]
tf.reduce_mean(x).eval() # 2.5
tf.reduce_mean(x, axis=0).eval() # [2. 3.]
tf.reduce_mean(x, axis=1).eval() # [1.5 3.5]
tf.reduce_mean(x, axis=-1).eval() # [1.5 3.5]
reduce sum, Argmax๋ ๊ฐ์ ์๋ฆฌ๋ก ๋์ํ๋ค!
Reshape
t = np.array([[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]]])
t.shape
(2, 2, 3)
# -1์ ๋๋ง์๋๋ก ๋ง๋ค์ด! ๋ผ๋ ์๊ธฐ!
tf.reshape(t, shape=[-1, 3]).eval()
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
Squeeze
tf.squeeze([[0], [1], [2]]).eval()
# array([0, 1, 2], dtype=int32)
Expand
tf.expand_dims([0, 1, 2], 1).eval()
array([[0],
[1],
[2]], dtype=int32)
One-hot
tf.one_hot([[0], [1], [2], [0]], depth=3).eval()
array([[[ 1., 0., 0.]],
[[ 0., 1., 0.]],
[[ 0., 0., 1.]],
[[ 1., 0., 0.]]], dtype=float32)
์๋์ ์ผ๋ก rank๊ฐ ํ๋๊ฐ ์ถ๊ฐ๋๋ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์จ๋ค.
0 ๋์ ์ 0์ ๋ํ๋ด์ฃผ๋ one-hot array๋ก ๋์ฒด๋์๊ธฐ ๋๋ฌธ์ด๋ค.
์ฐ๋ฆฌ๋ [0] ์์ฒด๋ฅผ ๋ฐ๊ฟ์ฃผ๊ณ ์ถ์ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ reshape๋ฅผ ํด์ค๋ค.
t = tf.one_hot([[0], [1], [2], [0]], depth=3)
tf.reshape(t, shape=[-1, 3]).eval()
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 1., 0., 0.]], dtype=float32)
Casting
tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval()
array([1, 2, 3, 4], dtype=int32)
์๋ฃํ์ ๋ณํํด์ ๋์ ธ์ฃผ๋ ์ญํ !
๋ง์ฝ ์์์ ์๋ฃํ๊ณผ ๊ฐ์ ๊ฒ์ ๋๊ฒจ์ฃผ๋ฉด ๊ทธ๋๋ก(as-is)๋ฅผ ๋๊ฒจ์ฃผ๋ ๊ฒ๊ณผ ๋์ผํจ
tf.cast([True, False, 1 == 1, 0 == 1], tf.int32).eval()
array([1, 0, 1, 0], dtype=int32)
์์ ๊ฐ๋ค์ ๋ค ์ซ์๋ก ๋ฐ๊ฟ๋ฒ๋ฆฐ ํ ํฉํ๋ค.
accuracy ์ธก์ ํ ๋ ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์์ฃผ ์ ์ฉํ๋ค!
Stack
x = [1, 4]
y = [2, 5]
z = [3, 6]
# Pack along first dim.
tf.stack([x, y, z]).eval()
array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)
๊ธฐ๋ณธ ์ค์ ์ axis = 0์ผ๋ก ๋์ด ์๋ค.
๊ฐ์ฅ ๋ฐ๊นฅ ๊ดํธ์ ํด๋นํ๋๋ก ์์์ง๋ค.
tf.stack([x, y, z], axis=1).eval()
array([[1, 2, 3],
[4, 5, 6]], dtype=int32)
๊ทธ ๋ค์ ๊ดํธ์์ ๊ฐ์ index์ ์๋ ๋ ์๋ผ๋ฆฌ ๋ฌถ์ธ๋ค.
0์ ์ด 1์ ํ์ด๋ผ ์๊ฐํ๋ฉด ํธํ๋ค.
Ones and Zeros like
๋ด๊ฐ ๊ฐ์ง ํ ์์ shape์ ๋์ผํ๋, 1 ๋๋ 0์ผ๋ก ์ฐฌ ํ๋ ฌ์ด ๊ฐ๊ณ ์ถ๋ค!
x = [[0, 1, 2],
[2, 1, 0]]
tf.ones_like(x).eval()
# array([[1, 1, 1],
# [1, 1, 1]], dtype=int32)
tf.zeros_like(x).eval()
# array([[0, 0, 0],
# [0, 0, 0]], dtype=int32)
Zip
for x, y in zip([1, 2, 3], [4, 5, 6]):
print(x, y)
1 4
2 5
3 6
for x, y, z in zip([1, 2, 3], [4, 5, 6], [7, 8, 9]):
print(x, y, z)
1 4 7
2 5 8
3 6 9