TensorFlow入门(3)——使用变量

xiaoxiao2021-02-28  42

本次可以学习到如何调用tf中的变量、常量,以及如何对其进行初始化。 ''' Created on 2018年4月9日 @author: yqy ''' import tensorflow as tf state = tf.Variable(0,name='counter')#tf.Variable(初值,name='名字') 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))#一定要run这个变量才能调用它的值
转载请注明原文地址: https://www.6miu.com/read-2612562.html

最新回复(0)