Keras custom metric iteration

前端 未结 3 2030
一整个雨季
一整个雨季 2021-01-18 11:16

I\'m pretty new to Keras and I\'m trying to define my own metric. It calculates concordance index which is a measure for regression problems.

def cindex_scor         


        
3条回答
  •  再見小時候
    2021-01-18 11:41

    I used @Pedia code for 3D-tensors to compute Rank loss for multi label classification:

    def rloss(y_true, y_pred):
        g = tf.subtract(tf.expand_dims(y_pred[1], -1), y_pred[1])
        g = tf.cast(g == 0.0, tf.float32) * 0.5 + tf.cast(g > 0.0, tf.float32)
        f = tf.subtract(tf.expand_dims(y_true[1], -1), y_true[1]) > 0.0
        f = tf.matrix_band_part(tf.cast(f, tf.float32), -1, 0)
        g = tf.reduce_sum(tf.multiply(g, f))
        f = tf.reduce_sum(f)
    return tf.where(tf.equal(g, 0), 0.0, g/f)
    
    
    model = Sequential()
    model.add(Dense(label_length, activation='relu'))
    model.add(Dense(label_length, activation='relu'))
    model.add(Dense(label_length, activation='sigmoid'))
    model.summary()
    
    
    adgard = optimizers.Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)
    model.compile(loss='binary_crossentropy',
              optimizer=adgard, metrics=[rloss])
    model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=n_epoch,
          validation_data=(X_test, y_test),
          shuffle=True)
    

提交回复
热议问题