m0_74420115 2024-05-07 17:45 采纳率: 71.9%
浏览 3

WD_CNN_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]
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-07 17:45
    关注

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

    在给定的代码基础上,将传统的LSTM层替换为WD_CNN_LSTM(Weighted Dilated Convolutional Long Short-Term Memory)层,以寻找最优模型。WD_CNN_LSTM是一种结合了卷积神经网络(CNN)和LSTM特性的新型网络结构,可以更好地捕捉时间序列数据中的局部特征。

    首先,你需要安装一个包含WD_CNN_LSTM层的库,比如tf-lstm-cnn-layer。如果没有安装,可以使用pip进行安装:

    pip install git+https://github.com/tensorflow/models.git@master//official/nlp/layers/wd_cnn_lstm_layer.py
    

    接下来,在导入模块时,引入WD_CNN_LSTM层,并在模型构建部分使用它替代普通的LSTM层。下面是修改后的代码:

    import pandas as pd
    
    # ... (保持其他部分不变)
    
    # 导入WD_CNN_LSTM层
    from official.nlp.layers.wd_cnn_lstm_layer import WD_CNN_LSTMCell
    
    # ... (保持其他部分不变)
    
    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}wdcnnlstm{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)
    
                    # ... (保持其他部分不变)
    
                    model = Sequential()
                    model.add(WD_CNN_LSTMCell(units=the_units, input_shape=X.shape[1:], activation='relu', return_sequences=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', dropout_rate=0.1, recurrent_dropout_rate=0.1, dilation_rate=1))
                    model.add(Dropout(0.1))
    
                    # 使用WD_CNN_LSTMCell替换普通LSTM层
                    for i in range(the_lstm_layers - 1):
                        model.add(WD_CNN_LSTMCell(units=the_units, activation='relu', return_sequences=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', dropout_rate=0.1, recurrent_dropout_rate=0.1, dilation_rate=2 ** i))
                        model.add(Dropout(0.1))
    
                    # ... (保持其他部分不变)
    
                    # ... (保持其他部分不变)
    

    这段代码做了以下改动:

    1. 引入WD_CNN_LSTMCell模块。
    2. 将模型的第一个层替换为WD_CNN_LSTMCell,之后的LSTM层也替换为WD_CNN_LSTMCell,注意设置正确的dilation_rate参数以实现不同步长的卷积。
    3. 修改文件路径中的模型名称,以便区分WD_CNN_LSTM模型。

    现在,这个代码应该可以训练WD_CNN_LSTM模型并找到最优模型。记得运行之前确保已经安装了tf-lstm-cnn-layer库。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月7日