본문 바로가기
Dev/딥러닝

01. Tensor Flow 의 기본적인 operations

by bsion 2018. 8. 16.
01. 머신러닝의 개념과 용어

출처: 모두를위한 머신러닝 (http://hunkim.github.io/ml/)

TensorFlow : Data Flow Graph


Node 와 Node가 연결되어 있는 구조

  • Nodes : Mathematiclal operations (어떤 값을 받아서 더한다, 곱한다 등..)
  • Edges : Data arrays (Tensors !)

Tensor 만드는 방법


  • tf.constant(넣을 값)
  • tf.placeholder(tf.데이터 타입) : run 단계에서 값 설정
In [10]:
import tensorflow as tf

hello = tf.constant("Hello")

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
node4 = node1 + node2

print("node1:", node1)
print("node2:", node2)
print("node3:", node3)
print("node4:", node4)
node1: Tensor("Const_1:0", shape=(), dtype=float32)
node2: Tensor("Const_2:0", shape=(), dtype=float32)
node3: Tensor("Add:0", shape=(), dtype=float32)
node4: Tensor("add:0", shape=(), dtype=float32)

Session 의 역할


  • Tensor 는 그 자체로 object 이므로, print() 했을 시 object 정보를 출력함
  • Session 을 만든후 run을 시킬 경우, 단순 변수인 tensor 는 변수를 출력하고
  • 만약 어떤 연산을 하는경우 연산을 run 시킴 -> 얽혀 있는 다른 변수들이 update 됌!!
In [4]:
sess = tf.Session()

print(sess.run(hello))
print("sess.run(node1, node2):", sess.run([node1, node2]))
print("sess.run(node3):", sess.run(node3))
print("sess.run(node4):", sess.run(node4))
b'Hello'
sess.run(node1, node2): [3.0, 4.0]
sess.run(node3): 7.0
sess.run(node4): 7.0

TensorFlow Mechanics


  • Flow Graph 를 만든후 (node1= , node2= , ..)
  • Session 을 실행 (session.run(node1))
  • Graph 안의 변수 값 리턴

2017-08-02%2016;28;11.PNG

Place holder


Tensor 의 값을 미리 정하지 않고 flow graph 틀만 구성한 후, sess.run 단계에서 값 지정

In [13]:
import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b

sess = tf.Session()

print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))  # adder node 에 dict 형태로 a, b 값 지정
print(sess.run(adder_node, feed_dict={a: [1, 3], b: [2, 4]}))
7.5
[ 3.  7.]

Tensor Ranks, Shapes and Types


  • Ranks: 몇차원 Array 인지2017-08-02%2016;56;40.PNG
  • Shapes: 각각의 element에 몇개씩 들어있는지2017-08-02%2016;56;52.PNG
  • Types: 데이터타입2017-08-02%2016;57;00.PNG


댓글