对 tensorflow 中 tf.nn.embedding_lookup 函数的解释
http://stackoverflow.com/questions/34870614/what-does-tf-nn-embedding-lookup-function-do
embedding_lookup
function retrieves rows of the params
tensor. The behavior is similar to using indexing with arrays in numpy. E.g.:
matrix = np.random.random([1024, 64]) # 64-dimensional embeddings
ids = np.array([0, 5, 17, 33])
print matrix[ids] # prints a matrix of shape [4, 64]
params
argument can be also a list of tensors in which case the ids will be distributed among the tensors. E.g. given a list of 3 [2, 64] tensors the default behavior is that they will represent ids: [0, 3], [1, 4], [2, 5]. partition_strategy
controls the way how the ids are distributed among the list. The partitioning is useful for larger scale problems when the matrix might be too large to keep in one piece.
========================
Yes, this function is hard to understand, until you get the point.
In its simplest form, it is similar to tf.gather
. It returns the elements of params
according to the indexes specified by ids
.
For example (assuming you are inside tf.InteractiveSession()
)
params = tf.constant([10,20,30,40])
ids = tf.constant([0,1,2,3])
print tf.nn.embedding_lookup(params,ids).eval()
would return [10 20 30 40], because the first element (index 0) of params is 10, the second element of params (index 1) is 20, etc.
Similarly,
params = tf.constant([10,20,30,40])
ids = tf.constant([1,1,3])
print tf.nn.embedding_lookup(params,ids).eval()
would return: [20 20 40]
But embedding_lookup
is more than that. The params
argument can be a list of tensors, rather than a single tensor.
params1 = tf.constant([1,2])
params2 = tf.constant([10,20])
ids = tf.constant([2,0,2,1,2,3])
result = tf.nn.embedding_lookup([params1, params2], ids)
In such a case, the indexes, specified in ids
, correspond to elements of tensors according to apartition strategy, where the default partition strategy is 'mod'.
In the 'mod' strategy, index 0 corresponds to the first element of the first tensor in the list. Index 1 corresponds to the first element of the second tensor. Index 2 corresponds to the first element of the third tensor, and so on. Simply index i
corresponds to the first element of the (i+1)th tensor , for all the indexes 0..(n-1)
, assuming params is a list of n
tensors.
Now, index n
cannot correspond to tensor n+1, because the list params
contains only n
tensors. So index n
corresponds to the second element of the first tensor. Similarly, index n+1
corresponds to the second element of the second tensor, etc.
So, in the code
params1 = tf.constant([1,2])
params2 = tf.constant([10,20])
ids = tf.constant([2,0,2,1,2,3])
result = tf.nn.embedding_lookup([params1, params2], ids)
index 0 corresponds to the first element of the first tensor: 1
index 1 corresponds to the first element of the second tensor: 10
index 2 corresponds to the second element of the first tensor: 2
index 3 corresponds to the second element of the second tensor: 20
Thus, the result would be:
[ 2 1 2 10 2 20]
对 tensorflow 中 tf.nn.embedding_lookup 函数的解释的更多相关文章
- 【TensorFlow】tf.nn.embedding_lookup函数的用法
tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素.tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量 ...
- tf.nn.embedding_lookup函数的用法
关于np.random.RandomState.np.random.rand.np.random.random.np.random_sample参考https://blog.csdn.net/lanc ...
- tf.nn.embedding_lookup函数【转载】
转自:https://www.cnblogs.com/gaofighting/p/9625868.html //里边有两个很好理解的例子. tf.nn.embedding_lookup(params, ...
- tf.nn.embedding_lookup函数
tf.nn.embedding_lookup(params, ids, partition_strategy='mod', name=None, validate_indices=True, max_ ...
- tf.nn.embedding_lookup()的用法
函数: tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, ...
- tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- 深度学习原理与框架-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] 代码说 ...
- Tensorflow BatchNormalization详解:4_使用tf.nn.batch_normalization函数实现Batch Normalization操作
使用tf.nn.batch_normalization函数实现Batch Normalization操作 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴恩达deeplearnin ...
随机推荐
- VS2015调用Matlab2017a环境配置(转载)
VS2015调用Matlab2017a环境配置 一定要在Debug+x64平台下进行配置,x64,x64,x64!!!* 1.配置环境变量 右键计算机—–>属性——>高级系统设置——> ...
- Geek们为什么都用Linux?《完全使用Linux工作-王垠》读后记
真正开始使用Linux是从2013年某月看到王垠写的一篇<完全用Linux工作>,当时属于无比崇拜王垠的阶段,虽然在那之前常年都在电脑上装着双系统(linux,win),但linux也只能 ...
- ArcGIS教程:“流向”的工作原理
获取表面的水文特征的关键之中的一个是可以确定从栅格中的每一个像元流出的方向.这可通过流向工具来完毕. 该工具把表面作为输入,然后输出一个显示从每一个像元流出方向的栅格. 假设选择了输出下降率栅格数据选 ...
- EasyUI获取DataGrid中某一列的所有值
function count() { var rows = $('#dg'').datagrid('getRows')//获取当前页的数据行 var total = 0; for (var i = 0 ...
- .NET平台下 极光推送
正好看到别人发了个极光的推送例子,想来前面也刚做过这个,就把我的push类共享下 public class JPush { /// <summary> /// push信息到手机应用上 J ...
- 优秀的Android资源
今天,收藏了一下“优秀的Android资源”,以后有时间也学习学习. 一.开发工具 开发android第一步就是安装开发工具SDK,国内有一些机构和公司对些作了境像,这个网站作了详细统计列表:http ...
- Using Notepad++ To Quickly Format XML
http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...
- 面试题思考:IO 和 NIO的区别,NIO优点
面试时答: IO是面向流的,NIO是面向缓冲区的 Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方: NIO则能前后移动流中的数据,因为是面向缓冲区的 ...
- spring boot打包会有.war.original文件的原因 (笔记)
今天使用spring boot 2.1.1.RELEASE版本搭建项目,虽然可以直接打包成可运行的jar包,但是由于公司准备采用docker容器来管理项目,所以需要把jar包变成war包,并且war包 ...
- 【BZOJ1491】[NOI2007]社交网络 Floyd
[BZOJ1491][NOI2007]社交网络 Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子 ...