tf_cnn.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # @FileName :nn_bp.py
  4. # @Time :2025/2/12 10:41
  5. # @Author :David
  6. # @Company: shenyang JY
  7. from tensorflow.keras.layers import Input, Dense, LSTM, concatenate, Conv1D, Conv2D, MaxPooling1D, Reshape, Flatten
  8. from tensorflow.keras.models import Model, load_model
  9. from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, ReduceLROnPlateau
  10. from tensorflow.keras import optimizers, regularizers
  11. from models_processing.model_tf.losses import region_loss
  12. from models_processing.model_tf.settings import set_deterministic
  13. import numpy as np
  14. from common.database_dml_koi import *
  15. from threading import Lock
  16. import argparse
  17. model_lock = Lock()
  18. set_deterministic(42)
  19. class CNNHandler(object):
  20. def __init__(self, logger, args):
  21. self.logger = logger
  22. self.opt = argparse.Namespace(**args)
  23. self.model = None
  24. self.model_params = None
  25. def get_model(self, args):
  26. """
  27. 单例模式+线程锁,防止在异步加载时引发线程安全
  28. """
  29. try:
  30. with model_lock:
  31. loss = region_loss(self.opt)
  32. self.model, self.model_params = get_keras_model_from_mongo(args, {type(loss).__name__: loss})
  33. except Exception as e:
  34. self.logger.info("加载模型权重失败:{}".format(e.args))
  35. @staticmethod
  36. def get_keras_model(opt):
  37. loss = region_loss(opt)
  38. l1_reg = regularizers.l1(opt.Model['lambda_value_1'])
  39. l2_reg = regularizers.l2(opt.Model['lambda_value_2'])
  40. nwp_input = Input(shape=(opt.Model['time_step'], opt.Model['input_size']), name='nwp')
  41. con1 = Conv1D(filters=64, kernel_size=1, strides=1, padding='valid', activation='relu', kernel_regularizer=l2_reg)(nwp_input)
  42. d1 = Dense(32, activation='relu', name='d1', kernel_regularizer=l1_reg)(con1)
  43. nwp = Dense(8, activation='relu', name='d2', kernel_regularizer=l1_reg)(d1)
  44. output = Dense(1, name='d5')(nwp)
  45. output_f = Flatten()(output)
  46. model = Model(inputs=nwp_input, outputs=output_f)
  47. adam = optimizers.Adam(learning_rate=opt.Model['learning_rate'], beta_1=0.9, beta_2=0.999, epsilon=1e-7, amsgrad=True)
  48. reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=5, verbose=1)
  49. model.compile(loss=loss, optimizer=adam)
  50. return model
  51. def train_init(self):
  52. try:
  53. # 进行加强训练,支持修模
  54. loss = region_loss(self.opt)
  55. base_train_model, self.model_params = get_keras_model_from_mongo(vars(self.opt), {type(loss).__name__: loss})
  56. base_train_model.summary()
  57. self.logger.info("已加载加强训练基础模型")
  58. return base_train_model
  59. except Exception as e:
  60. self.logger.info("加载模型权重失败:{}".format(e.args))
  61. return False
  62. def training(self, model, train_and_valid_data):
  63. # tf.reset_default_graph() # 清除默认图
  64. train_x, train_y, valid_x, valid_y = train_and_valid_data
  65. print("----------", np.array(train_x[0]).shape)
  66. print("++++++++++", np.array(train_x[1]).shape)
  67. model.summary()
  68. early_stop = EarlyStopping(monitor='val_loss', patience=self.opt.Model['patience'], mode='auto')
  69. history = model.fit(train_x, train_y, batch_size=self.opt.Model['batch_size'], epochs=self.opt.Model['epoch'], verbose=2, validation_data=(valid_x, valid_y), callbacks=[early_stop], shuffle=False)
  70. loss = np.round(history.history['loss'], decimals=5)
  71. val_loss = np.round(history.history['val_loss'], decimals=5)
  72. self.logger.info("-----模型训练经过{}轮迭代-----".format(len(loss)))
  73. self.logger.info("训练集损失函数为:{}".format(loss))
  74. self.logger.info("验证集损失函数为:{}".format(val_loss))
  75. return model
  76. def predict(self, test_x, batch_size=1):
  77. result = self.model.predict(test_x, batch_size=batch_size)
  78. self.logger.info("执行预测方法")
  79. return result
  80. if __name__ == "__main__":
  81. run_code = 0