상수와 변수

Python으로 시작하는 TensorFlow

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

TensorFlow란?

  • 그래프 기반 수치 계산을 위한 오픈 소스 라이브러리
    • Google Brain 팀 개발
  • 저수준·고수준 API 제공
    • 덧셈, 곱셈, 미분
    • 머신러닝 모델
  • TensorFlow 2.0의 주요 변경점
    • 기본 Eager Execution
    • Keras와 Estimator로 모델 구축
Python으로 시작하는 TensorFlow

텐서란?

  • 벡터와 행렬의 일반화
  • 수의 모음
  • 고정된 형태(Shape)
Python으로 시작하는 TensorFlow

텐서란?

이미지는 식빵 한 조각, 9조각으로 나눈 식빵 한 조각, 그리고 식빵 한 덩이를 보여줍니다. 출처: Public Domain Vectors

Python으로 시작하는 TensorFlow

TensorFlow에서 텐서 정의

import tensorflow as tf

# 0차원 텐서
d0 = tf.ones((1,))
# 1차원 텐서
d1 = tf.ones((2,))
# 2차원 텐서
d2 = tf.ones((2, 2))
# 3차원 텐서
d3 = tf.ones((2, 2, 2))
Python으로 시작하는 TensorFlow

TensorFlow에서 텐서 정의

# 3차원 텐서 출력
print(d3.numpy())
[[[1. 1.]
  [1. 1.]]

 [[1. 1.]
  [1. 1.]]]
Python으로 시작하는 TensorFlow

TensorFlow에서 상수 정의

  • 상수는 가장 단순한 텐서입니다
    • 학습 불가
    • 차원은 임의
from tensorflow import constant

# 2x3 상수 정의
a = constant(3, shape=[2, 3])
# 2x2 상수 정의
b = constant([1, 2, 3, 4], shape=[2, 2])
Python으로 시작하는 TensorFlow

편의 함수로 상수 정의하기

연산 예시
tf.constant() constant([1, 2, 3])
tf.zeros() zeros([2, 2])
tf.zeros_like() zeros_like(input_tensor)
tf.ones() ones([2, 2])
tf.ones_like() ones_like(input_tensor)
tf.fill() fill([3, 3], 7)
Python으로 시작하는 TensorFlow

변수 정의와 초기화

import tensorflow as tf

# 변수 정의
a0 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.float32)
a1 = tf.Variable([1, 2, 3, 4, 5, 6], dtype=tf.int16)
# 상수 정의
b = tf.constant(2, tf.float32)
# 곱 계산
c0 = tf.multiply(a0, b)
c1 = a0*b
Python으로 시작하는 TensorFlow

연습해 봅시다!

Python으로 시작하는 TensorFlow

Preparing Video For Download...