2016-12-13 10 views
1

私はこのような輸入tensorflow語彙、で働いていた:私はまだのトラックを維持しながら、私は、語彙を保存し、それをリロードし、新しい文を合うことができることを確認しましたユニットテストを書いたtensorflow、contrib.learnを使って既存の語彙に単語を追加するには?

from tensorflow.contrib import learn 
vocabulary = learn.preprocessing.VocabularyProcessor(length) 

古いもの。

これは私の結果であった:

The fit sentence: [1 2 3 4 5 6 2 7 8 4 5 9 7] 
The new fit sentence: [0 0 0 2 9 0 6 2 7 8 4 0 0] 

それは最初の文(2として処理)位置0のワードが第二位置3における単語のように同じ値(2)を有し、正しく働い彼らは同じなので、文章。

しかし、私は気づいた、すべての新しい単語が0

だった私は、このように見えるように私の新しいフィットの文章を期待しているでしょう:

[10 11 12 2 9 10 6 2 7 8 4 12 11] 

は、どのように私はこの問題を解決することができますか?語彙プロセッサに新しい単語を学習させるにはどうすればよいですか?

ありがとうございました!

EDIT 1:

これは私のユニットテストのストリップダウンバージョンです:

import numpy as np 
from tensorflow.contrib import learn 

# A test sentence 
test_sentence = "This is a test sentence. It is used to test. sentence, this, used" 
test_sentence_len = len(test_sentence.split(" ")) 

# A vocabulary processor 
vocabulary_processor = learn.preprocessing.VocabularyProcessor(test_sentence_len) 

# Turning a list of sentences ([test_sentence]) into a list of fit test sentences and taking the first one. 
fit_test_sentence = np.array(list(vocabulary_processor.fit_transform([test_sentence])))[0] 

# We see that "is" (position 1) and "is" (position 6) are the same. They should have the same numeric value 
# in the fit array as well 
print("The fit sentence: ", fit_test_sentence) 
# self.assertEqual(fit_test_sentence[1], fit_test_sentence[6]) 

initial_fit_sentence = fit_test_sentence 

# Now, let's save 

vocabulary_processor.save("some/path") 

# Now, we load into a different variable 

new_vocabulary_processor = learn.preprocessing.VocabularyProcessor.restore("some/path") 

new_test_sentence = "Very different uttering is this one. It is used to test." 

# Now, we fit the new sentence with the new vocabulary, which should be the old one 
# We should see "is" being transformed into the same numerical value, initial_fit_sentence[1] 

new_fit_sentence = np.array(list(new_vocabulary_processor.fit_transform([new_test_sentence])))[0] 

print("The new fit sentence: ", new_fit_sentence) 
# self.assertEqual(initial_fit_sentence[1], new_fit_sentence[3]) 

私はちょうどこれ以上新しい学ぶことができなかったかもしれない語彙を考えtest_sentence_lenの値を変更してみましたたとえそれを1000に設定しても、新しい単語を覚えることはありません。

+0

何をしようとしていますか?私は最終的なアプリケーションを意味します。 – martianwars

+0

@martianwarsテキスト分類。そのためには、私の言葉の数値表現が必要です。私は語彙を学ぶことができるようにしたい、それを保存し、読み込むときに新しい単語を学びたい。私はそれを保存して読み込むことができますが、新しい単語を0でマークします。これは私が修正しようとしている問題です。 – mayk93

+0

新しい文章でfit()やtransform()を呼び出していますか?あなたが行っている一連の呼び出しを表示できますか? – Russell

答えて

0

fit_transformのように、ボキャブラリがフリーズするようです。つまり、その時点までに観測されていないものは、0 ID(UNK)になります。あなたはnew_vocabulary_processor.vocabulary_.freeze(False)で語彙を解凍することができます。

new_vocabulary_processor = learn.preprocessing.VocabularyProcessor.restore("some/path") 
new_vocabulary_processor.vocabulary_.freeze(False) 
new_test_sentence = "Very different uttering is this one. It is used to test." 
+0

答えをありがとう!やってみます! – mayk93