TensorFlow维度不匹配InvalidArgumentError报错排查求助
Hey there! Let's break down this dimension mismatch error you're hitting—it's a super common gotcha when starting out with time series forecasting in TensorFlow, so don't worry too much. The core issue here is your model's output shape ([?,72]) doesn't match your training labels' shape ([?,100]), which causes the subtraction operation (used to calculate loss) to fail.
Here are the key steps to fix this:
Check your model's final layer output size
In multi-step forecasting, the lastDenselayer of your model needs to output exactly the same number of values as your target labels. Right now, your model is producing 72 values per sample, but your labels have 100. Head over to where you definemulti_step_modeland look for the finalDenselayer—adjust itsunitsparameter to match the size of your target labels (100 in this case). For example, if you hadDense(72), change it toDense(100).Verify your label generation logic
Double-check how you're creatingtrain_data_multiandval_data_multi. Make sure the number of future time steps you're using to generate labels matches the number of steps your model is supposed to predict. For example, if you're trying to predict 100 future steps, your data pipeline should be slicing exactly 100 steps for the labels—not 72. If you mixed up these numbers anywhere, that's the root cause.Debug shape mismatches with quick prints
Before runningmodel.fit(), add a few lines to confirm the shapes of your data and model output:# Check training data shapes for x, y in train_data_multi.take(1): print(f"Input batch shape: {x.shape}") print(f"Label batch shape: {y.shape}") # Check model output shape print(f"Model output shape: {multi_step_model(x).shape}")This will show you exactly where the mismatch is happening—either your model is outputting the wrong number of values, or your labels are the wrong size.
Once you align the model's output dimension with your label dimension, that Sub operation error should disappear.
内容的提问来源于stack exchange,提问作者notacorn

