[프로그래밍]/모두를 위한 딥러닝

[모두를위한딥러닝] 텐서(Tensor) 연산 기본 사용 명령어

댕이댕이 2019. 8. 20. 05:53

 

Matmul

행렬을 곱할 땐 두 개의 곱에 대해서 끝이 맞아야 한다.

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

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

위는 (2,2) 행렬(x)과 (2,1) 행렬(y)을 곱하는데 곱한 결과는

y의 shape와 동일 하도록 해야한다. 

 

Broadcasting

행렬 요소 안에 있는 값을 포함하여 행렬 간 합을 구할 경우 사용한다.

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

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


array([[ 5.,  5.],

       [ 5.,  5.]], dtype=float32)

Random Values for Variable initializations

 

랜덤한 값을 출력시키고자 할 경우 사용한다.

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

array([ 2.20866942, -0.73225045,  0.33533147], dtype=float32)

 

Reduce Mean/Sum

전체 평균/합을 구하고자 할 경우 사용한다.

x = [[1., 2.],
     [3., 4.]]

tf.reduce_mean(x, axis=0).eval()
array([ 2.,  3.], dtype=float32)

tf.reduce_sum(x, axis=0).eval()
array([ 4.,  6.], dtype=float32)

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

 

Argmax with axis

axis는 축에 대한 옵션

Argmax는 행렬 요소 중 가장 큰 값을 선택해준다.

 

axis가 0일 경우 축은 아래를 향하고 1일 경우 축은 오른쪽을 향한다.

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


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


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

 

Reshape

가장 중요한 것으로 Shape를 조절하는 역할을 한다. 데이터의 훼손이 있을 수 있으므로 

조절에 신중을 가하여 한다.

t = np.array([[[0, 1, 2], 
               [3, 4, 5]],
              
              [[6, 7, 8], 
               [9, 10, 11]]])
               
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]],



       [[ 3,  4,  5]],



       [[ 6,  7,  8]],



       [[ 9, 10, 11]]])
       
       

One Hot

정보를 갖고 0과 1로만 표현해주도록 한다. 

예를들어 0,1,2,3,4 중에서 3이라는 출력값 one hot으로 띄워주고자 할 시

[0,0,0,1,0]으로 띄워준다.

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

주어진 텐서에 대해서 주어진 자료형으로 변환하고자 할 경우 사용할 수 있다. 그 값이

True False여도 말이다.

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

어떠한 Shape를 갖고 있을 때 이와 동일한 0혹은 1로 채워진 Shape를 만들어낸다.

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)

 

zips

복수 개의 텐서를 묶어내고자 할 경우

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