TensorFlow学习笔记4——变量共享
因为最近在研究生成对抗网络GAN,在读别人的代码时发现了 with tf.variable_scope(self.name_scope_conv, reuse = reuse): 这样一条语句,查阅官方文档时明白了这是TensorFlow的变量共享机制。
举个例子:当我们研究生成对抗网络GAN的时候,判别器的任务是,如果接收到的是生成器生成的图像,判别器就尝试优化自己的网络结构来使自己输出0,如果接收到的是来自真实数据的图像,那么就尝试优化自己的网络结构来使自己输出1。也就是说,生成图像和真实图像经过判别器的时候,要共享同一套变量,所以TensorFlow引入了变量共享机制。
变量共享主要涉及到两个函数: tf.get_variable(<name>, <shape>, <initializer>) 和 tf.variable_scope(<scope_name>) 。
1. tf.get_variable(<name>, <shape>, <initializer>)
例如,我们搭建一个卷积层:
- def conv_relu(input, kernel_shape, bias_shape):
- # Create variable named "weights".
- weights = tf.get_variable("weights", kernel_shape,
- initializer=tf.random_normal_initializer())
- # Create variable named "biases".
- biases = tf.get_variable("biases", bias_shape,
- initializer=tf.constant_initializer(0.0))
- conv = tf.nn.conv2d(input, weights,
- strides=[1, 1, 1, 1], padding='SAME')
- return tf.nn.relu(conv + biases)
然后,我们调用两次:
- input1 = tf.random_normal([1,10,10,32])
- input2 = tf.random_normal([1,20,20,32])
- x = conv_relu(input1, kernel_shape=[5, 5, 1, 32], bias_shape=[32])
- x = conv_relu(x, kernel_shape=[5, 5, 32, 32], bias_shape = [32]) # This fails.
会发现报错信息。因为执行的命令不明确:第二次调用时是创建一套新的变量(weights,biases)还是再次使用已存在的那一套变量(第一次调用时生成的weights和biases)呢?
这时就需要用到第二个函数: tf.variable_scope(<scope_name>)
2. tf.variable_scope(<scope_name>)
请看例子:
- def my_image_filter(input_images):
- with tf.variable_scope("conv1"):
- # Variables created here will be named "conv1/weights", "conv1/biases".
- relu1 = conv_relu(input_images, [5, 5, 1, 32], [32])
- with tf.variable_scope("conv2"):
- # Variables created here will be named "conv2/weights", "conv2/biases".
- return conv_relu(relu1, [5, 5, 32, 32], [32])
在不同的域内会生成不同的变量。
如果想要变量共享,TensorFlow提供了两种方法:
1. 设置 reuse=True
- with tf.variable_scope("model"):
- output1 = my_image_filter(input1)
- with tf.variable_scope("model", reuse=True):
- output2 = my_image_filter(input2)
2. 调用 scope.reuse_variables()
- with tf.variable_scope("model") as scope:
- output1 = my_image_filter(input1)
- scope.reuse_variables()
- output2 = my_image_filter(input2)
注:在官方文档的最后有这样一段话:Since depending on exact string names of scopes can feel dangerous, it's also possible to initialize a variable scope based on another one:
- with tf.variable_scope("model") as scope:
- output1 = my_image_filter(input1)
- with tf.variable_scope(scope, reuse=True):
- output2 = my_image_filter(input2)
TensorFlow学习笔记4——变量共享的更多相关文章
- TensorFlow学习笔记3——变量共享
因为最近在研究生成对抗网络GAN,在读别人的代码时发现了 with tf.variable_scope(self.name_scope_conv, reuse = reuse): 这样一条语句,查阅官 ...
- tensorflow学习笔记二----------变量
tensorflow里面的变量表示,需要使用特定的语法进行.如果想构造一个行(列)向量,需要调用Variable函数进行.对两个变量进行操作,也要调用相应的函数. import tensorflow ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——自编码器及多层感知器
1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...
- TensorFlow学习笔记(一)
[TensorFlow API](https://www.tensorflow.org/versions/r0.12/how_tos/variable_scope/index.html) Tensor ...
- Tensorflow学习笔记2019.01.22
tensorflow学习笔记2 edit by Strangewx 2019.01.04 4.1 机器学习基础 4.1.1 一般结构: 初始化模型参数:通常随机赋值,简单模型赋值0 训练数据:一般打乱 ...
- 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...
- tensorflow学习笔记(1)-基本语法和前向传播
tensorflow学习笔记(1) (1)tf中的图 图中就是一个计算图,一个计算过程. 图中的constant是个常量 计 ...
- TensorFlow学习笔记——LeNet-5(训练自己的数据集)
在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...
随机推荐
- UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 5228 Descr ...
- NOIP2018有感
近日小编不知博客写些什么,正巧语文假期留了作文,那就博客作文通用吧. 光阴似箭,日月如梭,一个学期不知不觉过去了,有很多事情令我难以忘记. 一周一共七天,其中有两天能休息,但是我只有一天能休息,因为这 ...
- [BZOJ 1177] Oil
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1177 Solution: 相当于将大矩形分为3块,取每块中最大的正方形 对于此类分成几块 ...
- 小白的Python之路 day5 os,sys模块详解
os模块详解 1.作用: 提供对操作系统调用的接口 2.常用方法: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname" ...
- 今天升级Xcode 7.0 bata发现网络访问失败。
今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...
- 最新Mac安装CocoaPods详细教程及各种坑解决办法
网上有很多教程,但要么内容很老,要么不详细,要么各种坑的情况没写.最近买新电脑了,正好要走一遍这些流程,所以写下次教程. 一.安装RVM及更新Ruby 安装RVM的目的是为了更新Ruby,如果你的Ru ...
- FTTB FTTC FTTH FTTO FSA
FTTB Fiber to The Building 光纤到楼 FTTC Fiber to The Curb 光纤到路边 FTTH Fiber to The Home 光纤到家 FTTO Fiber ...
- High Speed Inter-CHIP USB 2.0 PHY
转载:http://arasan.com/products/usb/usb-2-0/hsic-phy/ High Speed Inter-CHIP USB 2.0 PHY USB is the ubi ...
- Linux C 面试题总结
1.进程和线程的区别,及优劣性比较 进程和线程的主要差别在于它们是不同的操作系统资源管理方式.进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路 ...
- 【Hadoop】HDFS客户端开发示例
1.原理.步骤 2.HDFS客户端示例代码 package com.ares.hadoop.hdfs; import java.io.FileInputStream; import java.io.F ...