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

模型输入形状不匹配求助:预期(9,)却得到(1,)附数据集

Hey there, let's break down this input shape mismatch issue and fix it step by step!

问题根源分析

First, let's look at the error you're hitting:

ValueError: 检查输入时出错:预期dense_1_input的形状为(9,),但得到形状为(1,)的数组。

This means your model's input layer expects samples with 9 feature dimensions, but you're passing in arrays that only have 1 feature. There's a clear mismatch between what the model is configured to accept and what you're feeding it.

Looking at your dataset columns: step, pos_x, pos_y, vel_x, vel_y, ship_lander_angle, ship_lander_angular_vel, leg_1_ground_contact, leg_2_ground_contact, action — we can see there are 9 potential feature columns (depending on whether you include step or exclude action as the target variable). The core issue is either you're not selecting all required features, or formatting the input data incorrectly.

Common Causes & Fixes

1. You're selecting the wrong input feature columns

Chances are you're only extracting a single column (like just pos_x) as your input data X, instead of all 9 required feature columns.

Fix:
First, clarify which columns are features and which is your target. Assuming action is your target variable, double-check if your model expects 9 features (including step) or 8 (excluding step). Then extract the correct columns:

import pandas as pd

# Load your dataset
df = pd.read_csv("your_dataset_file.csv")

# If model expects 9 features (including step)
X = df[["step", "pos_x", "pos_y", "vel_x", "vel_y", "ship_lander_angle", "ship_lander_angular_vel", "leg_1_ground_contact", "leg_2_ground_contact"]].values
# Target variable is action
y = df["action"].values

# Verify shape - should be (number_of_samples, 9)
print(X.shape)

2. Incorrect shape for single-sample prediction

If you're testing the model with a single sample, passing a 1D array (like df.iloc[0, 0]) will have a shape of (1,), but your model expects a 2D array with a batch dimension: (1, 9).

Fix:
Reshape your single sample to add the batch dimension:

# Extract a single sample (assuming first 9 columns are features)
single_sample = df.iloc[0, :-1].values
# Reshape to (1, 9) - the -1 lets numpy calculate the remaining dimension automatically
single_sample = single_sample.reshape(1, -1)

# Now predict with the correctly shaped sample
model.predict(single_sample)

3. Accidental dimension compression during preprocessing

If you're doing preprocessing (like scaling/normalization), you might have used functions like flatten() or incorrect axis parameters that squeezed your feature dimensions down to 1.

Fix:
Check the shape of your preprocessed input data:

print(X.shape)

It should output something like (N, 9) where N is your number of samples. If it's (N,) or (9, N), adjust the shape accordingly:

# If shape is (9, N), transpose it to (N, 9)
X = X.T

# If shape is (N,), you definitely picked the wrong columns - go back to fix #1

4. Mismatch between model input layer and feature count

If your actual number of features is 8 (e.g., you're excluding step), but your model's input layer is defined for 9 features, this error will pop up.

Fix:
Adjust your model's input layer to match your actual feature count:

from tensorflow.keras.layers import Input

# If you have 8 features instead of 9
input_layer = Input(shape=(8,))
# Build the rest of your model as usual
Quick Validation Steps

To quickly narrow down the issue:

  • Print your input data shape: print(X.shape) — should be (N, 9)
  • Print your model's input shape: print(model.input_shape) — should be (None, 9) (the None is for variable batch size)
  • If testing with a single sample, print its shape: print(single_sample.shape) — must be (1, 9)

Following these steps should help you resolve the shape mismatch quickly.

内容的提问来源于stack exchange,提问作者mishi ahmad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:26:22