tensorflow學習之tf.assign


tf.assign(ref, value, validate_shape=None, use_locking=None, name=None),

函數功能是將value賦值給ref

ref必須是tf.Variable創建的tensor,如果ref=tf.constant()就會報錯,而且默認情況下ref的shape和value的shape是相同的

 

import tensorflow as tf
state = tf.Variable(0,name='counter')
print(state.name)
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for __ in range(3):
        sess.run(update)
        print(sess.run(state))


#counter:0
1
2
3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM