2017-09-13 10 views
1

習慣的な損失関数を作成することで、偽陽性の予測のためにモデルの損失を増やしたい。
model.fit()のclass_weightパラメータは、この問題では機能しません。真のラベルと同じくらい多くの真でないラベルの23倍の場所にトレーニングデータを歪めているので、class_weightはすでに{0:1、1:23}に設定されています。ケラスの違いによって重みが異なる

ケラスのバックエンドで作業しているときに私はあまり経験していません。私は主に機能モデルで作業しました。

私が作成することです:

def weighted_binary_crossentropy(y_true, y_pred): 
    #where y_true == 0 and y_pred == 1: 
    # weight this loss and make it 50 times larger 
    #return loss 

私は、このような平均二乗誤差が、私はどのように論理的なものを行うには考えている取得などのテンソルでシンプルなものを行うことができます。
私は仕事をdoesntのいくつかのハックソリューションを実行しようとしましたし、完全に間違って感じています

def weighted_binary_crossentropy(y_true, y_pred): 
    false_positive_weight = 50   
    thresh = 0.5 
    y_pred_true = K.greater_equal(thresh,y_pred) 
    y_not_true = K.less_equal(thresh,y_true) 
    false_positive_tensor = K.equal(y_pred_true,y_not_true) 

    loss_weights = K.ones_like(y_pred) + false_positive_weight*false_positive_tensor 

    return K.binary_crossentropy(y_true, y_pred)*loss_weights 

私はバックエンドとしてkeras 2とtensorflowでのpython 3を使用しています。

ありがとうございます!ここで、この重量はない損失に、予測値に適用されるので、あなたが損失でそれをしたい場合は訂正があるかもしれないことを、私はあなたがほとんどそこだと思う

答えて

0

...

from keras.losses import binary_crossentropy 

def weighted_binary_crossentropy(y_true, y_pred): 
    false_positive_weight = 50   
    thresh = 0.5 
    y_pred_true = K.greater_equal(thresh,y_pred) 
    y_not_true = K.less_equal(thresh,y_true) 
    false_positive_tensor = K.equal(y_pred_true,y_not_true) 


    #changing from here 

    #first let's transform the bool tensor in numbers - maybe you need float64 depending on your configuration 
    false_positive_tensor = K.cast(false_positive_tensor,'float32') 

    #and let's create it's complement (the non false positives) 
    complement = 1 - false_positive_tensor 

    #now we're going to separate two groups 
    falsePosGroupTrue = y_true * false_positive_tensor 
    falsePosGroupPred = y_pred * false_positive_tensor 

    nonFalseGroupTrue = y_true * complement 
    nonFalseGroupPred = y_pred * complement 


    #let's calculate one crossentropy loss for each group 
    #(directly from the keras loss functions imported above) 
    falsePosLoss = binary_crossentropy(falsePosGroupTrue,falsePosGroupPred) 
    nonFalseLoss = binary_crossentropy(nonFalseGroupTrue,nonFalseGroupPred) 

    #return them weighted: 
    return (false_positive_weight*falsePosLoss) + nonFalseLoss 
+0

お知らせ値。 –

+0

ねえ!迅速な答えをありがとう。予測に重みを適用する問題は、K.binary_crossentropyがy_predの値をEPSILON、1 - EPSILONにクリップし、そこで行われた調整を取り除くことです。 K.binary_crossentropyが返す損失値に偽陽性に相当する損失値を一定の重みで乗算するのがいいでしょう。どのようにそれを行うことができる任意のアイデア? – user3537014

+0

'weighted_falses =(false_positive_weight-1)* false_positive_tensor'この行はmodel.compile()でコンパイルできません。それは、ブールが期待されているが、intを持っていると言う – user3537014

関連する問題