Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1856959
  • 博文数量: 211
  • 博客积分: 464
  • 博客等级: 下士
  • 技术积分: 3794
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-24 18:25
个人简介

阿弥陀佛

文章分类

全部博文(211)

文章存档

2020年(2)

2019年(3)

2018年(5)

2017年(6)

2016年(10)

2015年(9)

2014年(73)

2013年(90)

2012年(13)

分类: 大数据

2019-08-11 22:56:51

为什么word2vec需要negative sampling?
因为在训练word2vec的时候采用了浅层神经网络方法,对所有的vocabulary 进行 one-hot 编码,这样输出层就是一层softmax layer。而计算softmax layer,必须要将该数据与其他的vocabulary的embedding进行重算,这样非常的耗时。(因为one-hot 的维度有可能是上百万的,计算量太大)
所以采用negative sampling 的方法解决这个问题。negative sampling 也就是近似softmax的形式解决这个问题。
例如 I love Beijing.
2gram 来遍历,正常来讲,训练数据:应该是 (I, love), (love, Beijing)
但是用了NCE来近似softmax 的结果,就是采用选择true+k个negative sample的方式来训练model。
(I, love, 1), (I, the, 0), (I, of, 0)......
(love, Beijing, 1), (love, of, 0) ...... 
例如计算(I, love)的binary logistic 1. (I, the) target 0。
cost function 则选择使用cross-entropy。
代码如下:
这里hidden_ vector 就是input word的embedding。

点击(此处)折叠或打开

  1. real Model::negativeSampling(int32_t target, real lr) {
  2.   real loss = 0.0;
  3.   grad_.zero();
  4.   for (int32_t n = 0; n <= args_->neg; n++) {
  5.     if (n == 0) {
  6.       loss += binaryLogistic(target, true, lr);
  7.     } else {
  8.       loss += binaryLogistic(getNegative(target), false, lr);
  9.     }
  10.   }
  11.   return loss;
  12. }

 binaryLogistic 就是cost的计算过程,如果label 是false的话, cost = (1-0)*LOG(1-score)

点击(此处)折叠或打开

  1. real Model::binaryLogistic(int32_t target, bool label, real lr) {
  2.   real score = sigmoid(wo_->dotRow(hidden_, target));
  3.   real alpha = lr * (real(label) - score);
  4.   grad_.addRow(*wo_, target, alpha);
  5.   wo_->addRow(hidden_, target, alpha);
  6.   if (label) {
  7.     return -log(score);
  8.   } else {
  9.     return -log(1.0 - score);
  10.   }
  11. }


阅读(4759) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~