TF_Variable Sharing
Reference: http://jermmy.xyz/2017/08/25/2017-8-25-learn-tensorflow-shared-variables/
Tensorflow doesn't know what nodes should be grouped together, unless you tell it to.




name scope VS variable scope tf.name_scope() VS tf.variable_scope() variable scope facilitates variable sharing
tf.variable_scope() implicitly creates a name scope.
""" Examples to demonstrate variable sharing
CS 20: 'TensorFlow for Deep Learning Research'
cs20.stanford.edu
Chip Huyen (chiphuyen@cs.stanford.edu)
Lecture 05
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='' import tensorflow as tf x1 = tf.truncated_normal([200, 100], name='x1')
x2 = tf.truncated_normal([200, 100], name='x2') def two_hidden_layers(x):
assert x.shape.as_list() == [200, 100]
w1 = tf.Variable(tf.random_normal([100, 50]), name='h1_weights')
b1 = tf.Variable(tf.zeros([50]), name='h1_biases')
h1 = tf.matmul(x, w1) + b1
assert h1.shape.as_list() == [200, 50]
w2 = tf.Variable(tf.random_normal([50, 10]), name='h2_weights')
b2 = tf.Variable(tf.zeros([10]), name='2_biases')
logits = tf.matmul(h1, w2) + b2
return logits def two_hidden_layers_2(x):
assert x.shape.as_list() == [200, 100]
w1 = tf.get_variable('h1_weights', [100, 50], initializer=tf.random_normal_initializer())
b1 = tf.get_variable('h1_biases', [50], initializer=tf.constant_initializer(0.0))
h1 = tf.matmul(x, w1) + b1
assert h1.shape.as_list() == [200, 50]
w2 = tf.get_variable('h2_weights', [50, 10], initializer=tf.random_normal_initializer())
b2 = tf.get_variable('h2_biases', [10], initializer=tf.constant_initializer(0.0))
logits = tf.matmul(h1, w2) + b2
return logits # logits1 = two_hidden_layers(x1)
# logits2 = two_hidden_layers(x2)
#### Two sets of variables are created. You want all your inputs to use the same weights and biases!
# logits1 = two_hidden_layers_2(x1)
# logits2 = two_hidden_layers_2(x2) ####ValueError: Variable h1_weights already exists,disallowed.Did you mean to set reuse=True in VarScope? # with tf.variable_scope('two_layers') as scope:
# logits1 = two_hidden_layers_2(x1)
# scope.reuse_variables()
# logits2 = two_hidden_layers_2(x2) # with tf.variable_scope('two_layers') as scope:
# logits1 = two_hidden_layers_2(x1)
# scope.reuse_variables()
# logits2 = two_hidden_layers_2(x2) def fully_connected(x, output_dim, scope):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE) as scope:
w = tf.get_variable('weights', [x.shape[1], output_dim], initializer=tf.random_normal_initializer())
b = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
return tf.matmul(x, w) + b def two_hidden_layers(x):
h1 = fully_connected(x, 50, 'h1')
h2 = fully_connected(h1, 10, 'h2') with tf.variable_scope('two_layers') as scope:
logits1 = two_hidden_layers(x1)
# scope.reuse_variables()
logits2 = two_hidden_layers(x2) writer = tf.summary.FileWriter('./graphs/cool_variables', tf.get_default_graph())
writer.close()
TF_Variable Sharing的更多相关文章
- 伪共享(false sharing),并发编程无声的性能杀手
在并发编程过程中,我们大部分的焦点都放在如何控制共享变量的访问控制上(代码层面),但是很少人会关注系统硬件及 JVM 底层相关的影响因素.前段时间学习了一个牛X的高性能异步处理框架 Disruptor ...
- Salesforce的sharing Rule 不支持Lookup型字段解决方案
Salesforce 中 sharing rule 并不支持Look up 字段 和 formula 字段.但在实际项目中,有时会需要在sharing rule中直接取Look up型字段的值,解决方 ...
- [Erlang 0127] Term sharing in Erlang/OTP 上篇
之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...
- 跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享
在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...
- 006_Salesforce Sharing 使用说明
Salesforce Sharing 使用说明 背景说明:Salesforce共享实施记录和其它数据时,需要员工之间共享或多个用户在一个组织间的共享.然而,共享这些数据是有风险的,尤其是当它涉及到敏感 ...
- salesforce 零基础开发入门学习(十二)with sharing 、without sharing 、无声明区别
在salesforce中,声明类大概可以分成三类:分别是可以声明为with sharing,without sharing,以及两者均不声明. public with sharing class A ...
- Cross-Origin Resource Sharing协议介绍
传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片或者脚本.但是Javascript脚 ...
- [转]Stop Sharing Session State between Multiple Tabs of Browser
本文转自:http://jinaldesai.net/stop-sharing-session-state-between-multiple-tabs-of-browser/ Scenario: By ...
- POJ - 1666 Candy Sharing Game
这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...
随机推荐
- effective java(第三版)---读书笔记
第一章 引言 < Effective Java>这本书并不厚,而且并不适合初学者,适合有一定的工作经验的java攻城狮.这本书不是百科全书式的JAVA 手册,而是试图在讲述如何正确.高效地 ...
- 《剑指offer》Q01-12 (牛客10.11)
目录 T1 二维部分有序数组查找 ☆ T2 字符串字符不等长替换 - 从后往前 T3 返回链表的反序 vector T4 重建二叉树 T5 两个栈模拟队列 T6 旋转数组中的最小元素 - 二分或暴力 ...
- PYTHON 100days学习笔记008-4:错误和异常
目录 Day008_04:python错误和异常 1.语法错误 2.异常 3.异常处理 4.抛出异常 5.用户自定义异常 6.定义清理行为 7.预定义的清理行为 参考文章: python3错误和异常 ...
- 常用的API
API (application programming Interface)应用程序编程接口 java 是字典 Scanner类的功能可以实现从键盘输入到程序当中. 引用类型的一般使用步骤 ·1导包 ...
- poj2406(求字符串的周期,kmp算法next数组的应用)
题目链接:https://vjudge.net/problem/POJ-2406 题意:求出给定字符串的周期,和poj1961类似. 思路:直接利用next数组的定义即可,当没有周期时,周期即为1. ...
- Java静态代理与动态代理实现
一.什么是代理 代理是一种设计模式,它提供了一种通过代理访问目标对象的方式.在应用代理之前,我们调用对象的过程如下: 客户端直接调用对象并获取返回值.而应用了代理之后,我们调用对象的过程变成如下: 客 ...
- Tkinter(一)
采集小工具,目前采集主要针对知乎文章与评论,今天刚开始弄,会不断更新完善 目前效果(测试站点 :科技:测试连接:http://zhihu.sogou.com/include/pc/pc/topic/t ...
- 通过ImageReader进行图像裁剪时出现NoSuchElementException异常
首先放上最初的Image工具类 package util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import ...
- js date对象传参获取特定日期的时间戳
当我们想要通过js获取某一特定时间的时间戳时,会通过给date对象传参再通过getTime函数来获取,传递的参数格式也有不同形式.有些时候,可能会因为自己传入参数的格式不正确而导致date对象inva ...
- js 怎样判断用户是否在浏览当前页面
有些时候我们需要在项目中判断用户是否在浏览当前页面,或者当前页面是否处于激活状态.然后再进行相关的操作.浏览器中可通过window对象的onblur.onfocus判断,或者document的hidd ...