본문 바로가기
머신러닝

[실습] 모두의딥러닝 - tensorflow의 기본 및 설치방법

by 박정률 2017. 1. 23.

TensorFlow

* TensorFlow is an open source software library for numerical computation using data flow graphs.

* Python!


What is a data Flow Graph?

* Nodes in the graph represent mathematical operations (수학적 연산)

* Edges represent the multidimensional data arrays(행렬, 벡터) communicated between them.

노드가 operation 이고 Edge 가 data(tensor) 이다.



따라서 이 그래프를 이용함으로써

 1. 딥러닝 이나 linear regression 등 많은 작업 수행가능하게하고

 2. 각 노드들이 여러개의 cpu로 분산될 수 있다는 장점도 있습니다.

 그러므로 딥러닝, 머신러닝에 적당한 framework입니다!


Installation(mac os 기준입니다!)

맥북에는 기본적으로 python이 설치되어있지만 tensorflow를 바로 설치하기에는 에러가 자주 발생하는 것같습니다.

해매던 중 정식홈페이지의 설치방법을 자세히 읽어본 후 설치를 완료 했습니다!

(처음 그냥 설치 할 때 numpy? 관련 에러가 발생했습니다)

바로  virtualenv를 설치해야 하는것 인데요. 이 가상환경을 설치해야 정상적으로 설치됩니다.

무겁지않고 매우 간단한 설치입니다!



https://www.tensorflow.org/get_started/os_setup 

이 사이트에 들어가서 차례대로 하면됩니다! 이 때 바로 tensorflow를 설치하지마시고 virtualenv 를 클릭하여

절차대로 진행하시면 virtualenv가 active되고 (venv)라는 문구가 뜹니다! 

이 문구가 앞에 있는 상태에서 나머지 tensorflow를 설치해주시면 마무리됩니다.


처음에 이 virtualenv 를 설치하지 않음으로써 많은 에러들이 발생하여 하루종일 애먹었습니다!


자 설치가 완료되었으면 기본 코드를 실행시켜볼까요!?


tensorflow 기본코드

1
2
3
4
5
6
7
8
9
10
11
12
13
import tensorflow as tf
 
hellow = tf.constant('Hello, TensorFlow!')
// 이거 자체도 operator 가된다 (node)
 
print hello
//상수값이 나오지않고 Tensor 라는 것이 나온다.
 
sess = tf.Session()
 
print sess.run(hello)
//node를 실행시키는 것, 실행이 되야 의미를 갖는것.
 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tensorflow as tf
 
sess  = tf.Session()
= tf.constant(2)
= tf.constant(3)
 
= a+b
//Everything is operation!
print c
//실행이 아니라 노드의 형태만 출력됌.
 
print sess.run(c)
//이것이 실행.
 
cs



1
2
3
4
5
6
7
8
9
import tensorflow as tf
sess = tf.Session()
 
= tf.constant(2)
= tf.constant(3)
 
= a+b
print c
print sess.run(c)
cs



Basic operations

1
2
3
4
5
6
7
8
9
10
import tensorflow as tf
 
= tf.constant(2)
= tf.constant(3)
 
with tf.Session() as less:
print "a=2, b=3"
print "Addition with c constants : %i " %sess.run(a+b)
print "Multiplication with constants : %i" %sess.run(a*b)
 
cs


Placeholder

1
2
3
4
5
6
7
8
9
10
11
12
import tensorflow as tf
 
= tf.placeholder(tf.int16)
= tf.placeholder(tf.int16)
 
add = tf.add(a,b)
mul = tf.mul(a,b)
 
with tf.Session() as less:
print "Addition with variables: %i" %sess.run(add, feed_dict = {a: 2,b :3})
print "Multiplication with variables: %i" %sess.run(mum, feed_dict= {a: 2,b: 3})
cs


a*b 라는 model 을 정의할 때 데이타 타입만 정해주고 실행 시킬 때에 값을 넘겨준다.

함수가 아님에도 실행시점에서 값을 바꾸게 하는 powerful한 기능.


이상 tensorflow 를 설치하고 기본코드를 실행시켜 보았습니다.