码迷,mamicode.com
首页 > 其他好文 > 详细

word2vec中 distence.c 文件源码分析

时间:2015-04-09 22:01:15      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:word2vec   机器学习   

#include <stdio.h>
#include <string.h>
#include <math.h>
//#include <malloc.h>
#include <stdlib.h>

const long long max_size = 2000;         // max length of strings
const long long N = 5;                  // number of closest words that will be shown
const long long max_w = 50;              // max length of vocabulary entries

int main(int argc, char **argv) {
  FILE *f;
  char st1[max_size];
  char *bestw[N];//指针数组,大小为N,其中的每一个元素指向char型的指针。
  char file_name[max_size], st[100][max_size];
  float dist, len, bestd[N], vec[max_size];
  long long words, size, a, b, c, d, cn, bi[100];
  char ch;
  float *M;
  char *vocab;
  if (argc < 2) {
    printf("Usage: ./distance <FILE>\nwhere FILE contains word projections in the BINARY FORMAT\n");
    return 0;
  }
  strcpy(file_name, argv[1]);
  f = fopen(file_name, "rb");
  if (f == NULL) {
    printf("Input file not found\n");
    return -1;
  }
  fscanf(f, "%lld", &words);//vocab_size
  fscanf(f, "%lld", &size); //维数
  vocab = (char *)malloc((long long)words * max_w * sizeof(char));
  for (a = 0; a < N; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
  M = (float *)malloc((long long)words * (long long)size * sizeof(float));
  if (M == NULL) {
    printf("Cannot allocate memory: %lld MB    %lld  %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
    return -1;
  }
  for (b = 0; b < words; b++) {
    a = 0;
    while (1) {
      vocab[b * max_w + a] = fgetc(f);
      if (feof(f) || (vocab[b * max_w + a] == ‘ ‘)) break;
      if ((a < max_w) && (vocab[b * max_w + a] != ‘\n‘)) a++;
    }
    vocab[b * max_w + a] = 0;
    for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
    len = 0;
    for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
    len = sqrt(len);
    for (a = 0; a < size; a++) M[a + b * size] /= len;//将坐标归一化
  }
  fclose(f);

  while (1) {
    for (a = 0; a < N; a++) bestd[a] = 0;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    printf("Enter word or sentence (EXIT to break): ");
    a = 0;
    while (1) {
      st1[a] = fgetc(stdin);
      if ((st1[a] == ‘\n‘) || (a >= max_size - 1)) {
        st1[a] = 0;
        break;
      }
      a++;
    }
    printf("st1:%s words:%lld \n",st1,words);
    if (!strcmp(st1, "EXIT")) break;
    cn = 0;
    b = 0;
    c = 0;
    while (1) {//把st中的每个单词分别存储到st1二维数组中,共cn个。
      st[cn][b] = st1[c];
      b++;
      c++;
      st[cn][b] = 0;
      if (st1[c] == 0) break;
      if (st1[c] == ‘ ‘) {
        cn++;
        b = 0;
        c++;
      }
    }
    cn++;
    for (a = 0; a < cn; a++) {
      for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
      if (b == words) b = -1;
      bi[a] = b;

      printf("\nWord: %s  Position in vocabulary: %lld\n", st[a], bi[a]);
      if (b == -1) {
        printf("Out of dictionary word!\n");
        break;       //只要有一个词不在词汇表都终止for循环
      }
    }
    if (b == -1) continue;
    printf("\n                                              Word       Cosine distance\n------------------------------------------------------------------------\n");

    for (a = 0; a < size; a++) vec[a] = 0;

    for (b = 0; b < cn; b++) {//遍历每个词,如果输入多个词vec[a]是各个词向量的累加和
      if (bi[b] == -1) continue;
      for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
    }

    len = 0;
    for (a = 0; a < size; a++) len += vec[a] * vec[a];
    len = sqrt(len);
    for (a = 0; a < size; a++) vec[a] /= len;//将vec归一化,当只输入一个词时,不起作用。

    for (a = 0; a < N; a++) bestd[a] = -1;
    for (a = 0; a < N; a++) bestw[a][0] = 0;

    //由于查询词和词汇表的词向量都做了归一化,所以余弦相似度等价于向量的内积,内积越大越相似
    for (c = 0; c < words; c++) {//遍历词汇表
      a = 0;

      for (b = 0; b < cn; b++)   //a的作用:如果遍历词和查询词相同,则跳过此词
        if (bi[b] == c) a = 1;
      if (a == 1) continue;

      dist = 0;
      for (a = 0; a < size; a++)  //求向量的内积
        dist += vec[a] * M[a + c * size];

      for (a = 0; a < N; a++) {   //为dist寻找插入位置
        if (dist > bestd[a]) {         
          for (d = N - 1; d > a; d--) {
            bestd[d] = bestd[d - 1];
            strcpy(bestw[d], bestw[d - 1]);
          }
          bestd[a] = dist;
          strcpy(bestw[a], &vocab[c * max_w]);
          break;
        }
      }
    }
    for (a = 0; a < N; a++) printf("%50s\t\t%f\n", bestw[a], bestd[a]);
  }
  return 0;
}

word2vec中 distence.c 文件源码分析

标签:word2vec   机器学习   

原文地址:http://blog.csdn.net/li8630/article/details/44964595

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!