首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >理解tensorflow中的`tf.nn.nce_loss()`

理解tensorflow中的`tf.nn.nce_loss()`
EN

Stack Overflow用户
提问于 2017-01-05 07:54:20
回答 2查看 15K关注 0票数 24

我正在尝试理解Tensorflow中的NCE损失函数。NCE损失用于word2vec任务,例如:

# Look up embeddings for inputs.
embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)

# Construct the variables for the NCE loss
nce_weights = tf.Variable(
    tf.truncated_normal([vocabulary_size, embedding_size],
                        stddev=1.0 / math.sqrt(embedding_size)))
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

# Compute the average NCE loss for the batch.
# tf.nce_loss automatically draws a new sample of the negative labels each
# time we evaluate the loss.
loss = tf.reduce_mean(
    tf.nn.nce_loss(weights=nce_weights,
                   biases=nce_biases,
                   labels=train_labels,
                   inputs=embed,
                   num_sampled=num_sampled,
                   num_classes=vocabulary_size))

更多详细信息,请参考Tensorflow word2vec_basic.py

  1. NCE函数中的输入和输出矩阵是什么?

在word2vec模型中,我们对构建单词的表示感兴趣。在训练过程中,给定一个滑动窗口,每个单词将有两个嵌入: 1)当单词是中心词时;2)当单词是上下文词时。这两个嵌入分别称为输入向量和输出向量。(more explanations of input and output matrices)

在我看来,输入矩阵是embeddings,输出矩阵是nce_weights。是对的吗?

  1. 最终的嵌入是什么?

根据s0urcer的post也与nce有关,它说最终的嵌入矩阵就是输入矩阵。而,some others sayingfinal_embedding=input_matrix+output_matrix。哪一个是正确的/更常见的?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41475180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档