我的数据存在极端的类(Class)失衡。大约99.99%的样本为阴性。正数(大致)在其他三个类别之间平均分配。我认为我正在训练的模型基本上总是在预测多数学生。因此,我正在尝试对类(class)进行加权。

模型

model = Sequential()

#Layer 1
model.add(Conv1D( {{choice([32, 64, 90, 128])}}, {{choice([3, 4, 5, 6, 8])}}, activation='relu', kernel_initializer=kernel_initializer, input_shape=X_train.shape[1:]))
model.add(BatchNormalization())

#Layer 2
model.add(Conv1D({{choice([32, 64, 90, 128])}}, {{choice([3, 4, 5, 6])}}, activation='relu',kernel_initializer=kernel_initializer))
model.add(Dropout({{uniform(0, 0.9)}}))

#Flatten
model.add(Flatten())

#Output
model.add(Dense(4, activation='softmax'))

({{...}}Hyperas一起使用。)

我如何尝试对其加重

\1。在class_weight中使用model.fit()
model.fit(X_train, Y_train, batch_size=64, epochs=10, verbose=2, validation_data=(X_test, Y_test), class_weight={0: 9999, 1:9999, 2: 9999, 3:1})

\2。在class_weight中将model.fit()sklearn一起使用compute_class_weight()
model.fit(..., class_weight=class_weight.compute_class_weight("balanced", np.unique(Y_train), Y_train)

\3。具有自定义丢失功能
from keras import backend as K
def custom_loss(weights):
    #gist.github.com/wassname/ce364fddfc8a025bfab4348cf5de852d

    def loss(Y_true, Y_pred):
        Y_pred /= K.sum(Y_pred, axis=-1, keepdims=True)
        Y_pred = K.clip(Y_pred, K.epsilon(), 1 - K.epsilon())

        loss = Y_true * K.log(Y_pred) * weights
        loss = -K.sum(loss, -1)
        return loss

    return loss

extreme_weights = np.array([9999, 9999, 9999, 1])
model.compile(loss=custom_loss(extreme_weights),
            metrics=['accuracy'],
            optimizer={{choice(['rmsprop', 'adam', 'sgd','Adagrad','Adadelta'])}}
            )

#(then fit *without* class_weight)

结果

较差的。所有类的精度为〜.99,所有类的不平衡精度为〜.5。但是,更有意义的指标(如auPRC)则讲述了一个不同的故事。 auPRC对于大多数类别几乎是1,对于其余类别几乎是0

这是Keras如何平衡类(class)的吗?它只是确保它们之间的准确性是相同的,或者指标是否应该相等或可比?还是我指定的权重错误?

最佳答案

Keras在训练过程中使用类(class)权重,但准确性并不能反射(reflect)这一点。与所有类别之间的权重无关,所有样本的准确性均得到计算。这是因为您在compile()中使用度量“准确性”。您可以定义一个自定义且更准确的加权精度,然后使用它或使用sklearn指标(例如f1_score()可以是“二进制”,“加权”等)。

例子:

def macro_f1(y_true, y_pred):
     return f1_score(y_true, y_pred, average='macro')


model.compile(loss=custom_loss(extreme_weights),
        metrics=['accuracy', macro_f1],
        optimizer={{choice(['rmsprop', 'adam', 'sgd','Adagrad','Adadelta'])}}
        )

关于python - Keras:class_weight实际上试图平衡什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51432992/

10-12 22:23