七月 上海 | 高性能计算之GPU CUDA培训

7月27-29日三天密集式学习  快速带你入门阅读全文>

正文共364个字,预计阅读时间8分钟。

一、简介

tf.Variable()

1tf.Variable(initial_value=None, trainable=True,    collections=None, validate_shape=True, 2caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, 3import_scope=None)

tf.get_variable()

1tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, 2trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, 3custom_getter=None)

2、区别

1、使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错

1import tensorflow as tf2w_1 = tf.Variable(3,name="w_1")3w_2 = tf.Variable(1,name="w_1")4print w_1.name5print w_2.name6#输出7#w_1:08#w_1_1:0
1import tensorflow as tf23w_1 = tf.get_variable(name="w_1",initializer=1)4w_2 = tf.get_variable(name="w_1",initializer=2)5#错误信息6#ValueError: Variable w_1 already exists, disallowed. Did7#you mean to set reuse=True in VarScope?

2、基于这两个函数的特性,当我们需要共享变量的时候,需要使用tf.get_variable()。在其他情况下,这两个的用法是一样的

 1 import tensorflow as tf 2 3 with tf.variable_scope("scope1"): 4 w1 = tf.get_variable("w1", shape=[]) 5 w2 = tf.Variable(0.0, name="w2") 6 with tf.variable_scope("scope1", reuse=True): 7 w1_p = tf.get_variable("w1", shape=[]) 8 w2_p = tf.Variable(1.0, name="w2") 910 print(w1 is w1_p, w2 is w2_p)11 #输出12 #True  False

由于tf.Variable() 每次都在创建新对象,所有reuse=True 和它并没有什么关系。对于get_variable(),来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。
3、实例

 1import os 2os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 3 4import tensorflow as tf 5 6x1 = tf.truncated_normal([200, 100], name='x1') 7x2 = tf.truncated_normal([200, 100], name='x2') 8 9def two_hidden_layers_1(x):10assert x.shape.as_list() == [200, 100]11w1 = tf.Variable(tf.random_normal([100, 50]), name='h1_weights')12b1 = tf.Variable(tf.zeros([50]), name='h1_biases')13h1 = tf.matmul(x, w1) + b114assert h1.shape.as_list() == [200, 50]15w2 = tf.Variable(tf.random_normal([50, 10]), name='h2_weights')16b2 = tf.Variable(tf.zeros([10]), name='2_biases')17logits = tf.matmul(h1, w2) + b218return logits1920def two_hidden_layers_2(x):21assert x.shape.as_list() == [200, 100]22w1 = tf.get_variable('h1_weights', [100, 50],  initializer=tf.random_normal_initializer())23b1 = tf.get_variable('h1_biases', [50], initializer=tf.constant_initializer(0.0))24h1 = tf.matmul(x, w1) + b125assert h1.shape.as_list() == [200, 50]26w2 = tf.get_variable('h2_weights', [50, 10], initializer=tf.random_normal_initializer())27b2 = tf.get_variable('h2_biases', [10], initializer=tf.constant_initializer(0.0))28logits = tf.matmul(h1, w2) + b229return logits303132def fully_connected(x, output_dim, scope):33with tf.variable_scope(scope, reuse=tf.AUTO_REUSE) as scope:34w = tf.get_variable('weights', [x.shape[1], output_dim], initializer=tf.random_normal_initializer())35b = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))36return tf.matmul(x, w) + b3738def two_hidden_layers_3(x):39h1 = fully_connected(x, 50, 'h1')40h2 = fully_connected(h1, 10, 'h2')41return h242# with tf.variable_scope('two_layers') as scope:43#     logits1 = two_hidden_layers_1(x1) 44#     # scope.reuse_variables()45#     logits2 = two_hidden_layers_1(x2)46# 不会报错47# ---------------4849# with tf.variable_scope('two_layers') as scope:50#     logits1 = two_hidden_layers_2(x1)51#     # scope.reuse_variables()52#     logits2 = two_hidden_layers_2(x2)53# 会报错54# ---------------5556with tf.variable_scope('two_layers') as scope:57logits1 = two_hidden_layers_3(x1)58# scope.reuse_variables()59logits2 = two_hidden_layers_3(x2)60# 不会报错61# -------62writer = tf.summary.FileWriter('./graphs/cool_variables', tf.get_default_graph())63writer.close()

原文链接:https://www.jianshu.com/p/2061b221cd8f

查阅更为简洁方便的分类文章以及最新的课程、产品信息,请移步至全新呈现的“LeadAI学院官网”:

www.leadai.org

请关注人工智能LeadAI公众号,查看更多专业文章

大家都在看

LSTM模型在问答系统中的应用

基于TensorFlow的神经网络解决用户流失概览问题

最全常见算法工程师面试题目整理(一)

最全常见算法工程师面试题目整理(二)

TensorFlow从1到2 | 第三章 深度学习革命的开端:卷积神经网络

装饰器 | Python高级编程

今天不如来复习下Python基础

Tensorflow教程: tf.Variable() 和tf.get_variable()相关推荐

  1. TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别

    TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...

  2. TensorFlow学习笔记(一): tf.Variable() 和tf.get_variable()详解

    对于tf.Variable和tf.get_variable,这两个都是在我们训练模型的时候常遇到的函数,我们首先要知道懂得它的语法格式.常用的语法格式的作用以及在实际代码中是如何调用.如何运行的,运行 ...

  3. tensorflow 变量及命名空间 tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope()

    tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope() 1. 基础功能 1.1 tf.Variable() ...

  4. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  5. tf.Variable()、tf.get_variable()

    - tf.Variable() W = tf.Variable(<initial-value>, name=<optional-name>) 用于生成一个初始值为initial ...

  6. tf.Variable和 tf.get_variable区别(1)

    tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个创建变量函数的区别 先来看看这两个函数的参数列表,就不打了,直接截图 ...

  7. tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope

    tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量 ...

  8. tf.Variable、tf.get_variable、tf.variable_scope、tf.name_scope、random、initializer

    转自:tensorflow学习笔记(二十三):variable与get_variable TF.VARIABLE.TF.GET_VARIABLE.TF.VARIABLE_SCOPE以及TF.NAME_ ...

  9. tf.Variable() 和 tf.get_variable(),tf.name_scope() 和 tf.variable_scope()

    在gpu并行训练网络时,往往需要共享已创建的变量,tensorflow中为了使这些变量名和操作名唯一并且不重复,用了几个函数来应对这种情况.于是就有了tf.Variable(), tf.get_var ...

  10. tf.Variable 和 tf.get_variable的区别(2)

    上上篇博文也写了这个话题,这次自己又敲了一下代码,再次研究了一下关于tf.Variable() 和 tf.get_variable() 的区别, 我就先不说太多,先直接看看代码,再来总结分析,下面代码 ...

最新文章

  1. crontab的用法
  2. 白宫计划2019年春季发布新版人工智能研究战略
  3. 云上人最终产品简易代码
  4. iPhone手机获取uuid 安装测试app
  5. (47)逆向分析 KiSystemService 函数填充 _KTRAP_FRAME 部分
  6. request-promise 获取返回头信息_http返回的状态码 大全
  7. input发送a.jax_与时俱进:在JAX-RS API中采用OpenAPI v3.0.0
  8. element UI表格使用cell-style改变单元格样式
  9. Hadoop大数据生态组件环境安装
  10. Redis入门到精通-姜海强-专题视频课程
  11. Ajax与jQuery、json
  12. vim插件command-t安装
  13. windows 平台 atom编辑器常用快捷键
  14. 视频教程-ArcGIS for Android视频教程-Android
  15. 诺基亚Nokia的PC套件导出短信乱码问题解决(转)
  16. creo绘图属性模板_CREO工程图模板创建
  17. php抽奖中了奖品后怎么处理,抽奖程序,求思路.该怎么处理
  18. abandon connection, owner thread: DubboServerHandler错误原因
  19. 别混淆区别很大 LED网格屏和格栅屏区别对比及分析
  20. Android动画中Interpolator 详解和演示

热门文章

  1. python中key的意思_python中的key是什么意思
  2. mysql存储过程知识点_知识点:Mysql 基本用法之存储过程
  3. C# 实现 rtc_通过Xlua实现unity热更新的一个小例子
  4. mysql qps 索引查询_【MySQL】MySQL配置调优之 QPS/TPS/索引缓存命中率、innoDB索引缓存命中率、查询缓存命中率查看...
  5. 实验四 linux进程控制实验报告,Linux系统进程控制操作系统实验报告4
  6. 陕西省高等数学竞赛_关于参加“陕西高校第十二次大学生高等数学竞赛”的通知...
  7. [leetcode]468. Validate IP Address验证有效IP地址
  8. 集合系列之fail-fast 与fail-safe 区别
  9. jRating五星评级
  10. [BZOJ]2820: YY的GCD