You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LSTM外汇预测模型报错:维度解包不足(ValueError)

Fixing ValueError: not enough values to unpack (expected 3, got 2) in Your LSTM Forex Prediction Model

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.shape will 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. Use df.values or (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 specify dtype=np.float64 to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:27:15