使用Keras的多分类神经网络报错求助:AttributeError问题
I’ve run into this exact issue before when working with Keras image generators—let’s break down what’s going wrong and how to fix it.
What’s Causing the Error?
This error pops up because Keras is trying to treat your DirectoryIterator (the output from ImageDataGenerator.flow_from_directory()) like a standard NumPy array, which it’s not. The most common culprit is:
- You’re passing an extra
yparameter tomodel.fit(): When you useflow_from_directory(), the iterator already returns batches of(features, labels)pairs. If you manually pass a separateyargument (or include labels invalidation_data), Keras gets confused and tries to check the dimension (ndim) of the iterator as if it were a feature array—something iterators don’t have.
From your traceback, it looks like the issue is tied to how you’re passing inputs to NN.fit(), likely mixing generator inputs with explicit label arrays.
How to Fix It
Here’s the step-by-step fix:
1. Remove Explicit Label Parameters
When using DirectoryIterator for training/validation, you don’t need to pass separate y values or label arrays in validation_data. Just pass the iterator directly.
Wrong Code Example:
# This causes the error—don't pass y or labeled validation_data NN.fit(train_set, y=train_labels, validation_data=(validation_set, val_labels), steps_per_epoch=(train_samples / batch_size), validation_steps=(validation_samples / batch_size))
Correct Code Example:
# Pass only the DirectoryIterators, no extra labels NN.fit(train_set, validation_data=validation_set, steps_per_epoch=train_samples // batch_size, # Use integer division validation_steps=validation_samples // batch_size)
2. Use Integer Division for Step Counts
Notice I switched from / to // for steps_per_epoch and validation_steps. Keras expects integer values here—using floating-point division can lead to warnings or unexpected behavior down the line.
3. Double-Check Your Generator Setup
Make sure your train_set and validation_set are properly initialized with flow_from_directory(), especially for multi-class classification:
from keras.preprocessing.image import ImageDataGenerator # Initialize data generator for preprocessing train_datagen = ImageDataGenerator(rescale=1./255) val_datagen = ImageDataGenerator(rescale=1./255) # Create training set iterator train_set = train_datagen.flow_from_directory( 'path/to/training_images', target_size=(224, 224), # Match your model's input size batch_size=32, class_mode='categorical' # Use 'categorical' for multi-class ) # Create validation set iterator validation_set = val_datagen.flow_from_directory( 'path/to/validation_images', target_size=(224, 224), batch_size=32, class_mode='categorical' )
Why This Works
Keras handles DirectoryIterator inputs differently than NumPy arrays: it iterates over the generator to fetch each batch of data (and their corresponding labels) automatically. When you pass extra labels, Keras misinterprets the iterator as a standalone feature array and tries to access array-specific attributes like ndim, which doesn’t exist on iterators.
内容的提问来源于stack exchange,提问作者Cameron Blake




