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.

  1. """ Examples to demonstrate variable sharing
  2. CS 20: 'TensorFlow for Deep Learning Research'
  3. cs20.stanford.edu
  4. Chip Huyen (chiphuyen@cs.stanford.edu)
  5. Lecture 05
  6. """
  7. import os
  8. os.environ['TF_CPP_MIN_LOG_LEVEL']=''
  9.  
  10. import tensorflow as tf
  11.  
  12. x1 = tf.truncated_normal([200, 100], name='x1')
  13. x2 = tf.truncated_normal([200, 100], name='x2')
  14.  
  15. def two_hidden_layers(x):
  16. assert x.shape.as_list() == [200, 100]
  17. w1 = tf.Variable(tf.random_normal([100, 50]), name='h1_weights')
  18. b1 = tf.Variable(tf.zeros([50]), name='h1_biases')
  19. h1 = tf.matmul(x, w1) + b1
  20. assert h1.shape.as_list() == [200, 50]
  21. w2 = tf.Variable(tf.random_normal([50, 10]), name='h2_weights')
  22. b2 = tf.Variable(tf.zeros([10]), name='2_biases')
  23. logits = tf.matmul(h1, w2) + b2
  24. return logits
  25.  
  26. def two_hidden_layers_2(x):
  27. assert x.shape.as_list() == [200, 100]
  28. w1 = tf.get_variable('h1_weights', [100, 50], initializer=tf.random_normal_initializer())
  29. b1 = tf.get_variable('h1_biases', [50], initializer=tf.constant_initializer(0.0))
  30. h1 = tf.matmul(x, w1) + b1
  31. assert h1.shape.as_list() == [200, 50]
  32. w2 = tf.get_variable('h2_weights', [50, 10], initializer=tf.random_normal_initializer())
  33. b2 = tf.get_variable('h2_biases', [10], initializer=tf.constant_initializer(0.0))
  34. logits = tf.matmul(h1, w2) + b2
  35. return logits
  36.  
  37. # logits1 = two_hidden_layers(x1)
  38. # logits2 = two_hidden_layers(x2)

  39. #### Two sets of variables are created. You want all your inputs to use the same weights and biases!
  40. # logits1 = two_hidden_layers_2(x1)
  41. # logits2 = two_hidden_layers_2(x2)
  42.  
  43. ####ValueError: Variable h1_weights already exists,disallowed.Did you mean to set reuse=True in VarScope?
  44.  
  45. # with tf.variable_scope('two_layers') as scope:
  46. # logits1 = two_hidden_layers_2(x1)
  47. # scope.reuse_variables()
  48. # logits2 = two_hidden_layers_2(x2)
  49.  
  50. # with tf.variable_scope('two_layers') as scope:
  51. # logits1 = two_hidden_layers_2(x1)
  52. # scope.reuse_variables()
  53. # logits2 = two_hidden_layers_2(x2)
  54.  
  55. def fully_connected(x, output_dim, scope):
  56. with tf.variable_scope(scope, reuse=tf.AUTO_REUSE) as scope:
  57. w = tf.get_variable('weights', [x.shape[1], output_dim], initializer=tf.random_normal_initializer())
  58. b = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
  59. return tf.matmul(x, w) + b
  60.  
  61. def two_hidden_layers(x):
  62. h1 = fully_connected(x, 50, 'h1')
  63. h2 = fully_connected(h1, 10, 'h2')
  64.  
  65. with tf.variable_scope('two_layers') as scope:
  66. logits1 = two_hidden_layers(x1)
  67. # scope.reuse_variables()
  68. logits2 = two_hidden_layers(x2)
  69.  
  70. writer = tf.summary.FileWriter('./graphs/cool_variables', tf.get_default_graph())
  71. 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. [Agc028A]Two Abbreviations_数学

    Two Abbreviations 题目链接:https://atcoder.jp/contests/agc028/tasks/agc028_a 数据范围:略. 题解: 题目中的位置非常不利于思考,我 ...

  2. curl使用举例

    我在银行工作时,一个具体的用例:shell脚本中使用的 sendAddr=`echo http:192.168.1.100:8080/cloud-monitor/perfaddperf` SendDa ...

  3. 使用pycharm开发web——django2.1.5(四)视图和模板相关

    刘老师说这块很重要..... 应该是很重要,大概看了一下,这里面关于views中函数作用,大概看来可能就是相应请求,传入数据和跳转,基本功能上貌似这些框架都差不多吧(其实我并没用过3个框架以上.... ...

  4. git 使用中报错:LF will be replaced by CRLF in app.json

    git config --global core.autocrlf false //禁用自动转换

  5. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

  6. 为什么要使用 SPL中的 SplQueue实现队列

    今天看php的SPL标准库部分里面涉及到数据结构其中有 SplQueue 来实现队列效果,但是我刚接触php的时候学习到的是 使用array的 array_push 和 array_pop 就可以实现 ...

  7. 【Python基础】13_Python中的PASS

    pass关键字的使用 在程序分支中,如果不想立刻执行该分支,可使用pass占位符,pass不表示任何含义,仅保证程序不会报错. 如: action_str = input("请选择希望执行的 ...

  8. java项目上线的流程(将web项目部署到公网)

    本博文来源于网络,原文的地址在本篇博文最下方. 如何将java web项目上线/部署到公网 关于如何将Java Web上线,部署到公网,让全世界的人都可以访问的问题.小编将作出系列化,完整的流程介绍. ...

  9. Linux Centos7 离线安装docker 【官网翻译和注释】

    Centos7的Docker安装 需要一个维护版本的centos7,所以6不行. 卸载旧版本 旧版本的docker被称为 docker or docker-engine 如果存在请删除它们. sudo ...

  10. Java 并发进阶常见面试题总结

    声明:本文内容完全来自网络,转自GitHub->JavaGuide(https://github.com/Snailclimb/JavaGuide),致谢      1. synchronize ...