这是我的代码,出错了,应该要怎么改正,把改正后的完整代码写出
import pandas as pd
from collections import deque
import numpy as np
from tensorflow.keras.callbacks import ModelCheckpoint
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, Conv1D, MaxPooling1D, Flatten
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)
scaler = StandardScaler()
sca_X = scaler.fit_transform(df.iloc[:, :-1])
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 = 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]
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 = 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)
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False, test_size=0.1)
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
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])
这是出错原因
C:\Users\JAY\AppData\Local\Temp\ipykernel_15564\498793550.py:14: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.
df = pd.read_csv('D:/LSTMdata.csv', index_col='Date', parse_dates=True, date_parser=parse_date)
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[3], line 81
79 model.add(Dense(1))
80 model.compile(optimizer='adam', loss='mse', metrics=['mape'])
---> 81 model.fit(X_train, y_train, batch_size=32, epochs=50, validation_data=(X_test, y_test), callbacks=[checkpoint])
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" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 32)