m0_74420115 2024-05-06 22:54 采纳率: 71.9%
浏览 1
已结题

怎么把lstm模型换为wdcnnlstm模型

这是用LSTM找预测股票价格的最优模型,怎么在这个代码的基础上使用WD_CNN_LSTM来寻找最优模型


import pandas as pd
def parse_date(date_string):
    return pd.Timestamp(date_string.replace('_', '-'))
df = pd.read_csv('D:/LSTMdata.csv', index_col='Date', parse_dates=True, date_parser=parse_date)
df.sort_index(inplace=True)
def Stock_Price_LSTM_Data_Precesing(df,mem_his_days,pre_days):
    df.dropna(inplace=True)
    df.sort_index(inplace=True)
    df['label']= df['Close'].shift(-pre_days)
    from sklearn.preprocessing import StandardScaler
    scaler = StandardScaler()
    sca_X=scaler.fit_transform(df.iloc[:,:-1])
    
    
    mem_his_days = 10
    
    from collections import deque
    deq = deque(maxlen=mem_his_days)
    
    X = []
    for i in sca_X:
        deq.append(list(i))
        if len(deq)==mem_his_days:
            X.append(list(deq))
    X_lately = X[-pre_days:]
    X = X[:-pre_days]
    y = df['label'].values[mem_his_days-1:-pre_days]
    
    
    import numpy as np
    X = np.array(X)
    y = np.array(y)
    return X,y,X_lately
X,y,X_lately = Stock_Price_LSTM_Data_Precesing(df,5,10)
print(len(X))
print(len(y))
print(len(X_lately))
pre_days = 10
mem_days=[5,10,15]
lstm_layers=[1,2,3]
dense_layers=[1,2,3]
units = [16,32]
# mem_days=[10]
# lstm_layers=[1]
# dense_layers=[1]
# units = [32]

from tensorflow.keras.callbacks import ModelCheckpoint
for the_mem_days in mem_days:
    for the_lstm_layers in lstm_layers:
        for the_dense_layers in dense_layers:
            for the_units in units:
                filepath=filepath=f"./theLSTMbestmodel1/{{val_mape:.2f}}{{epoch:02d}}men{the_mem_days}lstm{the_lstm_layers}dense{the_dense_layers}unit{the_units}.keras"
                checkpoint = ModelCheckpoint(
                    filepath=filepath,
                    save_weights_only=False,
                    monitor='val_mape',
                    mode='min',
                    save_best_only=True)
                X,y,X_lately = Stock_Price_LSTM_Data_Precesing(df,the_mem_days,pre_days)
                from sklearn.model_selection import train_test_split
                X_train,X_test,y_train,y_test = train_test_split(X,y,shuffle=False,test_size=0.1)
                import tensorflow as tf
                from tensorflow.keras.models import Sequential
                from tensorflow.keras.layers import LSTM,Dense,Dropout
                model = Sequential()
                model.add(LSTM(the_units,input_shape=X.shape[1:],activation='relu',return_sequences=True))
                model.add(Dropout(0.1))
                for i in range(the_lstm_layers):
                    model.add(LSTM(the_units,activation='relu',return_sequences=True))
                    model.add(Dropout(0.1))
                
                model.add(LSTM(the_units,activation='relu'))
                model.add(Dropout(0.1))
                for i in range(the_dense_layers):
                    model.add(Dense(the_units,activation='relu'))
                    model.add(Dropout(0.1))
                
                model.add(Dense(1))
                model.compile(optimizer='adam',
                             loss='mse',
                             metrics=['mape'])
                model.fit(X_train,y_train,batch_size=32,epochs=50,validation_data=(X_test,y_test),callbacks=[checkpoint])       
  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-05-06 22:54
    关注

    引自免费微信小程序:皆我百晓生

    要将LSTM模型替换为WD_CNN_LSTM(卷积门控循环单元)模型,首先我们需要了解WD_CNN_LSTM的基本结构。WD_CNN_LSTM结合了卷积神经网络(CNN)的局部感知能力和长短期记忆(LSTM)在网络中的时间序列处理优势。在WD_CNN_LSTM中,通常会先使用一个卷积层来捕获输入数据的局部特征,然后这些特征被传递给LSTM层进行时序建模。

    下面是修改后的代码,将原始LSTM模型替换为WD_CNN_LSTM模型:

    import pandas as pd
    
    def parse_date(date_string):
        return pd.Timestamp(date_string.replace('_', '-'))
    
    df = pd.read_csv('D:/LSTMdata.csv', index_col='Date', parse_dates=True, date_parser=parse_date)
    df.sort_index(inplace=True)
    
    def Stock_Price_LSTM_Data_Precesing(df, mem_his_days, pre_days):
        df.dropna(inplace=True)
        df.sort_index(inplace=True)
        df['label'] = df['Close'].shift(-pre_days)
        from sklearn.preprocessing import StandardScaler
        scaler = StandardScaler()
        sca_X = scaler.fit_transform(df.iloc[:, :-1])
    
        mem_his_days = 10
    
        from collections import deque
        deq = deque(maxlen=mem_his_days)
    
        X = []
        for i in sca_X:
            deq.append(list(i))
            if len(deq) == mem_his_days:
                X.append(list(deq))
        X_lately = X[-pre_days:]
        X = X[:-pre_days]
        y = df['label'].values[mem_his_days - 1:-pre_days]
    
        import numpy as np
        X = np.array(X)
        y = np.array(y)
        return X, y, X_lately
    
    X, y, X_lately = Stock_Price_LSTM_Data_Precesing(df, 5, 10)
    print(len(X))
    print(len(y))
    print(len(X_lately))
    
    pre_days = 10
    mem_days = [5, 10, 15]
    wd_cnn_lstm_layers = [1, 2, 3]
    dense_layers = [1, 2, 3]
    units = [16, 32]
    
    # mem_days=[10]
    # wd_cnn_lstm_layers=[1]
    # dense_layers=[1]
    # units = [32]
    
    from tensorflow.keras.callbacks import ModelCheckpoint
    for the_mem_days in mem_days:
        for the_wd_cnn_lstm_layers in wd_cnn_lstm_layers:
            for the_dense_layers in dense_layers:
                for the_units in units:
                    filepath = f"./theLSTMbestmodel1/{{val_mape:.2f}}{{epoch:02d}}men{the_mem_days}wdclstm{the_wd_cnn_lstm_layers}dense{the_dense_layers}unit{the_units}.keras"
                    checkpoint = ModelCheckpoint(
                        filepath=filepath,
                        save_weights_only=False,
                        monitor='val_mape',
                        mode='min',
                        save_best_only=True)
                    X, y, X_lately = Stock_Price_LSTM_Data_Precesing(df, the_mem_days, pre_days)
                    from sklearn.model_selection import train_test_split
                    X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False, test_size=0.1)
                    import tensorflow as tf
                    from tensorflow.keras.models import Sequential
                    from tensorflow.keras.layers import Conv1D, LSTM, Dense, Dropout
    
                    # 使用卷积层作为输入层
                    model = Sequential()
                    model.add(Conv1D(filters=the_units, kernel_size=3, activation='relu', input_shape=X.shape[1:]))
                    model.add(MaxPooling1D(pool_size=2))
                    model.add(Flatten())
    
                    # 添加WD_CNN_LSTM层
                    for i in range(the_wd_cnn_lstm_layers):
                        model.add(WDCLSTM(units=the_units, return_sequences=True))  # 假设WDCLSTM是定义好的WD_CNN_LSTM层
                        model.add(Dropout(0.1))
    
                    model.add(WDCLSTM(units=the_units))
                    model.add(Dropout(0.1))
    
                    # 添加全连接层
                    for i in range(the_dense_layers):
                        model.add(Dense(units=the_units, activation='relu'))
                        model.add(Dropout(0.1))
    
                    model.add(Dense(1))
                    model.compile(optimizer='adam',
                                  loss='mse',
                                  metrics=['mape'])
    
                    model.fit(X_train, y_train, batch_size=32, epochs=50, validation_data=(X_test, y_test), callbacks=[checkpoint])
    
    # 注意:这里我们假设了一个名为WDCLSTM的层,你需要自己实现这个WD_CNN_LSTM层。
    # 可以参考Keras官方文档或其他库实现类似功能的层,例如tf.keras.layers.CuDNNGRU等。
    # 或者使用预训练的模块,如TF-Hub的模块。
    

    在这个代码中,我做了以下更改:

    1. 将LSTM层替换为WDCLSTM层,这是一个假设的层,你需要根据实际情况实现它。
    2. 在输入层添加了一个卷积层(Conv1D),用于提取局部特征。
    3. 修改了文件路径中的模型名称,以便区分WD_CNN_LSTM模型。

    记得在运行这段代码之前,确保你已经实现了WDCLSTM层或者找到了可用的预训练模块。此外,由于WD_CNN_LSTM可能不是标准的Keras层,因此你可能需要自行搜索或构建这样的层。如果找不到现成的实现,可以尝试将一个卷积层与LSTM层相结合,创建一个自定义的混合层来模拟WD_CNN_LSTM的功能。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 已采纳回答 5月6日
  • 创建了问题 5月6日