tf.assign的用法

来源:互联网 发布:电脑上的c语言编程软件 编辑:程序博客网 时间:2024/06/01 10:48

tf.assign(A, new_number): 这个函数的功能主要是把A的值变为new_number

例如:

[python] view plain copy
  1. import tensorflow as tf;  
  2.   
  3. A = tf.Variable(tf.constant(0.0), dtype=tf.float32)  
  4. with tf.Session() as sess:  
  5.     sess.run(tf.initialize_all_variables())  
  6.     print sess.run(A)  
  7.     sess.run(tf.assign(A, 10))  
  8.     print sess.run(A)  
输出:

0.0
10.0

开始给A赋值为0,经过tf.assign函数后,把A的值变为10



print("sssssssssss 2 ")
state = tf.Variable(0,name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
  # Runthe 'init' op
 sess.run(init_op)
  # Printthe initial value of 'state'
 print(sess.run(state))
  # Runthe op that updates 'state' and print 'state'.
 for k in range(5):
   sess.run(update)
   print(sess.run(state))

原创粉丝点击