m0_74420115 2024-05-06 21:26 采纳率: 71.9%
浏览 14
已结题

python修改代码

代码报错

import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Dropout, LSTM, LayerNormalization, TimeDistributed
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import MeanAbsolutePercentageError
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras import backend as K
 
def parse_date(date_string):
    return pd.Timestamp(date_string.replace('_', '-'))
 
def Stock_Price_WDCNNSeqData_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]
 
    # 将X转换为4维张量,以便与WD-CNN输入兼容
    X = np.reshape(X, (-1, mem_his_days, 1, len(sca_X[0])))
 
    # WD-CNN 部分
    def dilated_causal_conv(input, filters, kernel_size, rate):
        conv = tf.keras.layers.Conv1D(filters=filters, kernel_size=kernel_size, padding="causal", dilation_rate=rate)
        x = conv(input)
        return x
 
    def attention_layer(x):
        x = tf.expand_dims(x, axis=-1)
        w = tf.keras.layers.Dense(1)(x)
        w = tf.nn.softmax(w, axis=1)
        output = tf.reduce_sum(tf.multiply(x, w), axis=1)
        return output
 
    def WDCNN_block(inputs, filters, kernel_size, num_blocks):
        x = inputs
        for _ in range(num_blocks):
            x = dilated_causal_conv(x, filters, kernel_size, rate=1)
            x = LayerNormalization()(x)
            x = tf.nn.relu(x)
            x = dilated_causal_conv(x, filters, kernel_size, rate=2)
            x = LayerNormalization()(x)
            x = tf.nn.relu(x)
        return x
 
    # 增加WD-CNN块
    x = WDCNN_block(X, filters=the_units, kernel_size=3, num_blocks=2)
    x = attention_layer(x)
 
    # 原有LSTM部分
    x = LSTM(the_units, activation='relu', return_sequences=True)(x)
    x = Dropout(0.1)(x)
    for i in range(the_lstm_layers - 1):
        x = LSTM(the_units, activation='relu', return_sequences=True)(x)
        x = Dropout(0.1)(x)
 
    x = LSTM(the_units, activation='relu')(x)
    x = Dropout(0.1)
    for i in range(the_dense_layers):
        x = Dense(the_units, activation='relu')(x)
        x = Dropout(0.1)
 
    outputs = Dense(1)(x)
    model = Model(inputs=Input(shape=(mem_his_days, 1, len(sca_X[0]))), outputs=outputs)
 
    optimizer = Adam()
    model.compile(optimizer=optimizer,
                  loss='mse',
                  metrics=[MeanAbsolutePercentageError()])
    
    return model, X, y, X_lately
 
# ...保持原有的fitting循环和保存模型的部分不变...
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])

报错原因


D:\anaconda\Lib\site-packages\keras\src\layers\rnn\rnn.py:204: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(**kwargs)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 124
    122 from tensorflow.keras.layers import LSTM,Dense,Dropout
    123 model = Sequential()
--> 124 model.add(LSTM(the_units,input_shape=X.shape[1:],activation='relu',return_sequences=True))
    125 model.add(Dropout(0.1))
    126 for i in range(the_lstm_layers):

File D:\anaconda\Lib\site-packages\keras\src\models\sequential.py:120, in Sequential.add(self, layer, rebuild)
    118 self._layers.append(layer)
    119 if rebuild:
--> 120     self._maybe_rebuild()
    121 else:
    122     self.built = False

File D:\anaconda\Lib\site-packages\keras\src\models\sequential.py:139, in Sequential._maybe_rebuild(self)
    137 if isinstance(self._layers[0], InputLayer) and len(self._layers) > 1:
    138     input_shape = self._layers[0].batch_shape
--> 139     self.build(input_shape)

File D:\anaconda\Lib\site-packages\keras\src\layers\layer.py:223, in Layer.__new__.<locals>.build_wrapper(*args, **kwargs)
    220 @wraps(original_build_method)
    221 def build_wrapper(*args, **kwargs):
    222     with obj._open_name_scope():
--> 223         original_build_method(*args, **kwargs)
    224     # Record build config.
    225     signature = inspect.signature(original_build_method)

File D:\anaconda\Lib\site-packages\keras\src\models\sequential.py:183, in Sequential.build(self, input_shape)
    181 for layer in self._layers[1:]:
    182     try:
--> 183         x = layer(x)
    184     except NotImplementedError:
    185         # Can happen if shape inference is not implemented.
    186         # TODO: consider reverting inbound nodes on layers processed.
    187         return

File D:\anaconda\Lib\site-packages\keras\src\utils\traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File D:\anaconda\Lib\site-packages\keras\src\layers\input_spec.py:186, in assert_input_compatibility(input_spec, inputs, layer_name)
    184 if spec.ndim is not None and not spec.allow_last_axis_squeeze:
    185     if ndim != spec.ndim:
--> 186         raise ValueError(
    187             f'Input {input_index} of layer "{layer_name}" '
    188             "is incompatible with the layer: "
    189             f"expected ndim={spec.ndim}, found ndim={ndim}. "
    190             f"Full shape received: {shape}"
    191         )
    192 if spec.max_ndim is not None:
    193     if ndim is not None and ndim > spec.max_ndim:

ValueError: Input 0 of layer "lstm_1" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 5, 1, 5)

  • 写回答

4条回答 默认 最新

  • 笑面虎 2024-05-06 22:05
    关注

    以下内容由CHATGPT及笑面虎共同生成、有用望采纳:


    问题概括

    这个问题的目的是修改和优化一个使用Python编写的深度学习模型,该模型用于处理股票价格预测。模型结合了WD-CNN(Wavelet Dilated Convolutional Neural Network)和LSTM(Long Short-Term Memory)网络。

    问题出现原因

    1. 警告信息:模型在添加LSTM层时,由于使用了Sequential模型,不需要指定input_shape参数,这是由警告信息指出的。
    2. ValueError:在构建模型时,LSTM层期望的输入维度是3,但是实际提供的是4维的输入。这通常发生在输入数据的维度与模型期望的维度不匹配时。

    问题解决方案

    1. 移除input_shape参数:在Sequential模型中,第一个添加的层应该是一个Input层,它会自动推断输入的形状。
    2. 调整输入数据的维度:确保输入数据X的维度与LSTM层期望的维度相匹配。LSTM层期望的是一个3维的输入,形状为(batch_size, timesteps, input_features)

    提供代码

    以下是修改后的代码片段,特别是添加模型的部分:

    # ...之前的代码保持不变...
    
    # 修改前的添加LSTM层的方式(错误的)
    # model.add(LSTM(the_units, input_shape=X.shape[1:], activation='relu', return_sequences=True))
    
    # 修改后的添加LSTM层的方式(正确的)
    model.add(Input(shape=(None, X.shape[2])))
    lstm_layer = LSTM(the_units, activation='relu', return_sequences=True)
    model.add(lstm_layer)
    model.add(Dropout(0.1))
    
    # 确保后续的LSTM层接收到的输入是正确的
    for i in range(the_lstm_layers - 1):
        lstm_layer = LSTM(the_units, activation='relu', return_sequences=True)
        model.add(lstm_layer)
        model.add(Dropout(0.1))
    
    # 其余的模型构建代码保持不变...
    
    model.compile(optimizer='adam',
                  loss='mse',
                  metrics=['mape'])
    
    # ...保持原有的fitting循环和保存模型的部分不变...
    

    代码运行方式

    运行代码需要有Python环境,并且安装了必要的库,如pandas, tensorflow, sklearn等。代码可以在Jupyter Notebook、Python脚本或者任何支持Python的IDE中运行。

    代码预期运行结果

    预期中,修改后的代码将不再抛出ValueError异常,能够成功构建模型并开始训练。

    推荐相关链接

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

报告相同问题?

问题事件

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