본문 바로가기

Machine Learning/#Framework

[Framework] TensorFlow의 설치 및 작동법

(출처) inflearn 모두를 위한 딥러닝 - 기본적인 머신러닝과 딥러닝 강좌, Sung Kim

 

Conda에 TensorFlow 설치하는 방법

https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#tensorflow-cpu

 

Installation — TensorFlow Object Detection API tutorial documentation

Install Anaconda Python 3.7 (Optional) Although having Anaconda is not a requirement in order to install and use TensorFlow, I suggest doing so, due to it’s intuitive way of managing packages and setting up new virtual environments. Anaconda is a pretty us

tensorflow-object-detection-api-tutorial.readthedocs.io

 


▨ 그래프 속에 "Hello, TensorFlow!" 문자열이 들어있는 노드를 생성 후 출력하기

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

#seart a TF session
tf.compat.v1.Session() #2.0 버전부터는 sess = tf.Session() 명령어 작동되지 않음

#run the op and opt result
print(sess.run(hello))

→ b'Hello, TensorFlow!' 출력

b : Bytes literals

 

 


b에 대한 구체적인 내용 → http://stackoverflow.com/questions/6269765/

 

What does the 'b' character do in front of a string literal?

Apparently, the following is valid syntax my_string = b'The string' I would like to know: What does this b character in front of the string mean? What are the effects of using it? What are appro...

stackoverflow.com

 

더하기 (+) 노드로 연결되는 간단한 그래프 만들어보기

node1 = tf.constant(3.0, tf.float32) #노드1에 3의 값을 줌
node2 = tf.constant(4.0) #also tf.float32 implicitly #노드2에 4의 값을 줌
node3 = tf.add(node1, node2) #연산자 노드를 만들어줌 (add)

print("node1:", node1, "node2:", node2) 
node1: Tensor("Const_1:0", shape=(), dtype=float32) node2: Tensor("Const_2:0", shape=(), dtype=float32)
#결과값은 session을 통해 출력 가능

print("node3:", node3)
node3: Tensor("Add:0", shape=(), dtype=float32)

sess = tf.Session()
print("sess.run(node1, node2):", sess.run([node1, node2]))
sess.run(node1, node2): [3.0, 4.0]

print("sess.run(node3):", sess.run(node3))
sess.run(node3): 7.0 #더하기 tense를 실행시켜준 것

값을 가지는 두 개의 노드를 연산 노드로 엮어 computational graph 생성

 


 

텐서플로우 메커니즘 

 

 

  1. 그래프를 build → node 지정
  2. sess.run 명령어로 그래프 실행 → Session 생성
  3. 값을 return하거나 update → sess.run의 return값 출력

 

Placeholder

: 그래프를 미리 만들어 두고 실행 단계에서 값을 입력할 수 있음

  • node를 placeholder라는 특별한 형태로 생성
  • 연산자는 adder_node처럼 지정
  • Session 실행 feed_dict 명령어로 placeholder 값 입력
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # + provide a shortcut for tf.add(a,b)

print(sess.run(adder_node, feed_dict = {a: 3, b: 4.5}))
7.5

print(sess.run(adder_node, feed_dict = {a: [1,3], b: [2,4]})) #여러 값을 지정해줄 수도 있음
[3. 7.] #[1+2, 3+4]

 


 

Everything is Tensor

Rank

: 텐서의 차원

 

shape

: 한 텐서에 들어있는 원소의 개수 

  • "[]"의 형태로 표현
  • t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]의 shape 가장 안쪽의 원소가 3개, 묶음이 3개이므로, [3, 3]으로 표현

 

Type

☞ 대부분의 경우 float32, int32를 사용

 

Rank, Shape, Type 이 세가지 요소를 잘 알고 있으면 코드를 짜기 수월함


 

I'm a Senior Student in Data Science ! 

데이터 사이언스를 공부하고 있는 4학년 학부생의 TIL 블로그입니다. 게시글이 도움 되셨다면 구독과 좋아요 :)

'Machine Learning > #Framework' 카테고리의 다른 글

[Framework] Tensorflow로 선형회귀 구현하기  (0) 2019.10.03