LSTM输入形状不匹配报错,需用30行数据集预测第31个值
Let's break down your error and fix this step by step. The core issue is a mismatch between the input shape you defined for your LSTM and the actual shape of your training data, plus a few other small bugs in the prediction and visualization code.
1. Root Cause of the Shape Mismatch
Your LSTM's input_shape is set to (X_train.shape[0], 5) which is completely wrong:
X_train.shape[0]is the number of training samples (20 in your case, since you have 30 rows and use 10 timesteps), not the number of timesteps.- You only have 1 feature (the
powcolumn), not 5.
The correct input shape for LSTM should be (number_of_timesteps, number_of_features), which for your data is (10, 1).
2. Step-by-Step Fixes
Let's go through each part of your code and correct the issues:
Fix 1: Correct the LSTM Input Shape
When defining your first LSTM layer, change the input_shape parameter to match your training data's timesteps and features:
regressior.add(LSTM(units=60, activation='relu', return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))
X_train.shape[1] gives the timesteps (10), X_train.shape[2] gives the number of features (1).
Fix 2: Fix the Prediction Code
Your current prediction code has multiple issues:
- You're using a single value
new = 62instead of the last 10 timesteps from your data (since your model is trained on 10 timesteps to predict the next value). scaler.transformrequires a 2D array, not a scalar.- You have a typo:
regressior.predict(input)should beregressior.predict(inputs).
Replace the prediction section with this:
# Get the last 10 values from the scaled training data to predict the 31st value new_data = data_training[-10:] # Shape (10,1) # Reshape to match the model's expected input shape (samples, timesteps, features) inputs = new_data.reshape(1, 10, 1) # Predict and inverse transform to get the actual power value y_pred_scaled = regressior.predict(inputs) y_pred = scaler.inverse_transform(y_pred_scaled) print(f"Predicted 31st power value: {y_pred[0][0]}")
Fix 3: Fix Visualization Code
You can't plot the 3D X_train directly. Instead, plot the original training data alongside the model's predictions on the training set (to see how well it fits):
# Get predictions on training data to visualize train_predict_scaled = regressior.predict(X_train) train_predict = scaler.inverse_transform(train_predict_scaled) y_train_actual = scaler.inverse_transform(y_train.reshape(-1,1)) plt.figure(figsize=(14,5)) plt.plot(y_train_actual, color='red', label='Real Power Data') plt.plot(train_predict, color='blue', label='Predicted Power Data') plt.title('Power Prediction (Training Set)') plt.xlabel('Time Step') plt.ylabel('Power') plt.legend() plt.show()
Fix 4: Optional: Adjust Batch Size (Since Dataset is Small)
Your dataset only has 20 training samples, so a batch size of 32 is larger than the number of samples. Reduce it to 8 or 16 to avoid warnings:
regressior.fit(X_train, y_train, epochs=50, batch_size=8)
3. Full Corrected Code
Here's the complete fixed code:
import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.preprocessing import MinMaxScaler from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, LSTM, Dropout # Load and preprocess data data_training = pd.read_csv('G:\\Agrima\\Obj_Rec\\power.csv') # Extract the pow column (assuming it's the 3rd column, index 2) data_training = data_training.iloc[:, 2:3].values scaler = MinMaxScaler() data_training = scaler.fit_transform(data_training) print(data_training[0:10]) # Prepare training sequences (10 timesteps to predict next value) X_train = [] y_train = [] for i in range(10, data_training.shape[0]): X_train.append(data_training[i-10:i]) y_train.append(data_training[i, 0]) X_train, y_train = np.array(X_train), np.array(y_train) print(f"X_train shape: {X_train.shape}") # Should be (20, 10, 1) print(f"y_train shape: {y_train.shape}") # Should be (20,) # Build LSTM model with correct input shape regressor = Sequential() regressor.add(LSTM(units=60, activation='relu', return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2]))) regressor.add(Dropout(0.2)) regressor.add(LSTM(units=60, activation='relu', return_sequences=True)) regressor.add(Dropout(0.2)) regressor.add(LSTM(units=80, activation='relu', return_sequences=True)) regressor.add(Dropout(0.2)) regressor.add(LSTM(units=120, activation='relu')) regressor.add(Dropout(0.2)) regressor.add(Dense(units=1)) print(regressor.summary()) regressor.compile(optimizer='adam', loss='mean_squared_error') # Train with appropriate batch size regressor.fit(X_train, y_train, epochs=50, batch_size=8) regressor.save_weights('my_model_weights.h5') # Predict the 31st value new_data = data_training[-10:] inputs = new_data.reshape(1, 10, 1) y_pred_scaled = regressor.predict(inputs) y_pred = scaler.inverse_transform(y_pred_scaled) print(f"Predicted 31st Power Value: {y_pred[0][0]}") # Visualize training predictions train_predict_scaled = regressor.predict(X_train) train_predict = scaler.inverse_transform(train_predict_scaled) y_train_actual = scaler.inverse_transform(y_train.reshape(-1,1)) plt.figure(figsize=(14,5)) plt.plot(y_train_actual, color='red', label='Real Power Data') plt.plot(train_predict, color='blue', label='Predicted Power Data') plt.title('Power Prediction (Training Set)') plt.xlabel('Time Step') plt.ylabel('Power') plt.legend() plt.show()
Key Notes
- Since your dataset is very small (only 30 rows), the model might overfit easily. You might want to reduce the number of LSTM layers or units, or add more regularization.
- Always double-check the shape of your data at each step using
print(X_train.shape)to catch shape mismatches early.
内容的提问来源于stack exchange,提问作者Sai Krishnadas

