본문 바로가기

카테고리 없음

파이썬 텐서플로우(Tensorflow) 예제 - constant node, computational node, placeholder

출처 : http://it.plusblog.co.kr/221237799413


Constant Node

텐서플로우(TensorFlow)를 이용하여 데이터 그래프를 만드는 과정에서 사용할 수 있는 가장 기본적인 노드는 'constant'노드다. 텐서플로우의 가장 기본적인 hello_world.py를 보면

import tensorflow as tf hello = tf.constant("Hello, TensorFlow!") sess = tf.Session() print(sess.run(hello)) # https://github.com/ezkun/tensorflow_samples 참조

'tf.constant'라고 하는 부분을 볼 수 있다. constant 노드를 생성하는 부분으로 상수 값을 담고있는 노드다. TensorFlow를 수행하면서 해당 노드가 가지고 있는 값은 변하지 않는다. C언어에서 const 변수를 생각하면 될 것 같다.

Computational Node

두 노드의 값을 이용하여 연산을 수행하는 노드가 존재할 수 있다. 두 값을 받아서 더해주는 Computational 노드를 텐서플로우로 만들어보면 다음과 같다.

import tensorflow as tf const_node1 = tf.constant(3.0, tf.float32) const_node2 = tf.constant(4.0, tf.float32) com_node = tf.add(const_node1, const_node2) # or const_node1 + const_node2 sess = tf.Session() print("sess.run(const_node1, const_node2): ", sess.run([const_node1, const_node2])) print("sess.run(com_node): ", sess.run(com_node)) # https://github.com/ezkun/tensorflow_samples 참조

'3.0'이라는 값을 갖는 const_node1과 '4.0'이라는 값을 갖는 const_node2를 입력으로 받아 '덧셈(tf.add)'을 수행해주는 computational 노드를 정의한 코드다. 위 코드를 실행해보면, '7.0'을 출력한다.

Computational 노드는 또 다른 Computational 노드의 입력으로 사용될 수 있다. ((3.0 + 4.0) + 1.2)를 텐서플로우로 코딩해보겠다.

import tensorflow as tf const_node1 = tf.constant(3.0, tf.float32) const_node2 = tf.constant(4.0, tf.float32) const_node3 = tf.constant(1.2, tf.float32) com_node1 = tf.add(const_node1, const_node2) com_node2 = tf.add(com_node1, const_node3) sess = tf.Session() print("sess.run(const_node1, const_node2): ", sess.run([const_node1, const_node2])) print("sess.run(com_node1): ", sess.run(com_node1)) print("sess.run(com_node2): ", sess.run(com_node2)) # https://github.com/ezkun/tensorflow_samples 참조

위 코드를 보면 const_node1 + const_node2를 수행하는 com_node1이 정의되고, com_node1의 결과 값을 const_node3과 더해주는 com_node2가 정의된다. 실행결과는 다음과 같다.

Placeholder Node

텐서플로우로 데이터 플로우 그래프를 만드는 과정에서 노드의 값이 미리 정해지지 않도록 만들 수도 있다. 런타임에 사용자가 입력한 값을 가지고 수행을 할 수도 있다. 텐서플로우에서는 이런 노드를 Placeholder 노드라고 한다. 일단 자리만 잡아 놓고 나중에 가지고 들어온 값에 따라 수행하겠다는 의미다.

예를 들어 두 값을 더하는 과정을 생각해보면, X라는 변수와 Y라는 변수를 두고 'X + Y'라는 그래프를 만들어 둘 수 있다. 이 후 런타임에 사용자가 X=30, Y=20을 입력하면 두 값을 더해 'X + Y'라는 computational 노드의 값은 50이 된다. 만약 런타임에 X=10, Y=20이라는 값이 입력되면, 'X + Y'라는 computational 노드의 값은 30이 된다.

입력 받을 수 있는 모든 경우를 Constant Node로 만들어서 모든 경우의 수를 그래프로 모델링 할 수 없기 때문에 Placeholder의 효용성은 금방 깨달을 수 있을 것이다. Placeholder를 이용한 간단한 예제는 다음과 같다.

import tensorflow as tf num1 = tf.placeholder(tf.float32) num2 = tf.placeholder(tf.float32) out = num1 + num2 # shortcut for tf.add(num1, num2) sess = tf.Session() print(sess.run(out, feed_dict={num1:1.5, num2:2.5})) print(sess.run(out, feed_dict={num1:[1.2, 1.3], num2:[4.5, 5.7]})) # https://github.com/ezkun/tensorflow_samples 참조

num1과 num2를 placeholder 노드로 만들고 out이라는 computational 노드는 num1의 값과 num2의 값을 더하는 동작으로 정의를 했다. 나중에 session을 만들고 run으로 수행할 때 feed_dict라고 하는 변수로 실행값을 넘겨주면 feed_dict에 적혀있는 값을 이용해 덧셈 연산을 수행한다. 위 코드를 실행하면 다음과 같은 결과가 출력된다.

이 밖에도 텐서플로우는 다양한 데이터 플로우 그래프 모델링을 위한 노드들과 기능들을 제공한다. 필요한 경우 하나씩 예제와 함께 설명을 추가하도록 하겠다.