2017-01-28 19 views
2

私は先行単語の文脈でシーケンス内の次の単語を予測するtflearn内のlstmネットワークを持っています。単語は、特定のサイズの語彙のインデックスとしてネットワークに供給され、バイナリクラスで出力されます。コサインの類似性のためのカスタム損失関数

コンテキスト:[45,243,1,1906,4,2,0,0 、0]

ラベル:[0,0,0 ....... 1,0,0、...... 0,0,0](VOCAB_SIZEの長さ)

しかしモデルは回帰層で "categorical_crossentropy"目的関数を使用しているため、毎回ほとんど同じ単語を予測することを学びます。

データセットに対して既に生成した単語埋め込み(word2vec)に基づいて損失を評価してみたいと思います。したがって、地面の真実が「高」である「こんにちは」を予測するモデルは、「ピザ」を予測するよりはるかに低い損失を有するであろう。計画は、2つの埋め込みベクトルの間の余弦を計算して、単語間の「類似性」を得ることです。

私はこのカスタムロス関数をtflearnのインストールのtargets.pyファイルに追加しますが、予測を埋め込みベクトルに変換しようとすると、ロードブロッキングが発生しました。 objectives.py

tflearn /:

vocab = np.loadtxt('/home/vocab.txt',dtype='str') 
embedding_model = gensim.models.Word2Vec.load('/home/embedding') 

def embedded_similarity(y_pred, y_true): 
    global vocab, embedding_model 
    with tf.name_scope("Similarity"): 
     #convert one-hot format to indices of max values (predictions) 
     pred_max = tf.argmax(y_pred,dimension=1) 
     true_max = tf.argmax(y_true,dimension=1) 

     #convert indices into embedded vectors 
     pred_vectors = tf.map_fn(lambda x: embedding_model[vocab[x]], pred_max) 
     true_vectors = tf.map_fn(lambda x: embedding_model[vocab[x]], true_max) 

     #calc dot product 
     dot_products = tf.reduce_sum(tf.mul(pred_vectors,true_vectors),axis=1) 

     #return inverse mean of dot products 
     return -1*(tf.reduce_mean(dot_products)) 

エラーが返さは:

ValueError: Index out of range using input dim 0; input has only 0 dims for 'Similarity/map/while/strided_slice' (op: 'StridedSlice') with input shapes: [], [1], [1], [1]. 

これは私が単語(numpyの配列)へのインデックステンソルを使用できないことを示しています。しかし、これはセッションでは実行されないため、eval()を使ってテンソルの値を取得することはできません。だから私は、損失を計算するために、対応する単語ベクトルを含むテンソルにインデックスの1次元テンソルの変換を設定する方法が必要です。

私のモデルを評価するためのこの問題や別の方法の助けがあれば、大歓迎です。

+0

また、tf.map_fnの行でエラーが発生し、xがvocabにインデックスする場所を指します。 – etc

答えて

1

この問題を解決するためにtf.gatherを使用しました。さらに、私はアルゴリズムの分類の信頼度でスケーリングを追加し、勾配を計算できなかった箇所に遭遇したバグを修正しました。以下のコードです:objectives.pyのヘッダで

コード:

import numpy as np 
import gensim 
vocab = np.genfromtxt('/home/vocab.txt',dtype='str') 
embedding_model = gensim.models.Word2Vec.load('/home/embedding') 
i2v = [] 
for v in vocab: 
i2v.append(embedding_model[v]) 

embedded_similarity(y_pred、y_true):

global i2v 
with tf.name_scope("Similarity"): 

    i2v_tensors = [ tf.cast(tf.constant(iv), tf.float32) for iv in i2v ] 
    i2v_tensorarray = tf.pack(i2v_tensors) 

    #convert one-hot to indices 
    pred_max = tf.cast(tf.argmax(y_pred,dimension=1), tf.int32) 
    true_max = tf.cast(tf.argmax(y_true,dimension=1), tf.int32) 

    #extract probabilities for scaling later 
    pred_iter = tf.concat(tf.constant(1),[y_pred,tf.pack([tf.cast(pred_max,tf.float32)],axis=1)]) 
    confidence_scaler = 1/tf.map_fn(lambda x: tf.gather(x, tf.cast(tf.gather(x,tf.constant(5002)),tf.int32)), pred_iter, tf.float32) 

    #convert indices into vectors (of type tensor) 
    pred_vectors = tf.map_fn(lambda x: tf.gather(i2v_tensorarray, x), pred_max, tf.float32) 
    true_vectors = tf.map_fn(lambda x: tf.gather(i2v_tensorarray, x), true_max, tf.float32) 

    #calc dot product 
    dot_products = tf.reduce_sum(tf.mul(pred_vectors,true_vectors),axis=1) 

    #divide by magnitudes 
    pred_magnitudes = tf.sqrt(tf.reduce_sum(tf.mul(pred_vectors,pred_vectors),axis=1)) 
    true_magnitudes = tf.sqrt(tf.reduce_sum(tf.mul(true_vectors,true_vectors),axis=1)) 

    cosines = dot_products/tf.mul(pred_magnitudes,true_magnitudes) 
    loss = -1*tf.cast(cosines, tf.float32) + 2 #min loss is 1, max is 3 
    scaled_loss = tf.multiply(loss, confidence_scaler) 
    # loss = -1*cosines + 1 

    # return inverse sum of dot products 
    return tf.reduce_mean(scaled_loss) 

しかし、私は奇妙なバグに遭遇しています。その後、出力は単に全体のコンピュータがフリーズではなく

--------------------------------- 
Training samples: 800 
Validation samples: 200 

:私はモデルに合うようにしようとすると以下のように、それは、トレーニングおよびバリデーションサンプルの数を示していますまで、コードは完全に罰金実行します。私はコードをCtrl-Cできず、別の端末を起動しなければなりません。システムの大幅な減速はないようですが、トレーニングセットのサイズとバッチサイズの両方をやや低い数値に減らしてみましたが、結果はありませんでした。

私は主な問題に答えたのでこの問題を解決済みとマークしますが、誰かがこのタイプの動作に遭遇してからコメントしてください。ありがとう!

関連する問題