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 ...
随机推荐
- 《剑指offer》递推与循环,栈和队列,回溯 (牛客10.27)
难度 题目 知识点 07. 斐波那契数列 递推递归 - 两变量写法- 08. 跳台阶 同上 09. 变态跳台阶 dp 10. 矩形覆盖 同上 05. 用两个栈实现队列 模拟 ☆ 20. 包含min函数 ...
- [国外] 解决Windows10下google搜索连接不上,但其它网页都能成功登入的问题
我的电脑突然在学校不能登陆google了,在家可以连接但是非常慢. 在网上搜索发现是由于DNS出了问题,在此记录一下. 可以通过控制面板——网络和Internet——网络和共享中心——点击现在使用的网 ...
- CSharp实体生成器
专门为C#开发人员定制的一个实体生成器,以往的生成器功能达到了,但是很多细节未考虑到,所以我借鉴“先人”的一些已有的东西,重新定制了一个.当然,需要源码的同学,直接使用IL Spy这个小工具就可以看到 ...
- beego conf配置文件
1. 多个配置文件通过include引入 自定义配置文件mysql.conf 在app.conf 中引入mysql.conf include "mysql.conf"
- 顺序表的基本操作【c语言】【创建、插入、删除、输出】
作为数据结构初学者,上课时对一些知识点掌握得不是很透彻,所以利用课余时间通过微博平台总结所学知识,加深对知识的见解,记录学习历程便于后需要时参考. #include<stdio.h> #i ...
- vim中文帮助手册的安装
1. 下载: 下载页面:http://vimcdoc.sourceforge.net/ 选择“Latest platform independent tarball, including an Lin ...
- MySQL函数和过程(三)
--加密32位字符select md5('123456') --获取字符串的长度(一个中文三个长度)select LENGTH('呵呵') --获取字符串字符个数select CHAR_LENGTH( ...
- 网络流Dinic--模板
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio>//sprintf islower isupp ...
- hadoop 》》 django 简单操作hdfs 语句
>> from django.shortcuts import render # Create your views here. from hdfs.client import Clien ...
- LeetCode 328——奇偶链表(JAVA)
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1),时 ...