Tensorflow 中scope重复利用Variable 或者 训练参数
2018-02-13 11:53
836 查看
前言: 在Tensorflow中我们想共享变量的时候,需要在一个name_scope下,网络中的一些参数有时利用也是如此,尤其是在RNN中,接下来向大家举例!
代码如下:
#这里的每个变量都不一样,没有共享with tf.name_scope("a_name_scope"):
initializer = tf.constant_initializer(value=1)
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
#这里的var3 重复利用,共享变量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()
var3_reuse = tf.get_variable(name='var3',)
#这里RNN,重复利用训练的参数例子with tf.variable_scope('rnn') as scope:
sess = tf.Session()
train = RNN(train_config)
scope.reuse_variables()
test = RNN(test_config)
sess.run(tf.global_variables_initializer())
欢迎大家学习交流! ;)
代码如下:
initializer = tf.constant_initializer(value=1)
var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
#这里的var3 重复利用,共享变量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()
var3_reuse = tf.get_variable(name='var3',)
#这里RNN,重复利用训练的参数例子with tf.variable_scope('rnn') as scope:
sess = tf.Session()
train = RNN(train_config)
scope.reuse_variables()
test = RNN(test_config)
sess.run(tf.global_variables_initializer())
欢迎大家学习交流! ;)
相关文章推荐
- TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例—Jason niu
- TensorFlow训练的模型参数(W和B)利用np.savez()保存和用cnpy库C++读取
- oj第十二周训练——利用参数宏进行角度与弧度转换
- Tensorflow实战1:利用AlexNet训练MNIST
- 【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别
- TensorFlow学习笔记(六) tensorflow 中的保存训练的参数以及载入参数检测test集
- 利用tensorflow训练自己的图片数据(1)——预处理
- tensorflow2caffe(3) : 如何将tensorflow框架下训练得到的权重转化为caffe框架下的权重参数
- tensorflow name_scope && variable_scope
- 关于tensorflow在训练或者测试时越来越慢,重启之后又恢复
- tensorflow name_scope variable_scope
- tensorflow预训练参数解析
- tensorflow之变量作用域与变量共享(name_scope,variable_scope,get_variable,Variable)
- 利用tensorflow训练自己的图片数据(2)——输入图片处理
- light oj 1257 Farthest Nodes in a Tree (II)(724训练题目)主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点
- Tensorflow使用笔记(1) 怎么保存和使用训练好的参数和模型
- 机器学习笔记7:TensorFlow进阶之利用CNN训练MNIST
- TensorFlow 训练好模型参数的保存和恢复代码
- TensorFlow中,variable_scope和name_scope的不同之处
- 利用tensorflow训练自己的图片数据(3)——建立网络模型