|
@@ -77,8 +77,8 @@ def train_data_handler(data, opt):
|
|
|
scaled_train_bytes = BytesIO()
|
|
|
joblib.dump(scaled_train_data, scaled_train_bytes)
|
|
|
scaled_train_bytes.seek(0) # Reset pointer to the beginning of the byte stream
|
|
|
- x_train, x_valid, y_train, y_valid = dh.get_train_data(train_datas, col_time, features, target)
|
|
|
- return x_train, x_valid, y_train, y_valid, scaled_train_bytes
|
|
|
+ train_x, valid_x, train_y, valid_y = dh.get_train_data(train_datas, col_time, features, target)
|
|
|
+ return train_x, valid_x, train_y, valid_y, scaled_train_bytes
|
|
|
|
|
|
def pre_data_handler(data, args):
|
|
|
if 'is_limit' in data.columns:
|
|
@@ -118,8 +118,9 @@ class BPHandler(object):
|
|
|
d1 = Dense(32, activation='relu', name='d1', kernel_regularizer=l1_reg)(con1)
|
|
|
nwp = Dense(8, activation='relu', name='d2', kernel_regularizer=l1_reg)(d1)
|
|
|
|
|
|
- output = Dense(opt.Model['output_size'], name='d5')(nwp)
|
|
|
- model = Model(nwp_input, output)
|
|
|
+ output = Dense(1, name='d5')(nwp)
|
|
|
+ output_f = Flatten()(output)
|
|
|
+ model = Model(nwp_input, output_f)
|
|
|
adam = optimizers.Adam(learning_rate=opt.Model['learning_rate'], beta_1=0.9, beta_2=0.999, epsilon=1e-7, amsgrad=True)
|
|
|
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=5, verbose=1)
|
|
|
model.compile(loss=rmse, optimizer=adam)
|
|
@@ -144,7 +145,7 @@ class BPHandler(object):
|
|
|
train_x, train_y, valid_x, valid_y = train_and_valid_data
|
|
|
print("----------", np.array(train_x[0]).shape)
|
|
|
print("++++++++++", np.array(train_x[1]).shape)
|
|
|
-
|
|
|
+ model.summary()
|
|
|
check_point = ModelCheckpoint(filepath='./var/' + 'fmi.h5', monitor='val_loss', save_best_only=True, mode='auto')
|
|
|
early_stop = EarlyStopping(monitor='val_loss', patience=opt.Model['patience'], mode='auto')
|
|
|
history = model.fit(train_x, train_y, batch_size=opt.Model['batch_size'], epochs=opt.Model['epoch'], verbose=2, validation_data=(valid_x, valid_y), callbacks=[check_point, early_stop], shuffle=False)
|
|
@@ -155,12 +156,12 @@ class BPHandler(object):
|
|
|
self.logger.info("验证集损失函数为:{}".format(val_loss))
|
|
|
return model
|
|
|
|
|
|
- def predict(self, test_X, batch_size=1):
|
|
|
- result = self.model.predict(test_X, batch_size=batch_size)
|
|
|
+ def predict(self, test_x, batch_size=1):
|
|
|
+ result = self.model.predict(test_x, batch_size=batch_size)
|
|
|
self.logger.info("执行预测方法")
|
|
|
return result
|
|
|
|
|
|
-@app.route('/model_training_bp', methods=['POST'])
|
|
|
+@app.route('/nn_bp_training', methods=['POST'])
|
|
|
def model_training_bp():
|
|
|
# 获取程序开始时间
|
|
|
start_time = time.time()
|
|
@@ -168,9 +169,10 @@ def model_training_bp():
|
|
|
success = 0
|
|
|
bp = BPHandler(logger)
|
|
|
print("Program starts execution!")
|
|
|
+ args_dict = request.values.to_dict()
|
|
|
+ args = arguments.deepcopy()
|
|
|
+ args.update(args_dict)
|
|
|
try:
|
|
|
- args_dict = request.values.to_dict()
|
|
|
- args = arguments.deepcopy()
|
|
|
opt = argparse.Namespace(**args)
|
|
|
logger.info(args_dict)
|
|
|
train_data = get_data_from_mongo(args_dict)
|
|
@@ -195,7 +197,7 @@ def model_training_bp():
|
|
|
return result
|
|
|
|
|
|
|
|
|
-@app.route('/model_prediction_bp', methods=['POST'])
|
|
|
+@app.route('/nn_bp_predict', methods=['POST'])
|
|
|
def model_prediction_bp():
|
|
|
# 获取程序开始时间
|
|
|
start_time = time.time()
|
|
@@ -203,15 +205,15 @@ def model_prediction_bp():
|
|
|
success = 0
|
|
|
bp = BPHandler(logger)
|
|
|
print("Program starts execution!")
|
|
|
+ params_dict = request.values.to_dict()
|
|
|
+ args = arguments.deepcopy()
|
|
|
+ args.update(params_dict)
|
|
|
try:
|
|
|
- params_dict = request.values.to_dict()
|
|
|
- args = arguments.deepcopy()
|
|
|
- args.update(params_dict)
|
|
|
- opt = argparse.Namespace(**args)
|
|
|
print('args', args)
|
|
|
logger.info(args)
|
|
|
predict_data = get_data_from_mongo(args)
|
|
|
scaled_features = pre_data_handler(predict_data, args)
|
|
|
+ bp.get_model(args)
|
|
|
result = bp.predict(scaled_features, args)
|
|
|
insert_data_into_mongo(result, args)
|
|
|
success = 1
|
|
@@ -249,5 +251,9 @@ if __name__ == "__main__":
|
|
|
train_x, valid_x, train_y, valid_y, scaled_train_bytes = train_data_handler(train_data, opt)
|
|
|
|
|
|
bp_model = bp.training(opt, [train_x, train_y, valid_x, valid_y])
|
|
|
+
|
|
|
+ args_dict['gen_time'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
|
|
|
+ args_dict['params'] = arguments
|
|
|
+ args_dict['descr'] = '测试'
|
|
|
insert_trained_model_into_mongo(bp_model, args_dict)
|
|
|
insert_scaler_model_into_mongo(scaled_train_bytes, args_dict)
|