123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import keras
- # -*- encoding:utf-8 -*-
- import numpy as np
- from keras.models import Sequential
- # 优化方法选用Adam(其实可选项有很多,如SGD)
- from keras.optimizers import Adam
- import random
- from keras.models import load_model
- from imblearn.over_sampling import RandomOverSampler
- from keras.utils import np_utils
- # 用于模型初始化,Conv2D模型初始化、Activation激活函数,MaxPooling2D是池化层
- # Flatten作用是将多位输入进行一维化
- # Dense是全连接层
- from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
- from keras.layers import LSTM
- from keras import regularizers
- from keras.models import Model
- from keras.callbacks import EarlyStopping
- early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
- epochs= 330
- model_path = '161_18d_lstm_5D_ma5_s_seq.h5'
- data_dir = 'D:\\data\\quantization\\dmi\\'
- args = 'stock160_18d_train_A_'
- def read_data(path):
- lines = []
- with open(path) as f:
- for line in f.readlines()[:]:
- lines.append(eval(line.strip()))
- random.shuffle(lines)
- print('读取数据完毕')
- d=int(0.7*len(lines))
- train_x=[s[:-2] for s in lines[0:d]]
- train_y=[s[-1] for s in lines[0:d]]
- test_x=[s[:-2] for s in lines[d:]]
- test_y=[s[-1] for s in lines[d:]]
- print('转换数据完毕')
- ros = RandomOverSampler(random_state=0)
- X_resampled, y_resampled = ros.fit_sample(np.array(train_x), np.array(train_y))
- print('数据重采样完毕')
- return X_resampled,y_resampled,np.array(test_x),np.array(test_y)
- def mul_train(name="10_18d"):
- for x in range(0, 12):
- score = train(data_dir + name + str(x) + ".log", x) # stock160_18d_trai_0
- with open(data_dir + name + '_lstm.log', 'a') as f:
- f.write(str(x) + ':' + str(score[1]) + '\n')
- def train(file_path, idx):
- train_x,train_y,test_x,test_y=read_data(file_path)
- train_x_a = train_x[:,:18*24]
- train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24)
- # train_x_b = train_x[:, 18*24:18*16+10*18]
- # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 10, 1)
- train_x_c = train_x[:,18*24:]
- # create the MLP and CNN models
- mlp = create_mlp(train_x_c.shape[1], regress=False)
- cnn_0 = create_lstm(train_x_a.shape[1], 18, 24)
- # cnn_1 = create_cnn(18, 10, 1, kernel_size=(3, 5), filters=32, regress=False, output=120)
- # create the input to our final set of layers as the *output* of both
- # the MLP and CNN
- combinedInput = concatenate([mlp.output, cnn_0.output,])
- # our final FC layer head will have two dense layers, the final one
- # being our regression head
- x = Dense(256, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
- x = Dropout(0.2)(x)
- x = Dense(256, activation="relu")(x)
- x = Dense(512, activation="relu")(x)
- # 在建设一层
- x = Dense(5, activation="softmax")(x)
- # our final model will accept categorical/numerical data on the MLP
- # input and images on the CNN input, outputting a single value (the
- # predicted price of the house)
- model = Model(inputs=[mlp.input, cnn_0.input,], outputs=x)
- print("Starting training ")
- # h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
- # compile the model using mean absolute percentage error as our loss,
- # implying that we seek to minimize the absolute percentage difference
- # between our price *predictions* and the *actual prices*
- opt = Adam(lr=1e-3, decay=1e-3 / 200)
- model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy'],
- )
- # train the model
- print("[INFO] training model...")
- model.fit(
- [train_x_c, train_x_a, ], train_y,
- # validation_data=([testAttrX, testImagesX], testY),
- # epochs=int(3*train_x_a.shape[0]/1300),
- epochs=epochs,
- batch_size=4096, shuffle=True,
- callbacks=[early_stopping]
- )
- test_x_a = test_x[:,:18*24]
- test_x_a = test_x_a.reshape(test_x.shape[0], 18, 24)
- # test_x_b = test_x[:, 18*16:18*16+10*18]
- # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 10, 1)
- test_x_c = test_x[:,18*24:]
- # make predictions on the testing data
- print("[INFO] predicting house prices...")
- score = model.evaluate([test_x_c, test_x_a], test_y)
- print(score)
- print('Test score:', score[0])
- print('Test accuracy:', score[1])
- model.save(model_path.split('.')[0] + '_' + str(idx) + '.h5')
- return score
- def create_mlp(dim, regress=False):
- # define our MLP network
- model = Sequential()
- model.add(Dense(64, input_dim=dim, activation="relu"))
- model.add(Dense(64, activation="relu"))
- # check to see if the regression node should be added
- if regress:
- model.add(Dense(1, activation="linear"))
- # return our model
- return model
- def create_cnn(width, height, depth, filters=32, kernel_size=(5, 6), regress=False, output=24):
- # initialize the input shape and channel dimension, assuming
- # TensorFlow/channels-last ordering
- inputShape = (width, height, 1)
- chanDim = -1
- # define the model input
- inputs = Input(shape=inputShape)
- x = inputs
- # CONV => RELU => BN => POOL
- x = Conv2D(filters, kernel_size, strides=(2,2), padding="same",
- # data_format='channels_first'
- )(x)
- x = Activation("relu")(x)
- x = BatchNormalization(axis=chanDim)(x)
- # x = MaxPooling2D(pool_size=(2, 2))(x)
- # if width > 2:
- # x = Conv2D(32, (10, 6), padding="same")(x)
- # x = Activation("relu")(x)
- # x = BatchNormalization(axis=chanDim)(x)
- # flatten the volume, then FC => RELU => BN => DROPOUT
- x = Flatten()(x)
- x = Dense(output)(x)
- x = Activation("relu")(x)
- x = BatchNormalization(axis=chanDim)(x)
- x = Dropout(0.2)(x)
- # apply another FC layer, this one to match the number of nodes
- # coming out of the MLP
- x = Dense(output)(x)
- x = Activation("relu")(x)
- # check to see if the regression node should be added
- if regress:
- x = Dense(1, activation="linear")(x)
- # construct the CNN
- model = Model(inputs, x)
- # return the CNN
- return model
- def create_lstm(sample, timesteps, input_dim):
- inputShape = (timesteps, input_dim)
- # define the model input
- inputs = Input(shape=inputShape)
- x = inputs
- x = LSTM(units = 64, input_shape=(timesteps, input_dim), dropout=0.2
- )(x)
- # x = LSTM(16*16, return_sequences=False)
- # x = Activation("relu")(x)
- x = Dense(64)(x)
- x = Dropout(0.2)(x)
- x = Activation("relu")(x)
- # construct the CNN
- model = Model(inputs, x)
- # return the CNN
- return model
- if __name__ == '__main__':
- mul_train(args)
|