본문 바로가기
TensorFlow

변수 Variables (11)

by EasyGPT 2016. 3. 31.
반응형


Variables

Variables 그래프가 실행되는 동안 state 유지합니다.
Variables maintain state across executions of the graph.

다음 예제는 simple counter 역할을 하는 변수 보여주고 있습니다.
The following example shows a variable serving as a simple counter.

보다 상세한 사항은 Variables 참조 하세요.
See 
Variables for more details.

#
변수 만들기, 변수를 scalar 0으로 초기화


state = tf.Variable(0, name="counter")


# `state` 1 더하는 op 만들기.


one = tf.constant(1)


new_value = tf.add(state, one)


update = tf.assign(state, new_value)


 


# 그래프를 가동시킨 , `init` op 가동시켜서 변수를 초기화해야 , 먼저 `init` op 그래프에 더해야


init_op = tf.initialize_all_variables()


 


# 그래프를 가동시키고 ops 가동시키기, 'init' op 가동시키고, 'state' 초기값 프린트 하기


with tf.Session() as sess:


  # 'init' op 가동시키기


sess.run(init_op)


 


  # 'state'의 초기값 프린트 하기


print(sess.run(state))


 


  # 'state' 업데이트 하는 op 가동시키고, 'state' 프린트하기


  for _ in range(3):


    sess.run(update)


    print(sess.run(state))


 


# 출력된 결과


0


1


1


2


2


3


3


 


코드의 assign() op add() op 같은 expression graph 일부이기 때문에, 이것은 run() expression 실행할 때까지 할당을 실제로 수행하지 않습니다.
The assign() operation in this code is a part of the expression graph just like the add() operation, so it does not actually perform the assignment until run() executes the expression.

일반적으로 statistical model 패러미터를 Variables(변수) 세트로 표현합니다.
You typically represent the parameters of a statistical model as a set of Variables.

예를 들어, NN 가중치 변수 안의 텐서 저장할 것입니다.
For example, you would store the weights for a neural network as a tensor in a Variable.

훈련하는 중에, 훈련그래프를 반복적으로 가동시켜서 텐서를 업데이트 합니다.
During training you update this tensor by running a training graph repeatedly.




반응형

'TensorFlow' 카테고리의 다른 글

Feeds (13)  (0) 2016.03.31
Fetch (12)  (0) 2016.03.31
Interactive 사용법 (10)  (0) 2016.03.31
기초 사용법 Basic Usage(9)  (0) 2016.03.31
TensorFlow Introduction (8)  (0) 2016.03.31

댓글