keras-team/keras · error · NotImplementedError
`sample_weight` property method has not been implemented in
Error message
`sample_weight` property method has not been implemented in {}. What it means
The base Iterator class exposes sample_weight as an abstract property; no stock legacy iterator implements it, so accessing it always raises NotImplementedError unless a custom subclass overrides it. It is a hook for user-defined iterators that carry per-sample weights.
Source
Thrown at keras/src/legacy/preprocessing/image.py:390
def filepaths(self):
"""List of absolute paths to image files."""
raise NotImplementedError(
"`filepaths` property method has not "
"been implemented in {}.".format(type(self).__name__)
)
@property
def labels(self):
"""Class labels of every observation."""
raise NotImplementedError(
"`labels` property method has not been implemented in {}.".format(
type(self).__name__
)
)
@property
def sample_weight(self):
raise NotImplementedError(
"`sample_weight` property method has not "
"been implemented in {}.".format(type(self).__name__)
)
@keras_export("keras._legacy.preprocessing.image.DirectoryIterator")
class DirectoryIterator(BatchFromFilesMixin, Iterator):
"""Iterator capable of reading images from a directory on disk.
DEPRECATED.
"""
allowed_class_modes = {"categorical", "binary", "sparse", "input", None}
def __init__(
self,
directory,
image_data_generator,View on GitHub (pinned to 7a34a03db6)
Solutions
- In custom iterators, override sample_weight to return the weights array (or None)
- Pass sample_weight directly to model.fit(x, y, sample_weight=...) instead of reading it off the iterator
- Wrap access in try/except NotImplementedError with a None default
Example fix
# before
class MySeq(keras.utils.Sequence):
...
sw = it.sample_weight # NotImplementedError
# after
class MySeq(keras.utils.Sequence):
def __init__(self, w):
self._w = w
@property
def sample_weight(self):
return self._w Defensive patterns
Strategy: try-catch
Validate before calling
try:
sw = iterator.sample_weight
except NotImplementedError:
sw = None Type guard
def has_sample_weight(it):
try:
it.sample_weight
return True
except NotImplementedError:
return False Try / catch
try:
sw = iterator.sample_weight
except NotImplementedError:
sw = None
model.fit(iterator, sample_weight=sw) Prevention
- Pass sample_weight to fit() explicitly instead of reading it from the iterator
- Override sample_weight in custom Sequence subclasses
When it happens
Trigger: Reading iterator.sample_weight on any stock iterator (DirectoryIterator, NumpyArrayIterator), or failing to override it in a custom Iterator/Sequence subclass that supplies weights.
Common situations: Passing sample_weight to fit while using a custom Sequence/Iterator without implementing the property; generic introspection code enumerating iterator attributes.
Related errors
- `filepaths` property method has not been implemented in {}.
- `labels` property method has not been implemented in {}.
- The TFSMLayer is only currently supported with the TensorFlo
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/74c1563b36806096.
Report an issue: GitHub.