输入测试值超出训练输入值的可怕预测

时间:2019-09-01 12:40:58

标签: python keras regression

仅当测试输入值在火车输入值范围内时,使用回归DL才能获得良好的预测结果

我尝试过minmax和标准缩放器都没有帮助实现改进,我这样做是针对两个回归系统:仅继续值以及继续和类别输入数据的混合,两个系统的结果相似。

import pandas as pd
import numpy as np
import tensorflow as tf
from keras import models 
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras import optimizers
from sklearn.model_selection import KFold
from sklearn.preprocessing import MinMaxScaler
tf.reset_default_graph()
from keras.optimizers import SGD
import pickle

siz=100000
inp0=np.random.randint(100, 1000000 , size=(siz,3))
rand0=np.random.randint(-100, 100 , size=(siz,2))
a1=0.2;a2=0.8;a3=2.5;a4=2.6;a5=1.2;a6=0.3
oup1=np.dot(inp0[:,0],a1)+np.dot(inp0[:,1],a2)+np.dot(inp0[:,2],a3)\
+rand0[:,0]
oup2=np.dot(inp0[:,0],a4)+np.dot(inp0[:,1],a5)+np.dot(inp0[:,2],a6)\
+rand0[:,1]
oup0=np.concatenate((oup1.reshape(siz,1), 
oup2.reshape(siz,1)),axis=1)

normzer_inp = MinMaxScaler()
inp_norm = normzer_inp.fit_transform(inp0)
inp_scale = 'normzer_inp_1.sav'
pickle.dump(normzer_inp, open(inp_scale, 'wb'))

normzer_oup = MinMaxScaler()
oup_norm = normzer_oup.fit_transform(oup0)
oup_scale = 'normzer_oup_1.sav'
pickle.dump(normzer_oup, open(oup_scale, 'wb'))
X=inp_norm
Y=oup_norm
opti_SGD = SGD(lr=0.1, momentum=0.9)
model = Sequential()
model.add(Dense(16, input_dim=X.shape[1], activation='relu'))
model.add(Dense(16, activation='relu'))

model.add(Dense(Y.shape[1], activation='linear'))
model.compile(loss='mean_squared_error', optimizer=opti_SGD)

history = model.fit(X, Y, epochs=100,batch_size=2048, verbose=2)
model.save('DeepL_SelfTst_ContCtg_toCont_5.h5')

siz=10
inpt=np.random.randint(100, 1000000 , size=(siz,3))
randt=np.random.randint(-100, 100 , size=(siz,2))
oupt1=np.dot(inpt[:,0],a1)+np.dot(inpt[:,1],a2)\
+np.dot(inpt[:,2],a3)+randt[:,0]
oupt2=np.dot(inpt[:,0],a4)+np.dot(inpt[:,1],a5)\
+np.dot(inpt[:,2],a6)+randt[:,1]
oupt0=np.concatenate((oupt1.reshape(siz,1), 
oupt2.reshape(siz,1)),axis=1)
inp_scale_2 = 'normzer_inp_1.sav' 
inp_norm_do = pickle.load(open(inp_scale_2, 'rb'))
inp_norm_t = inp_norm_do.transform(inpt)
oup_scale_2 = 'normzer_oup_1.sav' 
oup_norm_do = pickle.load(open(oup_scale_2, 'rb'))

new_model = models.load_model('DeepL_SelfTst_ContCtg_toCont_5.h5')
predictions = new_model.predict(inp_norm_t)
pred_real=oup_norm_do.inverse_transform(predictions)
pre_sult=pred_real.copy()
pre_sult=np.concatenate((pre_sult, oupt0), axis=1)
df = pd.DataFrame({'pred 1': pre_sult[:, 0],\
 'real 1': pre_sult[:, 2],\
              'pred 2': pre_sult[:, 1],'real 2': pre_sult[:, 3],})


        pred 1          real 1          pred 2          real 2
0   2.889692e+06    2889419.3   2.048063e+06    2048469.7
1   2.374380e+06    2374681.2   2.684326e+06    2683353.8
2   2.754795e+06    2754609.5   1.796634e+06    1796445.9
3   4.967822e+05    496659.8    1.372401e+06    1372884.8
4   1.785036e+06    1785088.7   2.686990e+06    2687372.3
5   8.210452e+05    821230.4    1.121044e+06    1120332.6
6   2.124294e+06    2124235.6   9.910031e+05    990209.6
7   1.994700e+06    1995039.0   2.210463e+06    2209663.8
8   3.204532e+06    3204814.4   3.456536e+06    3455951.6
9   2.889887e+06    2889767.8   1.388561e+06    1388345.4

inpt=np.random.randint(1, 100 , size=(siz,3))

    pred 1          real 1  pred 2          real 2
0   49198.234375    -23.2   267707.34375    297.0
1   49355.171875    186.6   267697.28125    114.8
2   49330.609375    220.5   267652.18750    302.1
3   49194.960938    104.3   267697.53125    203.5
4   49245.285156    76.0    267710.71875    87.8
5   49286.746094    141.3   267733.25000    55.7
6   49195.906250    153.1   267700.15625    232.9
7   49348.496094    162.7   267646.90625    404.5
8   49375.519531    262.6   267653.40625    101.8
9   49396.093750    275.7   267686.25000    267.5

我如何训练模型更加灵活,或采用其他任何方法解决该问题?

0 个答案:

没有答案