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的更多相关文章

  1. 伪共享(false sharing),并发编程无声的性能杀手

    在并发编程过程中,我们大部分的焦点都放在如何控制共享变量的访问控制上(代码层面),但是很少人会关注系统硬件及 JVM 底层相关的影响因素.前段时间学习了一个牛X的高性能异步处理框架 Disruptor ...

  2. Salesforce的sharing Rule 不支持Lookup型字段解决方案

    Salesforce 中 sharing rule 并不支持Look up 字段 和 formula 字段.但在实际项目中,有时会需要在sharing rule中直接取Look up型字段的值,解决方 ...

  3. [Erlang 0127] Term sharing in Erlang/OTP 上篇

    之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...

  4. 跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享

    在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...

  5. 006_Salesforce Sharing 使用说明

    Salesforce Sharing 使用说明 背景说明:Salesforce共享实施记录和其它数据时,需要员工之间共享或多个用户在一个组织间的共享.然而,共享这些数据是有风险的,尤其是当它涉及到敏感 ...

  6. salesforce 零基础开发入门学习(十二)with sharing 、without sharing 、无声明区别

    在salesforce中,声明类大概可以分成三类:分别是可以声明为with sharing,without sharing,以及两者均不声明. public with sharing class A ...

  7. Cross-Origin Resource Sharing协议介绍

    传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片或者脚本.但是Javascript脚 ...

  8. [转]Stop Sharing Session State between Multiple Tabs of Browser

    本文转自:http://jinaldesai.net/stop-sharing-session-state-between-multiple-tabs-of-browser/ Scenario: By ...

  9. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

随机推荐

  1. 有关IP和子网掩码的总结

    从之前写的那个概括的文章之后,具体细节知识我也不知道写啥子,最近忙于毕业论文和考证,所以就总结一下这方面的知识,本篇的话就先从IP说起. IP这方面有IPv4和IPv6这两方面的内容,这里就只说IPv ...

  2. 【DSP开发技术】影响高性能DSP功耗的因素及其优化方法

    影响高性能DSP功耗的因素及其优化方法 德州仪器DSP技术应用工程师 冯华亮 摘要 本文讨论影响高性能DSP功耗的因素,介绍一些DSP功耗的优化方法. 随着嵌入式应用需求的不断提高,DSP的速度也不断 ...

  3. Java文章翻译

    一.基础 1.String 使用" "还是构造函数创建字符串? 画图说明字符串的不变性 在Java中字符串为什么是不可变的 Java中的字符串是按引用传递? 排名前十的Java字符 ...

  4. 前端手势控制图片插件书写二(transform矩阵的原理)

    上次解释了如何使用代码识别双指和单指操作及放大和旋转拖动操作.这次解释下css3的transform原理 一.transform矩阵原理 transform: matrix(a,b,c,d,e,f) ...

  5. Mysql错误--Table 'mysql.servers' doesn't exist.

    问题: 今天在初始化数据库的时候,在配置文件里加了"--skip grant tables",登陆进去之后,发现无法修改root密码,报这个错误.   Table 'mysql.s ...

  6. 记录一次hadoop2.8.4版本RM接入zk ha问题

    背景: 公司将线上hadoop RM接入ZK 实现高可用 但ZK Znode 默认存储1M,当存储数据量大时候可能导致线上业务的崩溃 处理方案如下: 1,修改ZK配置 增加默认存储上限 2,修改RM数 ...

  7. HDU 4614 线段树+二分查找

    Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is s ...

  8. NOIP比赛中如何加速c++的输入输出

    NOIP比赛中如何加速c++的输入输出 在竞赛中,遇到大数据时,往往需要更快的读取方式.由于比赛中输出一般规模较小,本文只讨论输入如何加速. 现在我们生成1000000个随机数,构成1000*1000 ...

  9. 学习GTK+ (1) ——编写helloworld

    环境 我使用的是新安装的manjaro 18.1 (kde版),安装新系统后后直接可以开始写代码,不需要安装各种调用的库等. 推荐一个网站,gnome开发者 https://developer.gno ...

  10. 在生产环境中使用Compose 【翻译】

    在生产环境中使用Compose 在开发环境中使用Compose定义你的应用,可以使用此定义在不同的环境,(如 CI.暂存和生产)中运行应用程序. 部署应用程序的最简单方法是在单个服务器上运行该应用程序 ...