测试训练的keras模型并在新样本上使用时的预测差异

时间:2017-10-23 19:20:29

标签: deep-learning time-series keras scaletransform

我在使用已经训练,保存和加载使用的模型时遇到了麻烦。问题是,当我使用带有新数据的模型时,我会得到不同的预测(这不是测试数据的一部分,而是来自同一个来源)。

编辑1:我意识到我在预测整个test_data时以及我一次预测test_data的每个项目时会得到不同的预测。

这就是我生成训练和测试数据的方法(我将其拆分)

    df.dropna(inplace=True)
    for i in range(100):        
        df[i] = df.data.shift(i)
    df.dropna(inplace=True)

    print(Counter(df['label'].values))

    Xa = np.array(df.drop(['label', 'Close', 'avg'], axis=1).values)

    data_for_scale = Xa[-1000:]

    scaler = MinMaxScaler(feature_range=(0, 1))
    Xa = scaler.fit_transform(Xa)

    Ya = np.array(df['label'].values)

然后我将数据分成火车并进行测试以适应模型。 训练结束后,我用测试数据测试模型并得到这种预测:

model.predict(x=X_test)
[ 0.64  0.17  0.19]
[ 0.24  0.76  0.  ]
[ 0.47  0.51  0.02]
[ 0.63  0.26  0.11]
[ 0.62  0.1   0.28]
[ 0.14  0.86  0.  ]
[ 0.36  0.63  0.01]
[ 0.57  0.37  0.06]
[ 0.45  0.53  0.02]
[ 0.18  0.82  0.  ]
[ 0.61  0.09  0.3 ]
#the maximum value in the 3th column is 0.45. nevermore!

要使用该模型,我会以这种方式收集数据:

    scaler = MinMaxScaler(feature_range=(0, 1))
    scaler.fit(data_for_scale)

    X_predict = np.array(df.data[current_position-100:current_position][::-1].values).reshape(1, -1)
    X_predict = scaler.transform(X_predict)
    prediccion = model.predict(X_predict.reshape(1, -1))[0]
    #**EDIT 1:** NOTICE i'm predicting just one item at a time

以这种方式得到的预测是:

[ 0.89  0.     0.10]
[ 1.  0.  0.]
[ 0.87  0.11  0.01]
[ 0.99  0.     0.00]
[ 0.30  0.69  0.   ]
[ 0.99  0.     0.00]
[ 0.61  0.     0.38]
[ 0.20  0.     0.79]
[ 0.39  0.     0.60]
[ 0.  0.  1.]
[ 0.00  0.     0.99]
[ 0.12  0.     0.87]
#here i get lots of values above 0.45 in the 3th column. This never 
#happens with the whole test_data...

哪里可能是我的错?我已经尝试保存并重新加载模型,但测试数据的预测没有改变,所以我不认为错误是保存/加载模型。我还尝试将新样本合并到预测到测试集中,fit_scaling整个集合并再次提取新数据但问题仍然存在。 任何提示都是受欢迎的 提前谢谢!

0 个答案:

没有答案