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)