FateMurphy / CEEMDAN_LSTM

CEEMDAN_LSTM is a Python project for decomposition-integration forecasting models based on EMD methods and LSTM.
MIT License
228 stars 42 forks source link

作者您好 请问如何添加新的预测模型 #34

Open Nilebaba opened 5 months ago

Nilebaba commented 5 months ago

作者,您好,我在您的代码的基础上添加新模型BiGRU def build_model(self, trainset_shape, model_name='Keras model', model_file=None): """ Build Keras model, eg. 'GRU', 'LSTM', 'DNN', 'BPNN', 'CUDNNLSTM', 'CUDNNGRU', model = Sequential(), or load_model. """ if model_file is not None and os.path.exists(str(model_file)): print('Load Keras model:', model_file) return load_model(model_file) # load user's saving custom model elif isinstance(self.KERAS_MODEL, Sequential): # if not load a model return self.KERAS_MODEL
elif self.KERAS_MODEL == 'LSTM': model = Sequential(name=model_name) model.add(LSTM(self.units4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', activation=self.activation, return_sequences=True)) model.add(Dropout(self.dropout)) model.add(LSTM(self.units2, recurrent_activation='sigmoid', activation=self.activation, return_sequences=True)) model.add(Dropout(self.dropout)) model.add(LSTM(self.units, recurrent_activation='sigmoid', activation=self.activation, return_sequences=False)) model.add(Dropout(self.dropout)) model.add(Dense(1, activation=self.activation)) model.compile(loss=self.opt_loss, optimizer=self.opt) return model elif self.KERAS_MODEL == 'GRU': model = Sequential(name=model_name) model.add(GRU(self.units4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=True)) model.add(Dropout(self.dropout)) model.add(GRU(self.units2, recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=True)) model.add(Dropout(self.dropout)) model.add(GRU(self.units, recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=False)) model.add(Dropout(self.dropout)) model.add(Dense(1, activation=self.activation)) model.compile(loss=self.opt_loss, optimizer=self.opt) return model elif self.KERAS_MODEL == 'DNN': model = Sequential(name=model_name) model.add(Flatten(input_shape=(trainset_shape[1], trainsetshape[2]))) for in range(8): model.add(Dense(self.units*4, activation=self.activation))

model.add(BatchNormalization())

            model.add(Dropout(self.dropout))
        model.add(Dense(self.units*2, activation=self.activation)) 
        model.add(Dropout(self.dropout))
        model.add(Dense(self.units, activation=self.activation)) 
        model.add(Dropout(self.dropout))
        model.add(Dense(1, activation=self.activation))
        model.compile(loss=self.opt_loss, optimizer=self.opt)
        return model
    elif self.KERAS_MODEL == 'BPNN':
        model = Sequential(name=model_name)
        model.add(Dense(self.units*4, input_shape=(trainset_shape[1], trainset_shape[2]), activation=self.activation))
        model.add(Dropout(self.dropout))
        model.add(Flatten())
        model.add(Dense(1, activation=self.activation))
        model.compile(loss=self.opt_loss, optimizer=self.opt)
        return model
  elif self.KERAS_MODEL == 'BiGRU':
        model = Sequential(name=model_name)
        model.add(Bidirectional(GRU(self.units*4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', 
                      reset_after=True, activation=self.activation, return_sequences=True)))
        model.add(Dropout(self.dropout))
        model.add(Flatten())
        model.add(Dense(1, activation=self.activation))
        model.compile(loss=self.opt_loss, optimizer=self.opt)
        return model

但是并不能被识别到 出现如下报错: Traceback (most recent call last): File "c:\Users\GGBond\Desktop\CAN_LSTM\main.py", line 34, in df_result = kr.hybrid_keras_predict(data=series, show=True, plot=True, save=True) File "c:\Users\GGBond\Desktop\CAN_LSTM\CEEMDAN_LSTM_library\keras_predictor.py", line 599, in hybrid_keras_predict df_result = self.keras_predict(data=df, show_model=show, **kwargs) # the ensemble method with matrix input File "c:\Users\GGBond\Desktop\CAN_LSTM\CEEMDAN_LSTM_library\keras_predictor.py", line 327, in keras_predict else: model = self.build_model(x_train.shape, data.name, model_file) File "c:\Users\GGBond\Desktop\CAN_LSTM\CEEMDAN_LSTM_library\keras_predictor.py", line 247, in build_model else: raise ValueError("%s is an invalid input for KERAS_MODEL! eg. 'GRU', 'LSTM', or model = Sequential()"%self.KERAS_MODEL) ValueError: BIGRU is an invalid input for KERAS_MODEL! eg. 'GRU', 'LSTM', or model = Sequential()

请问在代码的哪个地方能修改使之可以添加新的模型呢

FateMurphy commented 5 months ago

1、你这样设置使用新的模型是可以的,可能是导入的包不是你修改后的。 2、你还可以尝试直接修改KERAS_MODEL这个全局变量。 例如: model = Sequential(name='BiGRU') model.add(Bidirectional(GRU(self.units4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=True))) model.add(Dropout(self.dropout)) model.add(Flatten()) model.add(Dense(1, activation=self.activation)) model.compile(loss=self.opt_loss, optimizer=self.opt)

KERAS_MODEL = model 注意这种方式需要预先定义dropout,units4等变量

Nilebaba commented 5 months ago

1、你这样设置使用新的模型是可以的,可能是导入的包不是你修改后的。 2、你还可以尝试直接修改KERAS_MODEL这个全局变量。 例如: model = Sequential(name='BiGRU') model.add(Bidirectional(GRU(self.units4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=True))) model.add(Dropout(self.dropout)) model.add(Flatten()) model.add(Dense(1, activation=self.activation)) model.compile(loss=self.opt_loss, optimizer=self.opt)

KERAS_MODEL = model 注意这种方式需要预先定义dropout,units4等变量

很感谢您的回答 我解决了问题 发现是要把模型名称都设置成大写的 中间有些转换 导致小写的检测不到

Nilebaba commented 5 months ago

1、你这样设置使用新的模型是可以的,可能是导入的包不是你修改后的。 2、你还可以尝试直接修改KERAS_MODEL这个全局变量。 例如: model = Sequential(name='BiGRU') model.add(Bidirectional(GRU(self.units4, input_shape=(trainset_shape[1], trainset_shape[2]), recurrent_activation='sigmoid', reset_after=True, activation=self.activation, return_sequences=True))) model.add(Dropout(self.dropout)) model.add(Flatten()) model.add(Dense(1, activation=self.activation)) model.compile(loss=self.opt_loss, optimizer=self.opt)

KERAS_MODEL = model 注意这种方式需要预先定义dropout,units4等变量

此外还有一个问题问您 就是添加TCN模型的时候 好像检测不到这个模型 我使用的是keras-tcn的api 然后就报错 ValueError: Unknown layer: TCN. Please ensure this object is passed to the custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details. 我根据报错修改代码了 if self.PATH is not None: model = model = load_model(self.PATH+data.name) 添加了custom_objects={'TCN': TCN} if self.PATH is not None: model = model = load_model(self.PATH+data.name,custom_objects={'TCN': TCN}) 但是并不能预测 我个人感觉是没检测到正常的层 在您之前的代码中我看到有使用TCN 但又放弃了 请问有解决的办法吗

FateMurphy commented 5 months ago

因为之前在使用TCN没有得出正确的结果,所以没有启用tcn的包。tcn不是keras内置的模型所以无法导入,需要在安装好的添包keras_predictor文件中加form keras-tcn import TCN

Nilebaba commented 5 months ago

因为之前在使用TCN没有得出正确的结果,所以没有启用tcn的包。tcn不是keras内置的模型所以无法导入,需要在安装好的添包keras_predictor文件中加form keras-tcn import TCN

是的 我就是导入 但是结果就是根本没有预测 很奇怪 不知道怎么回事

FateMurphy commented 5 months ago

具体原因可能比较难找,可以先用这个简化版的试试 CEEMDAN-VMD-GRU

Nilebaba commented 5 months ago

具体原因可能比较难找,可以先用这个简化版的试试 CEEMDAN-VMD-GRU

好的 谢谢您 简版上上面我是测试成功的