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