Keras Sequential模型报错:'Sequential' object has no attribute 'ndim'求助
Hey there! This error is definitely a code issue—no internal Keras bug here. Let's break down what's going wrong and fix it step by step.
What's Causing the Error?
Take a look at your training line:
f.fit(d,f,epochs=20,batch_size=10)
The fit() method expects the second argument to be your label/target data (the y values your model is supposed to predict), but you've passed your Sequential model instance f instead. Keras tries to treat this model as a tensor of training labels, but models don't have the ndim attribute (this is a property of numpy arrays/tensors that Keras uses to validate input shapes). That's exactly why you're hitting the AttributeError.
Step-by-Step Fixes
Correct the
fit()arguments
Replace the secondfwith your actual label data variable. For example, if your labels are stored in a variable namedy, your line should be:f.fit(d, y, epochs=20, batch_size=10)Verify your data shapes
Make sure your input data and labels match the model's expectations:- Your input
dshould have a shape of(number_of_samples, 9)(matching your first Dense layer'sinput_shape=(9,)). - Your label data
yshould have a shape of(number_of_samples, 1)(aligning with your final Dense layer's 1-neuron output for binary classification).
You can check shapes quickly with:
print("Input data shape:", d.shape) print("Label data shape:", y.shape)- Your input
Double-check imports (a quick sanity check)
Ensure you've imported the necessary components correctly at the top of your script:from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense
That should resolve the error and get your model training smoothly!
内容的提问来源于stack exchange,提问作者pirateking

