2016-11-26 5 views
1

모국어를 위해 사전에 훈련 된 word2vec 모델을 다운로드했습니다. 은 그것은 "news.model.bin"파일을했고, 내가 그것을 압축을 풀 때, txt 파일이나 pickle를 볼 것으로 예상,하지만 난 이런 혼란 consistat으로 다른 .bin 파일을 발견Theano 또는 TensorFlow에 ".bin"로드하기

\09\b9\.,-;sdfkf %some really strange symbols and seem to be invalid symbols% 

I 파일을 정상적으로 열 수 없기 때문에 복사 할 수 없습니다. 무거워서 노트북이 죽을뿐입니다. 질문 :이 예시 코드는 사전 전차 모델 일 수 있습니까? 그렇다면 어떻게 처리해야합니까?

P. 난에서 모델 얻었다 링크 (모델 페이지의 하단에 있습니다) : http://ling.go.mail.ru/dsm/ru/about

+0

빠른 구글 온 [이 (http://mccormickml.com/2016/04/12/googles-pretrained-word2vec- 모델 -에서 - 파이썬 /). Word2vec의 특수 형식이라고 생각합니다. 희망이 도움이됩니다. – Kh40tiK

+0

[word2vec bin 파일을 텍스트로 변환] 가능한 복제본 (http://stackoverflow.com/questions/27324292/convert-word2vec-bin-file-to-text) –

답변

0

두 솔루션 :

  1. 이 .txt로하는 .BIN 변환 : Convert word2vec bin file to text
  2. 직접 읽어. 아래 그림과 같이 bin을 선택하십시오.

    # Initialize all variables 
        sess.run(tf.initialize_all_variables()) 
        # Initialize all variables 
        sess.run(tf.initialize_all_variables()) 
        if FLAGS.word2vec: 
         # initial matrix with random uniform 
         initW = np.random.uniform(-0.25,0.25,(len(vocab_processor.vocabulary_), FLAGS.embedding_dim)) 
         # load any vectors from the word2vec 
         print("Load word2vec file {}\n".format(FLAGS.word2vec)) 
         with open(FLAGS.word2vec, "rb") as f: 
          header = f.readline() 
          vocab_size, layer1_size = map(int, header.split()) 
          binary_len = np.dtype('float32').itemsize * layer1_size 
          for line in xrange(vocab_size): 
           word = [] 
           while True: 
            ch = f.read(1) 
            if ch == ' ': 
             word = ''.join(word) 
             break 
            if ch != '\n': 
             word.append(ch) 
           idx = vocab_processor.vocabulary_.get(word) 
           if idx != 0: 
            initW[idx] = np.fromstring(f.read(binary_len), dtype='float32') 
           else: 
            f.read(binary_len)  
    
        sess.run(cnn.W.assign(initW)) 
    

    당신이 text classification example in TensorFlow이 코드를 사용할 수 있습니다

https://gist.github.com/j314erre/b7c97580a660ead82022625ff7a644d8는 .BIN을 읽고 TensorFlow 변수에로드하는 몇 가지 코드가 포함되어 있습니다. FYI

는 :