TensorFlow基础知识点(三)变量/Variables
2017-03-14 14:17
232 查看
在TensorFlow中变量维持了图执行过程中的状态信息。
接下来我们用TensorFlow的实例代码来说明。
代码中
注意:通常会将一个统计模型中的参数表示为一组变量 . 例如 , 你可以将一个神经网络的权重作为某个变量存储在一个 tensor 中 . 在训练过程中 , 通过重复运行训练图 , 更新这个
tensor.
接下来我们用TensorFlow的实例代码来说明。
# Create a Variable, that will be initialized to the scalar value 0. # 建 立 一 个 变 量, 用0初 始 化 它 的 值 state = tf.Variable(0, name="counter") # Create an Op to add one to `state`. one = tf.constant(1) new_value = tf.add(state, one) update = tf.assign(state, new_value) # Variables must be initialized by running an `init` Op after having # launched the graph. We first have to add the #`init` Op to the #graph. init_op = tf.initialize_all_variables() # Launch the graph and run the ops. with tf.Session() as sess: # Run the 'init' op sess.run(init_op) # Print the initial value of 'state' print(sess.run(state)) # Run the op that updates 'state' and print 'state'. for _ in range(3): sess.run(update) print(sess.run(state)) # output: # 0 # 1 # 2 # 3
代码中
assign()操作时将图所描绘的表达式的一部分,正如
add()操作一样,所以在操作
run()执行表达式之前,它并不会真正执行赋值操作
注意:通常会将一个统计模型中的参数表示为一组变量 . 例如 , 你可以将一个神经网络的权重作为某个变量存储在一个 tensor 中 . 在训练过程中 , 通过重复运行训练图 , 更新这个
tensor.
相关文章推荐
- TensorFlow基础知识4-变量
- 用《叩响C#之门》复习C#基础知识 第二章 变量
- PROC基础知识之指示变量变量
- ABAP开发基础知识:2) 变量的声明(Declaring Variables)
- 黑马程序员_C#基础知识学习笔记:变量的使用和命名规则
- Java:变量的赋值操作(祭奠我那悲催的基础知识)
- 基础知识--变量和常量
- 黑马程序员_一 【基础知识】【Java环境搭建】和【环境变量】
- PHP基础知识系列:预定义变量
- thinkPHP开发基础知识 包括变量神马的
- 基础知识之绑定变量窥视----BIND PEEKING
- JavaScript 变量基础知识
- SQL 基础知识-声明变量-变量类型
- Request.ServerVariables变量意义. http代理相关知识【转载】
- ThinkPHP开发基础知识 函数变量定义等
- PROC基础知识之CURSOR变量
- 【C#基础知识】之常量、变量、枚举、字符串、转义符、运算符及表达式、try...catch等简单介绍
- php学习1-基础知识变量定义,输出显示,(eval,exit,die)
- java基础知识字节位、javadoc文件的生产、获取键盘输入、方法和变量静态情况
- 2.JavaSE基础知识之环境变量设置