LSTM外汇预测模型报错:维度解包不足(ValueError)
Let's break down what's going wrong here and fix it step by step.
The Root Cause
Your error happens because you messed up the slicing of your data_processed array when creating the training set. Let's look at the problematic lines:
train = data_processed[:, int(val_split), :] val = data_processed[int(val_split):, :]
Your data_processed is a 3D array with shape (total_samples, sequence_length, num_features) — this is the correct structure for LSTM input (samples, timesteps, features).
But when you use [:, int(val_split), :], you're extracting the val_split-th timestep from all samples, which collapses the array to 2D: (number_of_samples, num_features). That's why your print output shows Training data: (891, 50) — only two dimensions — but you're trying to unpack this into three variables (train_samples, train_nx, train_ny = train.shape), hence the ValueError.
The Fix
Change the training data slicing line to select all timesteps for the first portion of the data:
train = data_processed[:int(val_split), :, :]
This will keep train as a 3D array with shape (training_samples, sequence_length, num_features), matching the structure of your validation set. After this fix, when you run the code:
train.shapewill be something like(712, 822, 50)(since 891 * 0.8 ≈ 712)- Unpacking into three variables will work without errors
Extra Recommendations to Avoid Future Issues
- Stop using
df.as_matrix(): This method is deprecated in pandas. Usedf.valuesor (preferably for newer pandas versions)df.to_numpy()instead. - Fix the dtype parameter: You set
dtype='object'when loading your CSV, but your data is numerical. This will cause problems with scaling and model training. Remove this parameter or specifydtype=np.float64to let pandas handle numerical data correctly. - Adjust batch size: Setting
batch_size = df.__len__()means you're using the entire dataset as one batch (batch gradient descent), which is inefficient and can lead to slow convergence. Try reasonable values like 32, 64, or 128 instead.
内容的提问来源于stack exchange,提问作者Sayed Gouda

