갈루아의 반서재

728x90

노드의 종류

  • 상수 - tf.constant 를 사용해서 정의함
  • 변수 - tf.Variable 를 사용해서 정의함. 학습대상의 파라메터를 변수로 정의함으로써 파라메터의 갱신, 즉 학습이 가능해짐
  • 플레이스홀더 - tf.placeholder 를 사용해서 정의함. 다양한 값을 받아 넣을 수 있는 상자. 
  • 연산 

 

예제

  • 변수 
import tensorflow.compat.v1 as tf
a = tf.Variable(11, name='a')
b = tf.constant(9, name='b')
c = tf.assign(a, a+b)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('[1] : [c, a]=', sess.run([c, a]))
print('[2] : [c, a]=', sess.run([c, a]))

[1] : [c, a]= [20, 20]

[2] : [c, a]= [29, 29]

 

변수초기화 메서드

tf.global_variables_initializer : 모든 변수 초기화

tf.variables_initializer : 변수 지정 초기화

 

  • 플레이스홀더
import tensorflow.compat.v1 as tf
a = tf.placeholder(dtype=tf.int32, name='a')
b = tf.constant(1, name='b')
c = a+b
with tf.Session() as sess:
print('a + b=', sess.run(c, feed_dict={a: 13}))

a + b= 14

 

  • 연산
import tensorflow.compat.v1 as tf
a = tf.constant(11, name='a')
b = tf.constant(9, name='b')
c = tf.add(a, b)
d = tf.multiply(a, b)
with tf.Session() as sess:
print('a + b = ', sess.run(c))
print('a * b = ', sess.run(d))

a + b = 20

a * b = 99

 

728x90