2017-05-29 17 views
2

私は蒸留の概念を適用しようとしていますが、基本的に新しい小さなネットワークを訓練して元のものと同じようにするが、計算は少なくて済みます。categorical_crossentropyケラスの実装

私はロジットの代わりにすべてのサンプルに対してsoftmax出力を持っています。

私の質問は、カテゴリクロスエントロピー損失関数はどのように実装されていますか?これと同様 元のラベルの最大値をとり、同じインデックスに対応付け予測値とを乗算し、またはそれは、すべての式が言うようlogits(ワンホットエンコーディング)にわたって加算を行います。 enter image description here

おかげ!!

答えて

2

テンソルフロータグを使用していることがわかります。これは、使用しているバックエンドだと思いますか?

def categorical_crossentropy(output, target, from_logits=False): 
"""Categorical crossentropy between an output tensor and a target tensor. 
# Arguments 
    output: A tensor resulting from a softmax 
     (unless `from_logits` is True, in which 
     case `output` is expected to be the logits). 
    target: A tensor of the same shape as `output`. 
    from_logits: Boolean, whether `output` is the 
     result of a softmax, or is a tensor of logits. 
# Returns 
    Output tensor. 

このコードはkeras source codeから来ています。コードを直接見るとすべての質問に答える必要があります。

編集:ここでは

が興味のあるコードです:

# Note: tf.nn.softmax_cross_entropy_with_logits 
# expects logits, Keras expects probabilities. 
if not from_logits: 
    # scale preds so that the class probas of each sample sum to 1 
    output /= tf.reduce_sum(output, 
          reduction_indices=len(output.get_shape()) - 1, 
          keep_dims=True) 
    # manual computation of crossentropy 
    epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype) 
    output = tf.clip_by_value(output, epsilon, 1. - epsilon) 
    return - tf.reduce_sum(target * tf.log(output), 
          reduction_indices=len(output.get_shape()) - 1) 

あなたがリターンを見れば、彼らはそれを合計... :)

+0

はい、私は場所を知りますそれはから来るが、私はそれが合計か最大を見ているかどうか見ることができない。それは、元のラベルに対してワンホットエンコーディングの代わりにsoftmax出力を使用しているためです。そのため、通常のワンホットエンコーディングではなく、確率を持っています。ありがとう!! – Eric

+0

詳細編集詳細 –

+0

ニース!!私はちょうど確信したいと思った:D Thank you yoooou !!!! – Eric

関連する問題