python 워드 2 vec 작업 초보 구현

12015 단어 pythonword2vec
머리말
처음에 word2vec 환경의 설치 가 매우 복잡 한 것 을 보 았 는데 반나절 을 설 치 했 지만 Cygwin 도 잘 몰 랐 다.나중에 갑자기 내 가 왜 안 c 언어 버 전 으로 가 야 하 는 지 알 게 되 었 다.나 는 python 버 전 을 사용 해 야 하 는데 gensim 을 발견 했다.gensim 가방 을 설치 하면 word2vec 를 사용 할 수 있 지만 gensim 은 word2vec 안의 skip-gram 모델 만 실현 했다.다른 모델 을 사용 하려 면 다른 언어의 word2vec 를 연구 해 야 한다.
준비
gensim 패키지 가 생 긴 후에 인터넷 에서 많은 튜 토리 얼 을 보면 txt 파일 이 직접 들 어 옵 니 다.그러나 이 txt 파일 은 어떻게 생 겼 고 어떤 데이터 형식 입 니까?많은 블 로그 들 이 설명 하지 않 았 고 다운로드 할 수 있 는 txt 파일 을 제공 하지 않 았 습 니 다.더 이해 해 보 니 이 txt 는 거대 한 텍스트 를 포함 하 는 좋 은 단어 가 있 는 파일 입 니 다.다음 그림 에서 보 듯 이 제 가 훈련 한 언어 자료 입 니 다.저 는 파충류 로 잡 은 7000 개의 뉴스 를 언어 자료 로 삼 아 단 어 를 나 누 었 습 니 다.단어 와 단어 사이 에는 반드시 빈 칸 을 사용 해 야 합 니 다.

여기 서 단 어 는 말 을 더 듬 는 단 어 를 사용한다.
이 부분의 코드 는 다음 과 같다.

import jieba
f1 =open("fenci.txt")
f2 =open("fenci_result.txt", 'a')
lines =f1.readlines() #       
for line in lines:
  line.replace('\t', '').replace('
', '').replace(' ','') seg_list = jieba.cut(line, cut_all=False) f2.write(" ".join(seg_list)) f1.close() f2.close()
그리고 주의해 야 할 것 은 바로 언어 자료 중의 텍스트 가 반드시 많아 야 한 다 는 것 이다.인터넷 에서 보 는 모든 언어 자 료 는 여러 개의 G 이 고 처음에 나 는 뉴스 를 성어 자료 창고 로 사 용 했 는데 결과 가 매우 좋 지 않 고 수출 이 모두 0 이 었 다.그리고 나 서 나 는 7000 개의 뉴스 를 언어 자료 창고 로 사용 하여 단 어 를 나 눈 후에 얻 은 fenciresult.txt 는 20m 로 크 지 않 지만 이미 초보적인 결 과 를 얻 을 수 있 습 니 다.
3.gensim 의 word2vec 훈련 모델 사용
관련 코드 는 다음 과 같 습 니 다.

from gensim.modelsimport word2vec
import logging
 
#    
logging.basicConfig(format='%(asctime)s:%(levelname)s: %(message)s', level=logging.INFO)
sentences =word2vec.Text8Corpus(u"fenci_result.txt") #     
model =word2vec.Word2Vec(sentences, size=200) #  skip-gram  ,  window=5
 
print model
#          /    
try:
  y1 = model.similarity(u"  ", u"   ")
except KeyError:
  y1 = 0
print u"【  】 【   】     :", y1
print"-----
" # # y2 = model.most_similar(u" ", topn=20) # 20 print u" 【 】 :
" for item in y2: print item[0], item[1] print"-----
" # print u" - , -" y3 =model.most_similar([u' ', u' '], [u' '], topn=3) for item in y3: print item[0], item[1] print"----
" # y4 =model.doesnt_match(u" ".split()) print u" :", y4 print"-----
" # , model.save(u" .model") # # model_2 =word2vec.Word2Vec.load("text8.model") # c #model.save_word2vec_format(u" .model.bin", binary=True) # # model_3 =word2vec.Word2Vec.load_word2vec_format("text8.model.bin",binary=True)
출력 은 다음 과 같 습 니 다:

"D:\program files\python2.7.0\python.exe" "D:/pycharm workspace/  /cluster_test/word2vec.py"
D:\program files\python2.7.0\lib\site-packages\gensim\utils.py:840: UserWarning: detected Windows; aliasing chunkize to chunkize_serial
 warnings.warn("detected Windows; aliasing chunkize to chunkize_serial")
D:\program files\python2.7.0\lib\site-packages\gensim\utils.py:1015: UserWarning: Pattern library is not installed, lemmatization won't be available.
 warnings.warn("Pattern library is not installed, lemmatization won't be available.")
2016-12-12 15:37:43,331: INFO: collecting all words and their counts
2016-12-12 15:37:43,332: INFO: PROGRESS: at sentence #0, processed 0 words, keeping 0 word types
2016-12-12 15:37:45,236: INFO: collected 99865 word types from a corpus of 3561156 raw words and 357 sentences
2016-12-12 15:37:45,236: INFO: Loading a fresh vocabulary
2016-12-12 15:37:45,413: INFO: min_count=5 retains 29982 unique words (30% of original 99865, drops 69883)
2016-12-12 15:37:45,413: INFO: min_count=5 leaves 3444018 word corpus (96% of original 3561156, drops 117138)
2016-12-12 15:37:45,602: INFO: deleting the raw counts dictionary of 99865 items
2016-12-12 15:37:45,615: INFO: sample=0.001 downsamples 29 most-common words
2016-12-12 15:37:45,615: INFO: downsampling leaves estimated 2804247 word corpus (81.4% of prior 3444018)
2016-12-12 15:37:45,615: INFO: estimated required memory for 29982 words and 200 dimensions: 62962200 bytes
2016-12-12 15:37:45,746: INFO: resetting layer weights
2016-12-12 15:37:46,782: INFO: training model with 3 workers on 29982 vocabulary and 200 features, using sg=0 hs=0 sample=0.001 negative=5 window=5
2016-12-12 15:37:46,782: INFO: expecting 357 sentences, matching count from corpus used for vocabulary survey
2016-12-12 15:37:47,818: INFO: PROGRESS: at 1.96% examples, 267531 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:37:48,844: INFO: PROGRESS: at 3.70% examples, 254229 words/s, in_qsize 3, out_qsize 1
2016-12-12 15:37:49,871: INFO: PROGRESS: at 5.99% examples, 273509 words/s, in_qsize 3, out_qsize 1
2016-12-12 15:37:50,867: INFO: PROGRESS: at 8.18% examples, 281557 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:37:51,872: INFO: PROGRESS: at 10.20% examples, 280918 words/s, in_qsize 5, out_qsize 0
2016-12-12 15:37:52,898: INFO: PROGRESS: at 12.44% examples, 284750 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:37:53,911: INFO: PROGRESS: at 14.17% examples, 278948 words/s, in_qsize 0, out_qsize 0
2016-12-12 15:37:54,956: INFO: PROGRESS: at 16.47% examples, 284101 words/s, in_qsize 2, out_qsize 1
2016-12-12 15:37:55,934: INFO: PROGRESS: at 18.60% examples, 285781 words/s, in_qsize 6, out_qsize 1
2016-12-12 15:37:56,933: INFO: PROGRESS: at 20.84% examples, 288045 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:37:57,973: INFO: PROGRESS: at 23.03% examples, 289083 words/s, in_qsize 6, out_qsize 2
2016-12-12 15:37:58,993: INFO: PROGRESS: at 24.87% examples, 285990 words/s, in_qsize 6, out_qsize 1
2016-12-12 15:38:00,006: INFO: PROGRESS: at 27.17% examples, 288266 words/s, in_qsize 4, out_qsize 1
2016-12-12 15:38:01,081: INFO: PROGRESS: at 29.52% examples, 290197 words/s, in_qsize 1, out_qsize 2
2016-12-12 15:38:02,065: INFO: PROGRESS: at 31.88% examples, 292344 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:03,188: INFO: PROGRESS: at 34.01% examples, 291356 words/s, in_qsize 2, out_qsize 2
2016-12-12 15:38:04,161: INFO: PROGRESS: at 36.02% examples, 290805 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:05,174: INFO: PROGRESS: at 38.26% examples, 292174 words/s, in_qsize 3, out_qsize 0
2016-12-12 15:38:06,214: INFO: PROGRESS: at 40.56% examples, 293297 words/s, in_qsize 4, out_qsize 1
2016-12-12 15:38:07,201: INFO: PROGRESS: at 42.69% examples, 293428 words/s, in_qsize 4, out_qsize 1
2016-12-12 15:38:08,266: INFO: PROGRESS: at 44.65% examples, 292108 words/s, in_qsize 1, out_qsize 1
2016-12-12 15:38:09,295: INFO: PROGRESS: at 46.83% examples, 292097 words/s, in_qsize 4, out_qsize 1
2016-12-12 15:38:10,315: INFO: PROGRESS: at 49.13% examples, 292968 words/s, in_qsize 2, out_qsize 2
2016-12-12 15:38:11,326: INFO: PROGRESS: at 51.37% examples, 293621 words/s, in_qsize 5, out_qsize 0
2016-12-12 15:38:12,367: INFO: PROGRESS: at 53.39% examples, 292777 words/s, in_qsize 2, out_qsize 2
2016-12-12 15:38:13,348: INFO: PROGRESS: at 55.35% examples, 292187 words/s, in_qsize 5, out_qsize 0
2016-12-12 15:38:14,349: INFO: PROGRESS: at 57.31% examples, 291656 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:15,374: INFO: PROGRESS: at 59.50% examples, 292019 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:16,403: INFO: PROGRESS: at 61.68% examples, 292318 words/s, in_qsize 4, out_qsize 2
2016-12-12 15:38:17,401: INFO: PROGRESS: at 63.81% examples, 292275 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:18,410: INFO: PROGRESS: at 65.71% examples, 291495 words/s, in_qsize 4, out_qsize 1
2016-12-12 15:38:19,433: INFO: PROGRESS: at 67.62% examples, 290443 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:20,473: INFO: PROGRESS: at 69.58% examples, 289655 words/s, in_qsize 6, out_qsize 2
2016-12-12 15:38:21,589: INFO: PROGRESS: at 71.71% examples, 289388 words/s, in_qsize 2, out_qsize 2
2016-12-12 15:38:22,533: INFO: PROGRESS: at 73.78% examples, 289366 words/s, in_qsize 0, out_qsize 1
2016-12-12 15:38:23,611: INFO: PROGRESS: at 75.46% examples, 287542 words/s, in_qsize 5, out_qsize 1
2016-12-12 15:38:24,614: INFO: PROGRESS: at 77.25% examples, 286609 words/s, in_qsize 3, out_qsize 0
2016-12-12 15:38:25,609: INFO: PROGRESS: at 79.33% examples, 286732 words/s, in_qsize 5, out_qsize 1
2016-12-12 15:38:26,621: INFO: PROGRESS: at 81.40% examples, 286595 words/s, in_qsize 2, out_qsize 0
2016-12-12 15:38:27,625: INFO: PROGRESS: at 83.53% examples, 286807 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:28,683: INFO: PROGRESS: at 85.32% examples, 285651 words/s, in_qsize 5, out_qsize 3
2016-12-12 15:38:29,729: INFO: PROGRESS: at 87.56% examples, 286175 words/s, in_qsize 6, out_qsize 1
2016-12-12 15:38:30,706: INFO: PROGRESS: at 89.86% examples, 286920 words/s, in_qsize 5, out_qsize 0
2016-12-12 15:38:31,714: INFO: PROGRESS: at 92.10% examples, 287368 words/s, in_qsize 6, out_qsize 0
2016-12-12 15:38:32,756: INFO: PROGRESS: at 94.40% examples, 288070 words/s, in_qsize 4, out_qsize 2
2016-12-12 15:38:33,755: INFO: PROGRESS: at 96.30% examples, 287543 words/s, in_qsize 1, out_qsize 0
2016-12-12 15:38:34,802: INFO: PROGRESS: at 98.71% examples, 288375 words/s, in_qsize 4, out_qsize 0
2016-12-12 15:38:35,286: INFO: worker thread finished; awaiting finish of 2 more threads
2016-12-12 15:38:35,286: INFO: worker thread finished; awaiting finish of 1 more threads
Word2Vec(vocab=29982, size=200, alpha=0.025)
【  】 【   】     : 0.387535493256
-----
2016-12-12 15:38:35,293: INFO: worker thread finished; awaiting finish of 0 more threads
2016-12-12 15:38:35,293: INFO: training on 17805780 raw words (14021191 effective words) took 48.5s, 289037 effective words/s
2016-12-12 15:38:35,293: INFO: precomputing L2-norms of word weight vectors
 【  】      :
   0.6038454175
   0.585186183453
   0.530897378922
   0.516572892666
     0.508533298969
   0.507428050041
   0.494115233421
    0.471616715193
   0.465247869492
   0.457907706499
   0.457776963711
   0.455987215042
    0.450040221214
   0.44820779562
    0.436062157154
    0.432559013367
    0.430508673191
   0.430286765099
   0.429748386145
   0.429243773222
-----
 -  ,  -
   0.613928854465
   0.595371186733
   0.592055797577
----
     :  
-----
2016-12-12 15:38:35,515: INFO: saving Word2Vec object under   .model, separately None
2016-12-12 15:38:35,515: INFO: not storing attribute syn0norm
2016-12-12 15:38:35,515: INFO: not storing attribute cum_table
2016-12-12 15:38:36,490: INFO: saved   .model
Process finished with exit code 0




이상 의 python 에서 word2vec 작업 을 초보 적 으로 실현 하 는 것 은 바로 편집장 이 여러분 에 게 공유 한 모든 내용 입 니 다.여러분 에 게 참고 가 되 고 여러분 들 이 저 희 를 많이 지지 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기