{"record":{"id":"74c1563b36806096","repo":"keras-team/keras","slug":"sample-weight-property-method-has-not-been-imple","errorCode":null,"errorMessage":"`sample_weight` property method has not been implemented in {}.","messagePattern":"`sample_weight` property method has not been implemented in (.+?)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":390,"sourceCode":"    def filepaths(self):\n        \"\"\"List of absolute paths to image files.\"\"\"\n        raise NotImplementedError(\n            \"`filepaths` property method has not \"\n            \"been implemented in {}.\".format(type(self).__name__)\n        )\n\n    @property\n    def labels(self):\n        \"\"\"Class labels of every observation.\"\"\"\n        raise NotImplementedError(\n            \"`labels` property method has not been implemented in {}.\".format(\n                type(self).__name__\n            )\n        )\n\n    @property\n    def sample_weight(self):\n        raise NotImplementedError(\n            \"`sample_weight` property method has not \"\n            \"been implemented in {}.\".format(type(self).__name__)\n        )\n\n\n@keras_export(\"keras._legacy.preprocessing.image.DirectoryIterator\")\nclass DirectoryIterator(BatchFromFilesMixin, Iterator):\n    \"\"\"Iterator capable of reading images from a directory on disk.\n\n    DEPRECATED.\n    \"\"\"\n\n    allowed_class_modes = {\"categorical\", \"binary\", \"sparse\", \"input\", None}\n\n    def __init__(\n        self,\n        directory,\n        image_data_generator,","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L372-L408","documentation":"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.","triggerScenarios":"Reading iterator.sample_weight on any stock iterator (DirectoryIterator, NumpyArrayIterator), or failing to override it in a custom Iterator/Sequence subclass that supplies weights.","commonSituations":"Passing sample_weight to fit while using a custom Sequence/Iterator without implementing the property; generic introspection code enumerating iterator attributes.","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"],"exampleFix":"# before\nclass MySeq(keras.utils.Sequence):\n    ...\nsw = it.sample_weight  # NotImplementedError\n\n# after\nclass MySeq(keras.utils.Sequence):\n    def __init__(self, w):\n        self._w = w\n    @property\n    def sample_weight(self):\n        return self._w","handlingStrategy":"try-catch","validationCode":"try:\n    sw = iterator.sample_weight\nexcept NotImplementedError:\n    sw = None","typeGuard":"def has_sample_weight(it):\n    try:\n        it.sample_weight\n        return True\n    except NotImplementedError:\n        return False","tryCatchPattern":"try:\n    sw = iterator.sample_weight\nexcept NotImplementedError:\n    sw = None\nmodel.fit(iterator, sample_weight=sw)","preventionTips":["Pass sample_weight to fit() explicitly instead of reading it from the iterator","Override sample_weight in custom Sequence subclasses"],"tags":["keras","iterator","not-implemented","abstract-method","sample-weights"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}