【问题标题】:keras: 98% of accuracy but the NN always predicts the same. What could be the cause?keras:98% 的准确率,但 NN 总是预测相同。可能是什么原因?
【发布时间】:2020-06-06 14:55:44
【问题描述】:

我们在训练 DL 模型以预测贷款评分(分类为 0、1 或 3)时遇到以下问题。

这些是步骤:

第 1 步:创建新列“评分”(输出)

conditions = [
(df2['Credit Score'] >= 0) & (df2['Credit Score'] < 1000),
(df2['Credit Score'] >= 1000) & (df2['Credit Score'] < 6000),
(df2['Credit Score'] >= 6000) & (df2['Credit Score'] <= 7000)]
choices = [0,1,2]
df2['Scoring'] = np.select(conditions, choices) 

第 2 步:准备训练

array = df2.values
X = np.vstack((array[:,2:3].T, array[:,5:15].T)).T
Y = array[:,15:]
N = Y.shape[0]
T = np.zeros((N, np.max(Y)+1))
for i in range(N):
  T[i,Y[i]] = 1

x_train, x_test, y_train, y_test = train_test_split(X, T, test_size=0.2, random_state=42)

第 3 步:拓扑

model = Sequential()

model.add(Dense(80, input_shape=(11,), activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(80, activation='tanh'))
model.add(Dropout(0.1))
model.add(Dense(40, activation='relu'))
model.add(Dense(3, activation='softmax'))

epochs =200
learning_rate = 0.00001
decay_rate = learning_rate / epochs
momentum = 0.002
sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False)
ad = Adamax(lr=learning_rate)

第 4 步:训练

 epochs = 200 
 batch_size = 16 

 history = model.fit(x_train, y_train, validation_data=(x_test, y_test), nb_epoch=epochs, 
 batch_size=batch_size,validation_split=0.1) 
 print ('fit done!')

指标

365/365 [===============================] - 0s 60us/样本 - 损耗:0.0963 - acc: 0.9808 测试集 损失:0.096 准确度:0.981

accuracy

第五步:预测

text1 = [1358,1555,1,3,1741,8,0,1596,1518,0,0] #scoring 0 
text2 = [1454,1601,3,11,1763,10,0,685,1044,0,0] #scoring 1 
text3 = [1209,1437,3,11,199,18,1,761,1333,1,0] #scoring 2

tmp = np.vstack(text1).T
textA = tmp.reshape(1,-1)

tmp = np.vstack(text2).T
textB = tmp.reshape(1,-1)

tmp = np.vstack(text3).T
print(tmp)
textC = tmp.reshape(1,-1)

p = model.predict(textA)
t = p[0]
print(textA,np.argmax(t))


p = model.predict(textB)
t = p[0]
print(textB,np.argmax(t))

p = model.predict(textC)
t = p[0]
print(textC,np.argmax(t))

问题:预测中的输出总是一样的!!!

[9.9205679e-01 3.8634153e-04 7.5568780e-03] [[1358 1555 1 3 1741 8 0 1596 1518 0 0]] 0---得分0

[0.9862417 0.00205712 0.01170125] [[1454 1601 3 11 1763 10 0 685 1044 0 0]] 0 --- 得分 0

[9.9251783e-01 2.5733517e-04 7.2247880e-03] [[1209 1437 3 11 199 18 1 761 1333 1 0]] 0----得分0

¿这种行为的原因是什么?

提前致谢!

【问题讨论】:

    标签: python machine-learning keras deep-learning tensor


    【解决方案1】:

    您有一个非常不平衡的数据集。一个很好的看待它的方法是:如果总是预测 0 可以让你达到 98% 的准确率,那么说某物属于不同的类别是非常冒险的(或者必须非常明显)。 NN 可能发现的每个使少数类与多数类 (0) 不同的模式都必须非常独特,因为即使重叠很小,不预测 0 的成本也太高。

    考虑这个例子:你有一个数据集有两个类,A 和 B,它们都服从正态分布。 A 类具有均值 1 和标准 1,B 类具有均值 3 和标准 0.1。您有 0 类的 1,000,000 个样本和 1 类的 20,000 个样本,因此始终预测 A 可以为您提供 98% 的准确率。 B 类的所有样本都将位于 2.743 和 3.257 之间,置信度为 99%。在这些值之间,A 类预计有 29,300 个样本,因此将任何观测值预测为 B 类的成本在 29,300 个 A 样本中会出错,但将所有内容预测为 A 的成本只会在 20,000 个 B 样本中出错.

    这是该示例的图形外观:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Get A and B
    A = np.random.normal(1, 1, 1000000)
    B = np.random.normal(3, 0.1, 20000)
    
    # Count the number of observations in A for each B
    B.sort()
    a = A[np.logical_and(A >= B.min(), A <= B.max())]
    a = [(a<i).sum() for i in B]
    
    # Plot results
    plt.plot(B, np.arange(B.shape[0]), label='Class B')
    plt.plot(B, a, label='Class A')
    plt.ylabel('Count of samples')
    plt.xlabel('Values')
    plt.legend()
    plt.show()
    

    请参阅这篇关于平衡数据集的文章:https://www.kdnuggets.com/2017/06/7-techniques-handle-imbalanced-data.html

    【讨论】:

    • 感谢您的回答!你说的对。使用 ML 用相同的数据集解决它的准确性和预测,所以我不认为这可能是一个问题。是对的,但 DL 需要一个同质化的数据状态
    猜你喜欢
    • 2018-12-30
    • 2021-01-05
    • 2017-10-28
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多