我正在用python和Keras库构建一个分类ANN。我使用的训练神经网络的不平衡数据集与3个不同的类。第一班的流行率是第二班和第三班的7.5倍。作为补救,我采纳了这个堆叠溢出的答案的建议,并将我的班级权重设置如下:
class_weight = {0 : 1,
1 : 6.5,
2: 7.5}然而,的问题是:人工神经网络以相同的速度预测这三个类!
这是没有用的,因为数据集是不平衡的,预测结果,因为每个有33%的机会是不准确的。
这里的问题是:如何处理不平衡的数据集,这样人工神经网络就不会每次预测第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)发布于 2018-02-01 11:54:12
我在你的模型中看到的最明显的问题是它没有正确的分类结构。如果您的样本一次只能属于一个类,那么您不应该忽略这一事实,将sigmoid激活作为您的最后一层。
理想情况下,分类器的最后一层应该输出属于类的样本的概率,即(在您的例子中)数组[a, b, c],其中a + b + c == 1.。
如果您使用乙状结肠输出,那么输出[1, 1, 1]是可能的,尽管它不是您所追求的。这也是为什么您的模型没有正确泛化的原因:考虑到您没有专门地将它训练成更喜欢“不平衡”的输出(如[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'])如果这不起作用,请看我的其他评论,然后用这个信息回复我,但我很有信心这是主要的问题。
干杯
https://stackoverflow.com/questions/48547931
复制相似问题