관리 메뉴

ComputerVision Jack

[모두의 딥러닝 Chapter08] 본문

DeepLearning/DL_ZeroToAll

[모두의 딥러닝 Chapter08]

JackYoon 2020. 1. 20. 12:54
반응형

[08 tensor_manipulation]

Simple Array

간단한 1차원 array

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

pp.pprint(t)

print(t.ndim) # rank

print(t.shape) #shape

print(t[0], t[1], t[-1])

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

print(t[:2], t[3:])

 

2D Array

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

pp.pprint(t)

print(t.ndim)

print(t.shape)

 

Shape, Rank, Axis

t = tf.constant([1, 2, 3, 4])

tf.shape(t).eval()

 

t = tf.constant([[1, 2], [3, 4]])

tf.shape(t).eval()

 

t = tf.constant([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]])

tf.shape(t).eval()

 

Matmul vs Multiply

matrix1 = tf.constant([[3., 3.]])

matrix2 = tf.constant([[2.],[2.]])

tf.matmul(matrix1, matrix2).eval()

#실질적인 행렬의 곱연산이 이루어 진다.

 

(matrix1 * matrix2).eval()

#자리를 유지한 위치 기반의 곱연산이 이루어진다.

 

Watch out broadcasting

matrix1 = tf.constant([[3., 3.]])

matrix2 = tf.constant([[2.], [2.]])

(matrix1 + matrix2).eval()

#자동적으로 텐서의 크기가 변화된다.

 

matrix1 = tf.constant([[3., 3.]])

matrix2 = tf.constant([[2., 2]])

(matrix1 + matrix2).eval()

 

Random value for variable initializations

tf.random_normal([3]).eval()

 

tf.random_uniform([2]).eval()

#텐서플로우 랜덤값 산출 방법

 

Reduce Mean/ Sum

x = [[1., 2.],

     [3., 4.]]

 

tf.reduce_mean(x).eval()

#평균 구하는 함수 = 2.5출력

 

tf.reduce_mean(x, axis = 0).eval()

#axis 축을 기준으로 평균 연산 = [2., 3.]

axis = 0 일경우 열 기준

tf.reduce_mean(x, axis = 1).eval()

#axis 축을 1로 잡아 평균 연산 = [1.5, 3.5]

axis = 1일 경우 행 기준

 

tf.reduce_sum(x, axis = 0).eval()

tf.reduce_sum(x, axis = 1).eval()

 

Argmax with axis

x = [[0, 1, 2],

     [2, 1, 0]]

 

tf.argmax(x, axis = 0).eval()

#argmax() 축을 기준으로 최대값의 인덱스를 반환. axis = 0은 열기준

[1, 0, 0]

tf.argmax(x, axis = 1).eval()

#[2, 0]

 

Reshape, Squeeze, Expand_dims

t = np.array([[[0, 1, 2],

               [3, 4, 5]],

              [[6, 7, 8],

               [9, 10, 11]]])

t.shape

#기존 t의 shape = [2, 2, 3]

tf.reshape(t, shape=[-1, 3]).eval()

#shape의 변화

 

tf.squeeze([[0], [1], [2]]).eval()

#차원을 가장 낮게 만들어 버린다.

 

tf.expand_dims([0, 1, 2], 1).eval()

#차원을 원하는 만큼 높인다.

 

One hot

tf.one_hot([[0], [1], [2], [0]], depth = 3).eval()

#one hot 적용 깊이에 맞게

[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.], [1., 0., 0.]]

 

Casting

tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval()

#실수 정수로 반올림 적용하여 casting

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

#bool 자료형 정수로 casting

 

Stack

쌓아 올리는 방식을 표현하는 자료구조

x = [1, 4]

y = [2, 5]

z = [3, 6]

tf.stack([x, y, z], axis =1).eval()

[[1, 2, 3], [4, 5, 6]

 

Ones like and Zeros like

x = [[0, 1, 2],

     [2, 1, 0]]

 

tf.ones_like(x).eval()

#1로 가득 채워진 행렬을 x행렬 크기만큼 만듦

tf.zeros_like(x).eval()

#0으로 가득 채워진 행렬을 x행렬 크기만큼 만듦

 

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)

#하나로 묶어서 각각 변수에 맞게 매칭해주는 것

 

Transpose

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

pp.pprint(t.shape)

pp.pprint(t)

#전치행렬 적용하기

 

t1 = tf.transpose(t, [1, 0, 2])

pp.pprint(sess.run(t1).shape)

pp.pprint(sess.run(t1))

08-tensor_manipulation.ipynb
0.03MB

반응형

'DeepLearning > DL_ZeroToAll' 카테고리의 다른 글

[모두의 딥러닝 Chapter10]  (0) 2020.01.22
[모두의 딥러닝 Chapter09]  (0) 2020.01.21
[모두의 딥러닝 Chapter07]  (0) 2020.01.19
[모두의 딥러닝 Chapter06]  (0) 2020.01.17
[모두의 딥러닝 Chapter05]  (0) 2020.01.16
Comments