首页 文章

使用Keras的不 balancer 数据集

提问于
浏览
2

我正在用python和Keras库构建一个分类ANN . 我正在使用3个不同类别的不 balancer 数据集训练NN . 1级大约是第2类和第3类的7.5倍 . 作为补救措施,我接受了this stackoverflow answer的建议,并设置了我的 class 权重:

class_weight = {0 : 1,
                1 : 6.5,
                2: 7.5}

However, here is the problem: ANN正在以相同的速率预测3个 class !

这没有用,因为数据集是不 balancer 的,并且预测结果,因为每个具有33%的可能性是不准确的 .

Here is the question: 如何处理不 balancer 的数据集,以便人工神经网络不会每次都预测1级,而且人工神经网络不会以相同的概率预测类别?

这是我正在使用的代码:

class_weight = {0 : 1,
1 : 6.5,
2: 7.5}

# Making the ANN
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout

classifier = Sequential()


# Adding the input layer and the first hidden layer with dropout
classifier.add(Dense(activation = 'relu',
                     input_dim = 5,
                     units = 3,
                     kernel_initializer = 'uniform'))
#Randomly drops 0.1, 10% of the neurons in the layer.
classifier.add(Dropout(rate= 0.1))

#Adding the second hidden layer
classifier.add(Dense(activation = 'relu',
                     units = 3,
                     kernel_initializer = 'uniform'))
#Randomly drops 0.1, 10% of the neurons in the layer.
classifier.add(Dropout(rate = 0.1)) 

# Adding the output layer
classifier.add(Dense(activation = 'sigmoid',
                     units = 2,
                     kernel_initializer = 'uniform'))

# Compiling the ANN
classifier.compile(optimizer = 'adam',
                   loss = 'binary_crossentropy',
                   metrics = ['accuracy'])

# Fitting the ANN to the training set
classifier.fit(X_train, y_train, batch_size = 100, epochs = 100, class_weight = class_weight)

1 回答

  • 4

    我在模型中看到的最明显的问题是它没有适当的分类结构 . 如果您的样本一次只能属于一个类,那么您不应该通过将sigmoid激活作为最后一层来忽略这一事实 .

    理想情况下,分类器的最后一层应输出属于某个类的样本的概率,即(在您的情况下)数组 [a, b, c] ,其中 a + b + c == 1. .

    如果你使用sigmoid输出,那么输出 [1, 1, 1] 是一个可能的输出,虽然它不是你想要的 . 这也是为什么你的模型没有正确推广的原因:鉴于你并没有专门训练它而更喜欢"unbalanced"输出(如 [1, 0, 0] ),它将默认预测它在训练期间看到的平均值,考虑到重新加权 .

    尝试将上一个图层的激活更改为 'softmax' ,将损失更改为 'catergorical_crossentropy'

    # Adding the output layer
    classifier.add(Dense(activation='softmax',
                         units=2,
                         kernel_initializer='uniform'))
    
    # Compiling the ANN
    classifier.compile(optimizer='adam',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])
    

    如果这不是很确定这是主要问题 .
    干杯

相关问题