TensorFlow Variable 使用方法

来源:互联网 发布:音乐降调软件手机 编辑:程序博客网 时间:2024/05/22 03:44

一直纠结tf变量的使用方法经过查找才找到一些方法


import tensorflow as tf# test Variablewith tf.name_scope("test"):    state = tf.Variable(0,dtype=tf.float32)    oneConst = tf.constant(1,dtype=tf.float32)    new_state = tf.add(state * 2,oneConst)    updataOp = tf.assign(state,new_state)    sess.run(tf.global_variables_initializer()) #init Variable before use it    for i in range(3):        sess.run(updataOp)        print(sess.run(state))        varVal = tf.Variable(i, dtype=tf.float32)        op_var = tf.assign(varVal, i) #dont't init it        #sess.run(tf.global_variables_initializer())        print(sess.run(op_var))

Result:

1.0
0.0
3.0
1.0
7.0
2.0

原创粉丝点击