본문 바로가기
IT 기록/머신러닝

[머신러닝]01-2.Tensorflow의 기본

by Lazy Quant 2018. 6. 25.
반응형

※본 포스팅은 직접 강의하는 내용이 아닌, 김성훈 교수님의 머신러닝 강의를 정리하기 위한 포스팅입니다.

김성훈 교수님의 강의는 모두를 위한 머신러닝/딥러닝(http://hunkim.github.io/ml)에서 들을 수 있습니다.


Tensorflow란?

  • 구글에서 만든 오픈소스 라이브러리
  • 머신러닝을 위한 라이브러리는 많지만, Tensorflow가 압도적인 1등(new contributors, new forks, new issues 기준 등)
  • Data flow graphs를 사용하여 수치적으로 연산
  • Python을 사용


Data flow graph란?

tensorflow

  • 노드(Node)와 엣지(Edge)로 연결된 것이 그래프
  • 노드(Node) : 하나의 연산(더하기, 곱하기 등)
  • 엣지(Edge) : 데이터(Tensor)의 연결

Tensorflow의 프로세스

  1. 그래프를 설계
  2. 그래프를 실행
  3. 결과 값으로 변수 업데이트&학습

Tensorflow 버전 확인(Check TF version)

In [1]:
import tensorflow as tf
tf.__version__
Out[1]:
'1.0.0'

Hello TensorFlow! 출력하는 프로그램 만들기

In [2]:
# Create a constant op
# This op is added as a node to the default graph
hello = tf.constant("Hello, TensorFlow!")

# start a TF session
sess = tf.Session()

# run the op and get result
print(sess.run(hello))
Out[1]:

b'Hello, TensorFlow!'

참고)

  • tf.constant(데이터) : 상수를 생성 - 하나의 노드가 됨(여기서는 Hello, TensorFlow!이라는 문자열)
  • tf.Session() : 설계된 그래프를 실행하기 위한 세션을 생성
  • sess.run(hello) : 실행(Session내에서의 run 함수)

Tensors

In [3]:
3 # a rank 0 tensor; this is a scalar with shape []
[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]
Out[3]:
[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]


Tensor의 구조

  • Rank : 차원 수(몇 차원의 Array인가)
  • Shape : 배열의 구조(몇 개씩 들어있는가)
  • Type : 데이터 타입


반응형

댓글