프로그래밍 Programming

실전! 딥러닝 - 다차원 배열과 텐서

문장전달자 2020. 11. 18. 19:47
728x90

텐서 = 스칼라와 벡터와 행렬을 포함하는 다차원 배열

 

 

Tensors as generalizations of scalars, vectors and matrices.

www.researchgate.net/figure/Tensors-as-generalizations-of-scalars-vectors-and-matrices_fig3_332263806

 

  • 벡터연산
import tensorflow.compat.v1 as tf

a = tf.constant([1,2,3], name='a')
b = tf.constant([4,5,6], name='b')
c = a+b

with tf.Session() as sess:
    print('a + b = ', sess.run(c))

a + b = [5 7 9]

 

  • 행렬연산 : 2차원 배열 지정
import tensorflow.compat.v1 as tf

a = tf.constant([[1,2],[3,4]], name='a')
b = tf.constant([[1],[2]], name='b')
c = tf.matmul(a,b)

print('shape of a: ', a.shape)
print('shape of b: ', b.shape)
print('shape of c: ', c.shape)

with tf.Session() as sess:
    print('a = \n', sess.run(a))
    print('b = \n', sess.run(b))
    print('c = \n', sess.run(c))

shape of a: (2, 2)

shape of b: (2, 1)

shape of c: (2, 1)

a = [[1 2]

      [3 4]]

b = [[1]

      [2]]

c = [[ 5]

     [11]]

 

 

728x90