DeepLearning_Lab08 - 8BitsCoding/RobotMentor GitHub Wiki


์œ„์— ์ฝ”๋“œ ์‚ฌ์ดํŠธ๋กœ ๊ฐ€์„œ ๋ณด๋Š”๊ฒŒ ๊ฐ€์žฅ ์ •ํ™•(์„ค๋ช… ์ž˜ ๋˜์–ด ์žˆ์Œ.)

tensor์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์  ๋‚ด์šฉ์„ ์ •๋ฆฌ


import python

# 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()

Simple Array

t = np.array([0., 1., 2., 3., 4., 5., 6.])
pp.pprint(t)
# array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.])

print(t.ndim) 
# rank
# 1

print(t.shape) 
# shape
# (7,)

print(t[0], t[1], t[-1])
# array์˜ ์š”์†Œ ์ถœ๋ ฅ
# 0.0 1.0 6.0

print(t[2:5], t[4:-1])
# [ 2.  3.  4.] [ 4.  5.]

print(t[:2], t[3:])
# [ 0.  1.] [ 3.  4.  5.  6.]

2์ฐจ์› ๋ฐฐ์—ด(2D Array)

t = np.array([1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.](/8BitsCoding/RobotMentor/wiki/1.,-2.,-3.],-[4.,-5.,-6.],-[7.,-8.,-9.],-[10.,-11.,-12.))
pp.pprint(t)
# array([[  1.,   2.,   3.],
#       [  4.,   5.,   6.],
#       [  7.,   8.,   9.],
#       [ 10.,  11.,  12.]])

print(t.ndim) 
# rank
# 2

print(t.shape) 
# shape
# (4, 3)

Shape, Rank, Axis

t = tf.constant([1,2,3,4])
tf.shape(t).eval()
# array([4], dtype=int32)
# Shape : [4]
# Rank : [1]

t = tf.constant([[1,2],
                 [3,4]])
tf.shape(t).eval()
# array([2, 2], dtype=int32)
# Shape : [2, 2]
# Rank : [2]

t = tf.constant([[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12](/8BitsCoding/RobotMentor/wiki/[[1,-2,-3,-4],-[5,-6,-7,-8],-[9,-10,-11,-12),[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24](/8BitsCoding/RobotMentor/wiki/13,-14,-15,-16],-[17,-18,-19,-20],-[21,-22,-23,-24)]])
tf.shape(t).eval()
# array([1, 2, 3, 4], dtype=int32)
# Shape : [1, 2, 3, 4]
# Rank : [4]

Matmul, multiply

matrix1 = tf.constant([1., 2.],[3., 4.](/8BitsCoding/RobotMentor/wiki/1.,-2.],[3.,-4.))
matrix2 = tf.constant([1.],[2.](/8BitsCoding/RobotMentor/wiki/1.],[2.))
print("Metrix 1 shape", matrix1.shape)
# [2, 2]
print("Metrix 2 shape", matrix2.shape)
# [2, 1]
tf.matmul(matrix1, matrix2).eval()
# array([5.],[11.](/8BitsCoding/RobotMentor/wiki/5.],[11.), dtype=float32)
(matrix1*matrix2).eval()
# array([1., 2.],[6., 8.](/8BitsCoding/RobotMentor/wiki/1.,-2.],[6.,-8.), dtpye=float32)

Metrix Broadcasting

Braodcasting : Shape์ด ๋‹ค๋ฅด๋”๋ผ๋„ Metrix์—ฐ์‚ฐ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.!

matrix1 = tf.constant([3., 3.](/8BitsCoding/RobotMentor/wiki/3.,-3.))
matrix2 = tf.constant([2., 2.](/8BitsCoding/RobotMentor/wiki/2.,-2.))
(matrix1 + matrix2).eval()
# array([5., 5.](/8BitsCoding/RobotMentor/wiki/5.,-5.), dtype=float32)
# ์—ฌ๊ธด ์ •์ƒ์ ์ด๋‹ˆ Okay
matrix1 = tf.constant([1., 2.](/8BitsCoding/RobotMentor/wiki/1.,-2.))
matrix2 = tf.constant(3.)
(matrix1 + matrix2).eval()
# array([4., 5.](/8BitsCoding/RobotMentor/wiki/4.,-5.), dtype=float32)
matrix1 = tf.constant([1., 2.](/8BitsCoding/RobotMentor/wiki/1.,-2.))
matrix2 = tf.constant([3., 4.])
(matrix1 + matrix2).eval()
# array([4., 6.](/8BitsCoding/RobotMentor/wiki/4.,-6.), dtype=float32)
matrix1 = tf.constant([1., 2.](/8BitsCoding/RobotMentor/wiki/1.,-2.))
matrix2 = tf.constant([3.],[ 4.]])
(matrix1 + matrix2).eval()
# array([4., 5.],[5., 6.](/8BitsCoding/RobotMentor/wiki/4.,-5.],[5.,-6.), dtype=float32)

Reduce mean

tf.reduce_mean([1, 2], axis=0).eval()
# 1
# ํ‰๊ท ์€ 1.5 ์ธ๋ฐ ์™œ 1์ด๋‚˜์˜ค์ง€??
# ๊ฐ’์ด integer๋ผ์„œ
x = [[1., 2.],
     [3., 4.]]

tf.reduce_mean(x).eval()
# 2.5

tf.reduce_mean(x, axis=0).eval()
# axis=0 y์ถ•์˜ ํ‰๊ท ์„ ๊ตฌํ•ด๋ผ
# array([ 2.,  3.], dtype=float32)

tf.reduce_mean(x, axis=1).eval()
# axis=1 x์ถ•์˜ ํ‰๊ท ์„ ๊ตฌํ•ด๋ผ
# array([ 1.5,  3.5], dtype=float32)

tf.reduce_mean(x, axis=-1).eval()
# 
# array([ 1.5,  3.5], dtype=float32)

Reduce Sum

tf.reduce_sum(x).eval()
# 10.0

tf.reduce_sum(x, axis=0).eval()
# axis=0 y์ถ•์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ผ
# array([ 4.,  6.], dtype=float32)

tf.reduce_sum(x, axis=-1).eval()
# axis=1 x์ถ•์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ผ
# array([ 3.,  7.], dtype=float32)

tf.reduce_mean(tf.reduce_sum(x, axis=-1)).eval()
# 5.0

Argmax

x = [[0, 1, 2],
     [2, 1, 0]]
tf.argmax(x, axis=0).eval()
# axis=0 y์ถ•์˜ ๊ฐ€์žฅ ํฐ index๋ฅผ ์ถœ๋ ฅ
# array([1, 0, 0])

tf.argmax(x, axis=1).eval()
# axis=1 x์ถ•์˜ ๊ฐ€์žฅ ํฐ index๋ฅผ ์ถœ๋ ฅ
# array([2, 0])

tf.argmax(x, axis=1).eval()
# array([2, 0])

Reshape (์ค‘์š”)

t = np.array([[[0, 1, 2], 
               [3, 4, 5]],
              
              [[6, 7, 8], 
               [9, 10, 11]]])
t.shape
# (2, 2, 3)

tf.reshape(t, shape=[-1, 3]).eval()
# array([[ 0,  1,  2],
#       [ 3,  4,  5],
#       [ 6,  7,  8],
#       [ 9, 10, 11]])

tf.reshape(t, shape=[-1, 1, 3]).eval()
# array([[ 0,  1,  2](/8BitsCoding/RobotMentor/wiki/[-0,--1,--2),
#
#       [ 3,  4,  5](/8BitsCoding/RobotMentor/wiki/-3,--4,--5),
#
#       [ 6,  7,  8](/8BitsCoding/RobotMentor/wiki/-6,--7,--8),
#
#       [ 9, 10, 11](/8BitsCoding/RobotMentor/wiki/-9,-10,-11)])

squeeze

tf.squeeze([0], [1], [2](/8BitsCoding/RobotMentor/wiki/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

๋“ค์–ด์˜จ input์˜ ๊ฐ’์„ ๋ณด๊ณ  ๋ช‡ ๋ฒˆ์งธ index๊ฐ€ hot์ธ์ง€ ๋ฆฌํ„ด(์•„๋ž˜ ์ฐธ๊ณ )

tf.one_hot([0], [1], [2], [0](/8BitsCoding/RobotMentor/wiki/0],-[1],-[2],-[0), depth=3).eval()

# array([[ 1.,  0.,  0.](/8BitsCoding/RobotMentor/wiki/[-1.,--0.,--0.),
#
#        [ 0.,  1.,  0.](/8BitsCoding/RobotMentor/wiki/-0.,--1.,--0.),
#
#        [ 0.,  0.,  1.](/8BitsCoding/RobotMentor/wiki/-0.,--0.,--1.),
#
#        [ 1.,  0.,  0.](/8BitsCoding/RobotMentor/wiki/-1.,--0.,--0.)], dtype=float32)

one_hot์„ ํ•˜๋ฉด shape์„ ์กฐ์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Œ.

t = tf.one_hot([0], [1], [2], [0](/8BitsCoding/RobotMentor/wiki/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)

tf.cast([True, False, 1 == 1, 0 == 1], tf.int32).eval()

# array([1, 0, 1, 0], dtype=int32)

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)
tf.stack([x, y, z], axis=1).eval()

# array([[1, 2, 3],
#        [4, 5, 6]], dtype=int32)

ones and zeros like

๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์‚ฌ์ด์ฆˆ๋กœ 0, 1๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

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)
for x, y, z in zip([1, 2, 3], [4, 5, 6], [7, 8, 9]):
    print(x, y, z)