2016-05-15 11 views
0

私はTensorflowチュートリアルを試して、RNN/language model tutorialを強化して、文の次の単語を予測できるようにしようとしています。このチュートリアルでは、単語の埋め込みを単語の表現として使用します。Tensorflowの予測に単語埋め込みを使用する方法

モデルは単語の埋め込みについて学習するので、私が追加した予測のどんな種類も同じ埋め込みを出力すると仮定しています。私が理解できないことは、それらの埋め込みからデータセットの単語IDに変換する方法です。私が見てきた唯一の例は、wordid - >埋め込みのマッピングの逆を使ってメモリ内のデータ構造を保持し、それをルックアップに使用したものです。これは明らかにすべての問題では機能しません。より良い方法がありますか?

答えて

0

あなたは語彙からword_to_idxidx_to_wordの両方を持っていると仮定すると、これはあなたが予測のための入力は、「これはサンプルです」である

batch_size = 1 
num_steps = 3 # i.e each step for the word in "this is sample" 
hidden_size = 1500 
vocab_size = 10000 
translate the `word_to_idx` for the input `"this is sample"` 
get the word embeddings for the each word in the input 
Input to model will be word embedding of size 1x1500 at each time step 
Output of model at each time step will be of size 1x1500 
y_pred is output at last step from model for the given input 
adding projection to output (i.e y_pred x Weights(hidden_size, vocab_size) + bias(vocab_size,) = 1x10000) 
now sample the output through the below function to get the index with max probability 
generate idx_to_word from the index we just got 
use the generated word along with the previous input to generate next word until you get `<eos>` or some predefined sentence stopping. 

想像してみてください何のための擬似コードは、ここでは、サンプリングの例ですされますhere

def sample(a, temperature=1.0): 
    # helper function to sample an index from a probability array 
    a = np.log(a)/temperature 
    a = np.exp(a)/np.sum(np.exp(a)) 
    return np.argmax(np.random.multinomial(1, a, 1)) 
+0

ありがとうございました!私は理解していると思うが、それを試してみるには1〜2日かかるだろう。私はそれを試してみると答えとしてマークします。 – mmarklar

関連する問題