【问题标题】:Keras prediction returns the same results each timeKeras 预测每次都返回相同的结果
【发布时间】:2018-12-21 10:07:35
【问题描述】:

我是 Keras 的新手。刚从本地开始,取自here 的示例。示例数据工作正常。然后我稍微修改了代码以适应我的数据(在我的数据文件结果列中首先出现)。然后当我再次运行并尝试预测输入时,它总是为每个输入行返回相同的结果 - [1. 0.], [1. 0.] ...。这是我的代码:

import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.utils import to_categorical

#read in training data
train_df_2 = pd.read_csv('/Users/my_user/python-workspace/Deep-Learning-in-Keras-Tutorial/data/my_data.csv')

#view data structure
train_df_2.head()

#create a dataframe with all training data except the target column
train_X_2 = train_df_2.drop(columns=['result'])

target = train_df_2[['result']]

#check that the target variable has been removed
train_X_2.head()

#one-hot encode target column
train_y_2 = to_categorical(train_df_2.result)

#create model
model_2 = Sequential()

#get number of columns in training data
n_cols_2 = train_X_2.shape[1]

#add layers to model
model_2.add(Dense(25, activation='relu', input_shape=(n_cols_2,)))
model_2.add(Dense(25, activation='relu'))
model_2.add(Dense(2, activation='softmax'))
# model_2.add(Dense(10, input_dim=n_cols_2, kernel_initializer='normal', activation='relu'))
# model_2.add(Dense(25, activation='relu'))
# model_2.add(Dense(2, activation='softmax'))

#compile model using accuracy to measure model performance
model_2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#set early stopping monitor so the model stops training when it won't improve anymore
early_stopping_monitor = EarlyStopping(patience=3)

#train model
model_2.fit(train_X_2, train_y_2, epochs=30, validation_split=0.1, callbacks=[early_stopping_monitor])

p = model_2.predict(train_X_2, verbose=0, batch_size=1)
print(p)

我的输入数据示例:

result,i1,i2,i3,i4
0,1770,2390,1750,1816
1,1675,2540,2029,1940
1,1770,2384,1765,1770
0,1690,2485,2075,1900
0,1680,2465,2050,1920
0,1770,2395,1744,1795
1,1675,2490,2050,1915
0,1768,2400,1740,1790
0,1675,2525,2050,1910 
.... (total 2312 rows)

为什么它总是为每一行返回相同的结果[1. 0.]?我预计至少有一排[0. 1.]。我做错了什么?

【问题讨论】:

  • 我认为该模型过度拟合,因为您的 0 类的数据比 1 类的数据多?你能告诉我们每个类的数据数量吗?
  • 考虑到您只有 4 列,我认为您使用的模型过于强大来预测结果。您应该尝试拟合一个简单的逻辑回归。当您的数据具有高度复杂性时,NN 非常有用。
  • @Vaibhavgusain 0/1 接近 50/50
  • 如果result只有2个值:0和1,你可以使用二进制输出格式。

标签: python tensorflow machine-learning keras neural-network


【解决方案1】:

您尚未对输入数据进行标准化。因此,它会阻碍训练过程并破坏梯度更新,并且您的模型可能一无所获。尝试使用类似sklearn.preprocessing.StandardScaler 的方式对其进行标准化。或者您也可以手动操作:

mean = train_X_2.mean(axis=0)
train_X_2 -= mean
std = train_X_2.std(axis=0)
train_X_2 /= std

【讨论】:

  • 谢谢!现在结果发生了一点变化。现在它看起来像 [[0.4751541 0.5248459 ] [0.44454956 0.55545044] [0.47325155 0.5267484 ]] - 第一列总是少于第二列。那么,我需要玩数据缩放吗?它应该总是
  • @CatH 是的,这些值通常被标准化为一个小范围,例如 [0,1] 或 [-1, 1]。模型在训练阶段的准确率是多少?
  • 其实我也不知道……怎么设置准确率?
  • @CatH 你设置了metrics=['accuracy'],所以必须在训练进度条中显示准确度?!它写成acc: xxxx
  • 是的,我在输出中有这个,比如 acc: 0.5248。系数好不好?
【解决方案2】:

我在 iris 数据集(前 100 行只有 2 种类型的目标)上使用了具有二进制结果的模型:

def getmodel(n_cols_2): 
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.utils import to_categorical
    #create model
    model_2 = Sequential()
    #add layers to model
    model_2.add(Dense(25, activation='relu', input_shape=(n_cols_2,)))
    model_2.add(Dense(25, activation='relu'))
    model_2.add(Dense(1, activation='sigmoid')) 
    #compile model using accuracy to measure model performance
    model_2.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    return model_2

from sklearn import datasets
iris = datasets.load_iris()
train_X_2 = iris.data[:100]
train_y_2 = iris.target[:100]
# print top 5 rows:
print(train_X_2[:5])
print(train_y_2[:5])
n_cols = iris.data.shape[1]
# get model: 
model = getmodel(n_cols)
# set early stopping monitor so the model stops training when it won't improve anymore
from keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(patience=3)
#train model
model.fit(train_X_2, train_y_2, epochs=30, batch_size=10 , validation_split=0.2) # , callbacks=[early_stopping_monitor])
# predict and print classes
p = model.predict_classes(train_X_2, verbose=0, batch_size=10)
print(p.ravel())

输出完美:

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]]
[0 0 0 0 0]
Using TensorFlow backend.
Train on 80 samples, validate on 20 samples
Epoch 1/100
80/80 [==============================] - 0s 3ms/step - loss: 0.7615 - acc: 0.3750 - val_loss: 0.4282 - val_acc: 1.0000
Epoch 2/100
80/80 [==============================] - 0s 88us/step - loss: 0.6658 - acc: 0.3750 - val_loss: 0.4944 - val_acc: 1.0000
Epoch 3/100
...
...
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

请注意,此处未使用标准化。缺乏标准化可能会影响预测,但不会像您的情况那样使神经网络无法正常工作。

还要注意我使用了sigmoidbinary_crossentropypredict_classes

通常使用圆锥形,因此您的第二个 Dense 层可能只有 12 个左右的神经元。此外,可以通过在每个 Dense 层之后添加 Dropout 层来提高准确性。

如果你仍然得到全 0 或 1,可能是你的数据非常随机,并没有真正预测目标。

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 2012-05-28
    • 1970-01-01
    • 2019-01-06
    • 2019-01-02
    • 2020-05-19
    • 2020-01-27
    • 2021-06-29
    • 1970-01-01
    相关资源
    最近更新 更多