2017-07-20 15 views
0

複数のラベル分類モデルを作成したいと思います(各サンプルに複数のラベルがあり、各サンプルのラベル数は固定されていません) 。例えば、example1はクラスラベル "X"、 "Y"を持ち、example2はクラスラベル "X"、 "Y"、 "Z"を持つことができます。私の目標は、このマルチラベル分類モデルのクロスエントロピー損失を計算することです。tf.nn.softmax_cross_entropy_with_logits()でクロスエントロピー損失の計算を高速化する方法

私の最初の解決策は、ターゲットクラスの高密度ワンホット表現を手動で作成し、損失を計算することです。しかし、私の語彙のサイズがO(10K)の場合、この解決策は遅いです。私はこれを行うより効率的な方法があるのだろうか?

[更新は、関連するコードを提供する]

## During the data input phrase 
def input_fn(): 
    ... 
    ## target_ids is a sparseTensor 
    target_ids = lookup_table.lookup(target_label_strings) 

    ## change the dense_shape 
    st2 = tf.SparseTensor(indices=target_ids.indices, 
          values=target_ids.values, 
          dense_shape=[batch_size,vocab_size]) 

    ## Convert to dense Tensor 
    st2_ordered = tf.sparse_reorder(st2) 
    dt = tf.sparse_tensor_to_dense(st2_ordered) 

    ## Row normalization 
    dt_float = tf.cast(dt, tf.float32) 
    dt_float = tf.add(dt_float, tf.constant(1e-10)) 

    dt_row_norm = tf.reduce_sum(dt_float, axis=1) 
    target["target_ids"] = dt_float/tf.reshape(dt_row_norm, (-1,1)) 

    return feature_map, target 

## Model training 
def get_loss_fn(self, target, weights, mode): 
    ... 
    ## the self.final_logit is the final output layer 
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
     labels=target["target_ids"], logits=self.final_logit)) 
    ... 

おかげ。

+0

を使用することです。 –

+0

元の質問にpesudocodeを追加します。ありがとう。 –

答えて

0

TensorFlowにソフトマックスクロスエントロピーを行う際に、大きな語彙に対処する最も簡単な方法は、関数の独自の実装を投稿することができればそれが役立つだろうtf.nn.sampled_softmax_loss

+0

私は参照してください。お返事ありがとうございます。 –