(第一章第五部分)TensorFlow框架之变量OP

 

系列博客连接:html

(一)TensorFlow框架介绍:https://www.cnblogs.com/kongweisi/p/11038395.htmlpython

(二)TensorFlow框架之图与TensorBoard:https://www.cnblogs.com/kongweisi/p/11038517.html网络

(三)TensorFlow框架之会话:https://www.cnblogs.com/kongweisi/p/11038550.html框架

(四)TensorFlow框架之张量:https://www.cnblogs.com/kongweisi/p/11039237.htmldom

 

本文概述:url

  • 说明变量op的特殊做用
  • 说明变量op的trainable参数的做用
  • 应用global_variables_initializer实现变量op的初始化

 

一、变量

TensorFlow变量是表示程序处理的共享持久状态的最佳方法。变量经过 tf.Variable OP类以及tf.get_variable()类进行操做。spa

变量的特色:.net

  • 存储持久化
  • 可修改值
  • 可指定被训练

1.1 建立变量

  • tf.Variable(initial_value=None, trainable=True, collections=None, name=None)
    • initial_value:初始化的值
    • trainable:是否被训练
    • collections:新变量将添加到列出的图的集合中collections,默认为[GraphKeys.GLOBAL_VARIABLES],若是trainable是True变量也被添加到图形集合 GraphKeys.TRAINABLE_VARIABLES
# 特殊的建立张量OP
# 一、必须手动初始化

var = tf.Variable(tf.random_normal([2, 2], mean=0.0, stddev=1.0), name="var", trainable=True) with tf.Session() as sess: sess.run(var)
  • 变量须要显示初始化,才能运行值
# 添加一个初始化变量的OP
# 一、变量显示初始化
init_op = tf.global_variables_initializer() with tf.Session() as sess: # 二、运行初始化变量的OP sess.run(init_op)
   # 或者直接这么作 sess.run(tf.global_variables_initializer())

1.2 变量OP的方法

 

给变量赋值一个新的值,返回一个新的变量code

  • new_var = assign(value)   原变量变化
  • new_var = assign_add(delta)  原变量不变
var = tf.Variable(tf.random_normal([2, 2], mean=0.0, stddev=1.0), name="var", trainable=True)
#
给变量赋值一个新的值
var1 = var.assign([[2, 3], [4, 5]])
# 初始化变量OP
init_op
= tf.global_variables_initializer()
# 给变量在原值的基础上,加上新的值
va = var.assign_add([[1, 3], [4, 5]])

with tf.Session() as sess:
  # 运行初始化op  
  sess.run(init_op)
  print(sess.run(va))
  print(sess.run(var))

关于变量的被训练,我会在后面的线性回归案例当中介绍orm

二、命名空间与共享变量

共享变量的主要用途在一些网络当中的参数共享, 因为在TensorFlow当中,只要咱们定义的不一样OP, 即便name参数指定同样,但实际上也并非同一个变量。

若是想要达到重复利用变量的效果,咱们就要使用tf.variable_scope()结合tf.get_variable()一块儿使用

2.1 定义一个相同名字的变量

var = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) var_double = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) <tf.Variable 'var:0' shape=() dtype=float32_ref>
<tf.Variable 'var_1:0' shape=() dtype=float32_ref>

2.2 使用tf.variable_scope()修改OP命名空间

会在OP的名字前面增长命名空间的指定名字

with tf.variable_scope("name"): var = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) var_double = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) <tf.Variable 'name/var:0' shape=() dtype=float32_ref>
<tf.Variable 'name/var_1:0' shape=() dtype=float32_ref>

2.2 tf.get_variable共享变量

经过tf.get_variable的初始化与Variable参数同样, 可是要是实现共享须要打开 tf.variable_scope("name")中的reuse = tf.AUTO_REUSE参数

# 打开共享参数 # 或者 # with tf.variable_scope("name") as scope: # 在须要使用共享变量的前面定义: scope.reuse_variables()
with tf.variable_scope("name", reuse=tf.AUTO_REUSE): var = tf.Variable(initial_value=4.0, name="var", dtype=tf.float32) var_double = tf.Variable(initial_value=4.0, name="var", dtype=tf.float32) var1 = tf.get_variable(initializer=tf.random_normal([2, 2], mean=0.0, stddev=1.0), name="var1", dtype=tf.float32) var1_double = tf.get_variable(initializer=tf.random_normal([2, 2], mean=0.0, stddev=1.0), name="var1", dtype=tf.float32) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(var1) print(var1_double)

注意:TensorFlow和python不一样, 它是维护一个全部OP名字的列表,不是以取的最前面的名字(自定义的接收结果,python变量)区分。

共享变量就至关于全局变量。

相关文章
相关标签/搜索