【发布时间】:2017-01-04 23:54:19
【问题描述】:
我正在尝试了解 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))
更多详情请参考Tensorflowword2vec_basic.py
- NCE 函数中的输入和输出矩阵是什么?
在 word2vec 模型中,我们对构建单词表示感兴趣。在训练过程中,给定一个滑动窗口,每个词都会有两个嵌入:1)当词是中心词时; 2)当词是上下文词时。这两个嵌入分别称为输入和输出向量。 (more explanations of input and output matrices)
在我看来,输入矩阵是embeddings,输出矩阵是nce_weights。对吗?
- 什么是最终嵌入?
根据与nce 相关的 s0urcer 的post,它说最终的嵌入矩阵只是输入矩阵。而some others saying,final_embedding=input_matrix+output_matrix。哪个是正确的/更常见的?
【问题讨论】:
-
回答您的第二个问题:您使用哪种策略可能并不重要,但我建议您按照 s0urcer 所说的进行
-
我试图在这篇文章中解释 NCE 损失。请看一下。 linkedin.com/pulse/…
-
@GabrielChu 您发现每个词都有两个嵌入(中心词和上下文词)。您介意确定这些在任何答案中的位置吗?我的印象是隐藏层权重矩阵就是其中之一。但是哪一个?另一个在哪里?
-
好吧,经过一番思考 - 直观上看起来 nce_weights 是“其他”嵌入(毕竟,您正在执行一行与另一列的点积) - 最终它是刚刚被丢弃?
标签: python tensorflow