train_jqxx_lstm.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. '''
  4. 最简单的mse
  5. '''
  6. import sys
  7. import os
  8. sys.path.append(os.path.abspath('..'))
  9. from util.config import config
  10. import numpy as np
  11. from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
  12. from keras.layers import LSTM
  13. from sklearn import metrics
  14. from sklearn.model_selection import train_test_split
  15. from sklearn.metrics import accuracy_score
  16. from imblearn.over_sampling import RandomOverSampler
  17. import joblib
  18. from keras.models import Sequential
  19. from keras.models import Model
  20. from keras.optimizers import Adam
  21. from keras import regularizers
  22. from keras.callbacks import EarlyStopping
  23. epochs= 330
  24. early_stopping = EarlyStopping(monitor='accuracy', patience=30, verbose=2)
  25. def curce_data(x,y,y_pred):
  26. x=x.tolist()
  27. y=y.tolist()
  28. y_pred=y_pred.tolist()
  29. results=zip(x,y,y_pred)
  30. results=["{},{},{}".format(s[0],s[1][0],s[2][0]) for s in results ]
  31. return results
  32. def read_data(path):
  33. with open(path) as f :
  34. lines=f.readlines()
  35. lines=[eval(line.strip()) for line in lines]
  36. X,z,y=zip(*lines)
  37. X=np.array(X)
  38. y=np.array(y)
  39. return X,y
  40. def create_mlp(dim, regress=False):
  41. # define our MLP network
  42. model = Sequential()
  43. model.add(Dense(64, input_dim=dim, activation="relu"))
  44. model.add(Dense(64, activation="relu"))
  45. # check to see if the regression node should be added
  46. if regress:
  47. model.add(Dense(1, activation="linear"))
  48. # return our model
  49. return model
  50. def create_lstm(sample, timesteps, input_dim):
  51. inputShape = (timesteps, input_dim)
  52. # define the model input
  53. inputs = Input(shape=inputShape)
  54. x = inputs
  55. x = LSTM(units = 64, input_shape=(timesteps, input_dim), dropout=0.2)(x)
  56. # x = LSTM(16*16, return_sequences=False)
  57. # x = Activation("relu")(x)
  58. x = Dense(64)(x)
  59. x = Dropout(0.2)(x)
  60. x = Activation("relu")(x)
  61. # construct the CNN
  62. model = Model(inputs, x)
  63. # return the CNN
  64. return model
  65. def demo_1(file, model_file):
  66. X_train,y_train=read_data(file)
  67. Xtrain, Xtest, Ytrain, Ytest = train_test_split(X_train, y_train, test_size=0.1)
  68. train_x_a = Xtrain.reshape(Xtrain.shape[0], windows, x_lenth)
  69. test_x_a = Xtest.reshape(Xtest.shape[0], windows, x_lenth)
  70. # 随机过采样
  71. # ros = RandomOverSampler(random_state=22)
  72. # X_rsampled, y_resampled = ros.fit_resample(X_train, y_train)
  73. # 一调用这个函数,就会不停地找合适的w和b 直到误差最小
  74. # create the MLP and CNN models
  75. # mlp = create_mlp(Xtrain.shape[1], regress=False)
  76. cnn_0 = create_lstm(train_x_a.shape[1], windows, x_lenth)
  77. # cnn_1 = create_cnn(18, 10, 1, kernel_size=(3, 5), filters=32, regress=False, output=120)
  78. # create the input to our final set of layers as the *output* of both
  79. # the MLP and CNN
  80. # combinedInput = concatenate([cnn_0.output,])
  81. # our final FC layer head will have two dense layers, the final one
  82. # being our regression head
  83. x = Dense(128, activation="relu", kernel_regularizer=regularizers.l1(0.003))(cnn_0.input)
  84. x = Dropout(0.1)(x)
  85. x = Dense(56, activation="relu")(x)
  86. x = Dense(56, activation="relu")(x)
  87. x = Dense(56, activation="relu")(x)
  88. x = Dense(56, activation="relu")(x)
  89. x = Flatten()(x)
  90. # 在建设一层
  91. x = Dense(2, activation="sigmoid")(x)
  92. # our final model will accept categorical/numerical data on the MLP
  93. # input and images on the CNN input, outputting a single value (the
  94. # predicted price of the house)
  95. model = Model(inputs=[cnn_0.input,], outputs=x)
  96. print("Starting training ")
  97. # h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
  98. # compile the model using mean absolute percentage error as our loss,
  99. # implying that we seek to minimize the absolute percentage difference
  100. # between our price *predictions* and the *actual prices*
  101. opt = Adam(lr=1e-3, decay=1e-3 / 200)
  102. model.compile(loss="binary_crossentropy", optimizer=opt, metrics=['accuracy'],
  103. )
  104. # train the model
  105. print("[INFO] training model...")
  106. model.fit(
  107. [train_x_a], Ytrain,
  108. validation_data=([test_x_a], Ytest),
  109. # epochs=int(3*train_x_a.shape[0]/1300),
  110. epochs=epochs,
  111. batch_size=1024, shuffle=True,
  112. callbacks=[early_stopping]
  113. )
  114. model.save(model_file.split('.')[0] + '_' + '.h5')
  115. score = model.evaluate([test_x_a], Ytest)
  116. print('MIX', score)
  117. windows = 5
  118. x_lenth = 19
  119. if __name__ == '__main__':
  120. root_dir = 'D:\\data\\quantization\\jqxx2\\'
  121. model_dir = 'D:\\data\\quantization\\jqxx2_svm_model\\'
  122. m = '000007.SH.log' # 12
  123. demo_1(root_dir + m, model_dir + str(m)[:6] + '.pkl')