TensorFlow 学习(一)—— tf get variable vs tf Variable ,tf nam
2018-11-14 02:08
645 查看
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
- 对于一个复杂的 tensorflow 模型会有很多个变量,
tf.variable_scope() :提供了简单的命名空间技术以避免冲突; - tf.get_variable():从同一个变量范围内获取或者创建;
1. name_scope()
先说结论:
- tf.get_variable() 以及 tf.Variable() 是 TensorFlow 中创建变量的两种主要方式;
- 如果在 tf.name_scope() 环境下分别使用 tf.get_variable() 和 tf.Variable(),两者的主要区别在于
tf.get_variable() 创建的变量名不受 name_scope 的影响; - tf.get_variable() 创建的变量,name 属性值不可以相同;tf.Variable() 创建变量时,name 属性值允许重复(底层实现时,会自动引入别名机制)
- tf.Variable() 对应地多了一个 initial_value 关键字参数,也即对于 tf.Variable 创建变量的方式,必须显式初始化;
import tensorflow as tfwith tf.name_scope('a_name_scope'): initilizer = tf.constant_initilizer(value=1) var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initilizer=initilizer) # var11 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initilizer=initilizer) var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32) var21 = tf.Variable(name='var21', initial_value=[2.1], dtype=tf.float32) var22 = tf.Variable(name='var22', initiali_value=[2.2], dtype=tf.float32)with tf.Session() as sess: print(var1.name, sess.run(var1)) # print(var11.name, sess.run(var11)) print(var2.name, sess.run(var2)) print(var21.name, sess.run(var21)) print(var22.name, sess.run(var22))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
输出为:
var1:0 [ 1.]a_name_scope/var2:0 [ 2.]a_name_scope/var2_1:0 [ 2.0999999]a_name_scope/var2_2:0 [ 2.20000005]
- 1
- 2
- 3
- 4
2. variable_scope()
一个双层嵌套名称空间:with tf.variable_scope('foo'): with tf.variable_scope('bar'): v = tf.get_variable('v', [1])assert v.name == 'foo/bar/v:0'
- 1
- 2
- 3
- 4
3. get_variable()
get_variable() 函数的行为依赖于 reuse 的状态:case1:reuse 设置为 False,创建并返回新变量:
with tf.variable_scope('foo'): v = tf.get_variable('v', [1])assert v.name == 'foo/v:0'
[ul]1- 2
- 3
with tf.variable_scope('foo'): v = tf.get_variable('v', [1])with tf.variable_scope('foo', reuse=True): v1 = tf.get_variable('v')assert v1 == v1
with tf.variable_scope('a_variable_scope') as scope: initializer = tf.constant_initializer(value=3) var3 = tf.get_variable(name='var3', shape=[1], dtype=tf.float32, initializer=initializer) scope.reuse_variables() # 另一种写法,tf.get_variable_scope().resue_variables() var3_reuse = tf.get_variable(name='var3') var4 = tf.Variable(initial_value=[4.], name='var4', dtype=tf.float32) var4_reuse = tf.Variable(initial_value=[4.], name='var4', dtype=tf.float32)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(var3.name, sess.run(var3)) print(var3_reuse.name, sess.run(var3_reuse))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
a_variable_scope/var3:0 [ 3.]a_variable_scope/var3:0 [ 3.]a_variable_scope/var4:0 [ 4.]a_variable_scope/var4_1:0 [ 4.]
- 1
- 2
- 3
- 4
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
阅读更多相关文章推荐
- TensorFlow 学习(一)—— tf get variable vs tf Variable ,tf nam
- Tensorflow学习教程------tfrecords数据格式生成与读取
- TensorFlow学习---tf.nn.conv2d实现卷积操作
- 【tensorflow】tf.one_hot,tf.reduce_mean,tf.argmax,tf.equal,tf.cast学习
- TensorFlow学习笔记(十六)tf.random_normal
- tensorflow学习——tf.floor与tf.train.batch
- 深度学习笔记——深度学习框架TensorFlow(十)[Creating Estimators in tf.contrib.learn]
- TensorFlow学习--tf.FIFOQueue队列
- tensorflow学习笔记十四:TF官方教程学习 tf.contrib.learn Quickstart
- tensorflow学习之常用函数总结:tensorflow官方例子中的诸如tf.reduce_mean()这类函数
- Tensorflow学习笔记-通过slim读取TFRecord文件
- 【tensorflow 学习】tf.split()和tf.squeeze()
- tensorflow学习day2简单监督学习模型及用tf.train.Saver实现检查点恢复
- 学习笔记TF052:卷积网络,神经网络发展,AlexNet的TensorFlow实现
- TensorFlow学习笔记(5)----TF生成数据的方法
- Tensorflow学习---tf.nn.embedding_lookup 4000
- TensorFlow学习笔记6----tf.contrib.learn Quickstart
- tensorflow学习笔记2——TFrecord
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- TensorFlow学习---tf.nn.dropout防止过拟合