Python2/정리

tf.constant , tf.Variable, tf.placeholder 차이

고수트 2019. 3. 12. 15:40
반응형

tf.constant , tf.Variable, tf.placeholder 차이


tf.constant : 텐서플로우 변하지 않는 상수 생성

tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

ex) 

import tensorflow as tf

sess = tf.Session()

x = tf.constant([5], dtype=tf.float32, name='test')

init = tf.global_variables_initializer()

sess.run(init)

print(sess.run(x))

    

tf.Variable : 값이 바뀔 수도 있는 변수 생성

단! 변수는 그래프를 실행하기 전에 초기화를 해주어야한다.

세션을 초기화(tf.global_variables_initializer()) 하는 순간 변수에 그 값이 지정된다.

ex) 

import tensorflow as tf

sess = tf.Session()

x = tf.Variable([2], dtype=tf.float32, name='test2')

init = tf.global_variables_initializer()

sess.run(init)

print(sess.run(x))



tf.placeholder : 일정 값을 받을 수 있게 만들어주는 그릇을 생성한다.

이 placeholder 를 이용할 때에는 

실행시 feed_dict={x="들어갈 값"} 와 같은 식을 값을 지정하여 실행시킨다. 


ex)

import tensorflow as tf


input_data = [1,2,3]

x = tf.placeholder(dtype=tf.float32)

y = x * 2

sess = tf.Session()

print(sess.run(y,feed_dict={x:input_data}))



반응형