这个函数把最后的问题转化为了num_sampled+num_real个两类分类问题,然后每个分类问题用了交叉熵的损伤函数,也就是logistic regression常用的损失函数。TF里还提供了一个softmax_cross_entropy_with_logits的函数,和这个有所区别。 word2vec里面也有使用nec_loss: loss = tf.reduce_mean( tf.nn.nce_loss(nce_weig...
tf.nn.nce_loss是word2vec的skip-gram模型的负例采样方式的函数,下面分析其源代码。 1 上下文代码 其中, train_inputs中的就是中心词,train_label中的就是语料库中该中心词在滑动窗口内的上下文词。 所以,train_inputs中会有连续n-1(n为
return array_ops.reshape(math_ops.matmul(x, ones), [-1]) 最后,再对nce_loss的返回结果用reduce_mean即可计算一个batch的平均损失。 关于_compute_sampled_logits中如何采样,如何计算的,这里就不再阐述,同文字理论是一样的。 我们将_compute_sampled_logits函数中的 # Construct output logits and labels. T...
tf.nn.nce_loss是word2vec的skip-gram模型的负例采样⽅式的函数,下⾯分析其源代码。1 上下⽂代码 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))其中,train_inputs = tf....