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 ...
随机推荐
- Had I not seen the Sun(如果我不曾见过太阳)
Had I not seen the Sun by Emily Dickinson Had I not seen the Sun I could have borne the shade But Li ...
- Java学习笔记-包,classpath,import,jar
这里介绍Java的包,classpath,import和jar 包(package) 对类文件进行分类管理 给类提供多层命名空间 写在程序文件的第一行 类名的全称的是 包名.类名 包也是一种封装形式 ...
- 洛谷 题解 P4158 【[SCOI2009]粉刷匠】
状态: dp[i][j][k][0/1]: 到达第i行时, 到达第j列时, 刷到第k次时, 这一格有没有刷对 转移 换一块木板时肯定要多刷一次 dp[i][j][k][0]=max(dp[i-1][m ...
- javascript let
es6支持通过let关键字声明属于单独块{}的变量,更好的管理变量作用屿 funtion foo() { var a=1; if (a>1) { let b=2; //只属于if模块 while ...
- session和cookie区别,多台WEB服务器如何共享session,禁用COOKIE后SESSION是否可用,为什么?
答:session的运行机制: 用户A访问站点Y,如果站点Y指定了session_start();(以下假设session_start()总是存在)那么会产生一个session_id,这个sessio ...
- Go语言注意事项
必须恰当导入需要的包,缺少了必要的包或者导入了不需要的包,程序都无法编译通过.这项严格要求避免了程序开发过程中引入未使用的包(译注:Go语言编译过程没有警告信息,争议特性之一 import 声明必须跟 ...
- 第一章、web应用安全概论--web应用系统介绍--TCP/IP协议
TCP/IP协议源于1969年,是国际互联网Internet采用的协议标准TCP/IP协议是一组通信协议的代名词,是由一系列协议组成的协议族,本身是指两个协议集: TCP--传输控制协议 ...
- MyBatis 源码篇-DataSource
本章介绍 MyBatis 提供的数据源模块,为后面与 Spring 集成做铺垫,从以下三点出发: 描述 MyBatis 数据源模块的类图结构: MyBatis 是如何集成第三方数据源组件的: Pool ...
- Jmeter4.0---- jmeter逻辑控制器(16)
1.说明 逻辑控制器可以帮助用户控制Jmeter的测试逻辑,特别是何时发送请求.逻辑控制器可以改变其子测试元件的请求执行顺序. 2.逻辑控制器 (1)如果(if)控制器 用法一: 审核人员,数据分为 ...
- 深入浅出GNU X86-64 汇编
深入浅出GNU X86-64 汇编 来源 https://blog.csdn.net/pro_technician/article/details/78173777 原文 https://www3.n ...