tf.contrib.layers.embed_sequence()是做什么用的? [英] tf.contrib.layers.embed_sequence() is for what?

查看:167
本文介绍了tf.contrib.layers.embed_sequence()是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最新的 Tensorflow 示例中找到了 tf.contrib.layers.embed_sequence()函数,但这是不包含在主API中。我不知道为什么

I found tf.contrib.layers.embed_sequence() function in the lastest Tensorflow examples, but it is not included in the main API . I don't know why. Any explanation about how it works would be appreciated.

推荐答案

我可以想到 tensorflow的两个主要原因.contrib.layers.embed_sequence 很有用:


  1. 建立具有多个门的神经网络模型时通过使用 tensorflow.contrib.layers.embed_sequence 作为功能,您可以在保留深度的同时减少网络中的参数数量。例如,它消除了LSTM的每个门执行自己的特征线性投影的需要。

  2. 它允许任意输入形状,这有助于实现简单而灵活的实现。 / li>
  1. When building a neural network model that has multiple gates that take features as input, by using tensorflow.contrib.layers.embed_sequence, you can reduce the number of parameters in your network while preserving depth. For example, it eliminates the need for each gates of the LSTM to perform its own linear projection of features.
  2. It allows for arbitrary input shapes, which helps the implementation be simple and flexible.

让我们说我有一个看起来像这样的数据集:

Let us say that I have a data set which looks something like this:

[(城市中的垃圾堆,垃圾),
(城市塞满了车辆,交通)]

我想取每个元组的第一个元素,它是单词序列。单词需要以矢量形式嵌入。
第一步,应将它们转换为索引或数字。例如,在这种情况下,
的词汇将是:

I want to take the first element of each tuple which is a sequence of words. The words need to be embedded in a vector form. As the first step, they should be converted as indices or numbers. For example, in this case, the vocabulary will be:

vocab = [{'garbage':1},
         {'piles':2},
         {'in':3},
         {'the':4},
         {'city':5},
         {'is':6},
         {'clogged':7},
         {'with':8},
         {'vehicles':9}]

编码后的文本如下:

features = [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]]

您将此编码文本作为功能批量传递给该函数:

You pass this encoded text as features to this function in batches:

features_embedded = tf.contrib.layers.embed_sequence(
    ids=features,
    vocab_size=len(vocab),
    embed_dim=EMBEDDING_SIZE,
    scope='words'
)

现在,每个单词使用索引(1到5)表示的值嵌入到大小为 EMBEDDING_SIZE 的向量中。

Now, every word which is represented using the indices (1 to 5), becomes embedded into a vector of size EMBEDDING_SIZE.

如果批处理大小为2(即一批中有2个序列),并且 EMBEDDING_SIZE 为10,则输出将是形状为(2,5,10)

If the batch size is 2 (ie. 2 sequences in one batch) and EMBEDDING_SIZE is 10, the output will be a matrix of shape (2, 5, 10)

示例输出:

[[[0.1, 0.3, 0.4, 0.2, 0.5, 0.2, 0.2, 0.2, 0.4, 0.1], # garbage
  [0.1, 0.3, 0.4, 0.2, 0.5, 0.2, 0.1, 0.2, 0.4, 0.1], # piles
  [0.1, 0.3, 0.4, 0.2, 0.5, 0.2, 0.4, 0.2, 0.4, 0.1], # in
  [0.1, 0.3, 0.4, 0.2, 0.5, 0.3, 0.1, 0.2, 0.4, 0.1], # the
  [0.1, 0.3, 0.4, 0.2, 0.5, 0.2, 0.1, 0.2, 0.4, 0.6]], # city
 [sent2]]

send2 的编码方式类似(5 x 10矩阵)。

sent2 is encoded similarly ( 5 x 10 matrix).

希望这很清楚。

这篇关于tf.contrib.layers.embed_sequence()是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆