一、tf.variable变量系列函数

1. tf.Variable与tf.get_variable

tensorflow提供了通过变量名称来创建或者获取一个变量的机制。通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递。 
TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。 
当然,变量也可以通过tf.Varivale来创建。当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价

1

2

3

#以下两个定义是等价的

= tf.get_variable('v', shape=[1], initializer=tf.constant_initializer(1.0))

= tf.Variable(tf.constant(1.0, shape=[1], name='v')

tf.get_varialbe和tf.Variable最大的区别在于:tf.Variable的变量名是一个可选项,通过name=’v’的形式给出。但是tf.get_variable必须指定变量名

2. tf.get_variable与tf.variable_scope

上面已经提到过了:TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。在这里,我主要解释下大家深恶痛绝的reuse问题。 
其实只要记住一件事情就ok了:当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。 
下面我们通过代码来看下:

1

2

3

4

5

6

#reuse=False时会报错的情况:

with tf.variable_scope('foo'):

    = tf.get_variable('v',[1],initializer=tf.constant_initializer(1.0))

with tf.variable_scope('foo'):

    v1 = tf.get_variable('v',[1])

在这种情况下会报错:Variable foo/v already exists, disallowed.Did you mean to set reuse=True in Varscope? 
其原因就是在命名空间foo中创建了相同的变量。如果我要在foo下创建一个变量v1,其name=‘v’,只需要将reuse设置为Ture就ok了。将上面第二部分代码修改为:

1

2

3

with tf.variable_scope('foo', reuse=True):

    v1 = tf.get_variable('v',[1])

    print(v1.name)      #结果为foo/v

当reuse已经设置为True时,tf.variable_scope只能获取已经创建过的变量。这个时候,在命名空间bar中创建name=‘v’的变量v3,将会报错:Variable bar/v dose not exists, diallowed. Did you mean to set reuse=None in VarScope?

1

2

with tf.variable_scope('bar', reuse=True):

    v3 = tf.get_variable('v',[1])

简而言之,reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量

3. tf.variable_scope与tf.name_scope

除了tf.variable_scope,tf.name_scope函数也提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。 
tf.get_variable函数不受tf.name_scope的影响。 
我们从代码看下这句话的具体意思。 
首先是tf.variable_scope:

1

2

3

with tf.variable_scope('foo'):

    = tf.get_variable('bar',[1])

    print(a.name)#结果为foo/bar:0

  

再看tf.name_scope:

1

2

3

4

5

6

with tf.name_scope('a'):

    a=tf.Variable([1])

    print(a.name)#结果为a/Variable:0

    b=tf.get_variable('b',[1])

    print(b.name)#结果为b:0

TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

链接:http://www.cnblogs.com/MY0213/p/9270864.html

二、tf.nn.embedding_lookup函数

tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素。tf.nn.embedding_lookup(params, ids):params可以是张量也可以是数组等,id就是对应的索引,其他的参数不介绍。

例如:

# -*- coding= utf-8 -*-
import tensorflow as tf
import numpy as npa = [[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]]
a = np.asarray(a)
idx1 = tf.Variable([0, 2, 3, 1], tf.int32)
idx2 = tf.Variable([[0, 2, 3, 1], [4, 0, 2, 2]], tf.int32)
out1 = tf.nn.embedding_lookup(a, idx1)
out2 = tf.nn.embedding_lookup(a, idx2)
init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)print sess.run(out1)print out1print '=================='print sess.run(out2)print out2输出:
[[ 0.1  0.2  0.3][ 2.1  2.2  2.3][ 3.1  3.2  3.3][ 1.1  1.2  1.3]]
Tensor("embedding_lookup:0", shape=(4, 3), dtype=float64)
==================
[[[ 0.1  0.2  0.3][ 2.1  2.2  2.3][ 3.1  3.2  3.3][ 1.1  1.2  1.3]][[ 4.1  4.2  4.3][ 0.1  0.2  0.3][ 2.1  2.2  2.3][ 2.1  2.2  2.3]]]
Tensor("embedding_lookup_1:0", shape=(2, 4, 3), dtype=float64)

tf.nn.embedding_lookup,tf.variable系列变量相关推荐

  1. 深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)...

    1. tf.nn.embedding_lookup(W, X) W的维度为[len(vocabulary_list), 128], X的维度为[?, 8],组合后的维度为[?, 8, 128] 代码说 ...

  2. tf.nn.embedding_lookup函数的用法

    tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(params, ids):params可以是张量也可以是数组等 ...

  3. 深度学习-函数-tf.nn.embedding_lookup 与tf.keras.layers.Embedding

    embedding函数用法 1. one_hot编码 1.1. 简单对比 1.2.优势分析: 1.3. 缺点分析: 1.4. 延伸思考 2. embedding的用途 2.1 embedding有两个 ...

  4. tf.nn.embedding_lookup()函数

    一.tf.nn.embedding_lookup() tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tens ...

  5. tf.nn.embedding_lookup()的用法

    函数: tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, ...

  6. tf.nn.embedding_lookup

    用法: a1 = tf.nn.embedding_lookup(a, index) index是索引,a是输入,通过index来选取a中对应的元素返回给a1,注意index是从0开始算起 例子: im ...

  7. tf.nn,tf.layers, tf.contrib模块介绍

    一.tf.nn,tf.layers, tf.contrib概述 我们在使用tensorflow时,会发现tf.nn,tf.layers, tf.contrib模块有很多功能是重复的,尤其是卷积操作,在 ...

  8. tf.nn.conv2d() / tf.nn.depthwise_conv2d() 和 Batchsize效益

    1. 卷积函数tf.nn.conv2d() tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_form ...

  9. tf.nn 和tf.layers以及tf.contrib.layers的简单区别(转)

    tensorflow不同层的使用(tf.nn 和tf.layers以及tf.contrib.layers)的简单区别(转) 2018年09月02日 18:50:33 holmes_MX 版权声明:原创 ...

  10. TensorFlow 学习(七) — 常用函数 api、tf.nn、tf.keras

    0. 四则运算 平方:tf.square(),开方:tf.sqrt() tf.add().tf.sub().tf.mul().tf.div().tf.mod().tf.abs().tf.neg() 1 ...

最新文章

  1. LIVE 预告 | 南方科大张宇:神经网络可解释性综述
  2. from __future__ import absolute_import用法心得小结
  3. key可以重复的map集合:IdentityHashMap
  4. 图像处理和计算机视觉中的经典论文
  5. SAP本地化-银企直连
  6. WPF 自定义快捷键命令(COMMAND)(转)
  7. 从零开始学android开发-IDE空间不够报错
  8. 使用jsp打印HTTP请求头部所有字段的值
  9. emacs python ide_Emacs Python IDE win7 x64
  10. 菜鸟裹裹电脑版_【绵阳最新转让】3500低价出售家用制氧机!东芝i5笔记本电脑、索尼微单相机、联想笔记本电脑、奶茶店、服装店转让......
  11. springboot监控服务器信息,面试官:聊一聊SpringBoot服务监控机制
  12. 砸115亿元入局!后知后觉的欧洲重金建设AI,为赶中超美
  13. cocos+kbe问题记录
  14. SQL 基础之单行函数(七)
  15. Android 制定安装重写迁移至SD卡 APP2SD
  16. 人工智能 一种现代方法 第3章 通过搜索进行问题求解
  17. 计算机电源烧保险,ATX电脑电源保险换了就烧怎么维修?
  18. 测试转开发,我都经历了什么
  19. zkw线段树(详解)
  20. redis应用之安装配置介绍

热门文章

  1. rpm 安装、卸载软件命令 ——以nginx为例
  2. Netty优雅退出机制和原理
  3. 贝叶斯推断之最大后验概率(MAP)
  4. 20171201-构建之法:现代软件工程-阅读笔记
  5. 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
  6. 成员变量的隐藏,方法的覆盖,super关键字
  7. 高可用Kubernetes集群原理介绍
  8. 多普达D700手机利用USB电缆与电脑连接上网方法
  9. 在moss上自己总结了点小经验。。高手可以飘过
  10. make clean、make mrproper、make distclean的区别【转】