{"record":{"id":"f9c7226949460895","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"object-has-no-attribute","errorCode":null,"errorMessage":"'{}' object has no attribute '{}'","messagePattern":"'(.+?)' object has no attribute '(.+?)'","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"pytorch_keypoint/HRNet/train_utils/distributed_utils.py","lineNumber":137,"sourceCode":"\nclass MetricLogger(object):\n    def __init__(self, delimiter=\"\\t\"):\n        self.meters = defaultdict(SmoothedValue)\n        self.delimiter = delimiter\n\n    def update(self, **kwargs):\n        for k, v in kwargs.items():\n            if isinstance(v, torch.Tensor):\n                v = v.item()\n            assert isinstance(v, (float, int))\n            self.meters[k].update(v)\n\n    def __getattr__(self, attr):\n        if attr in self.meters:\n            return self.meters[attr]\n        if attr in self.__dict__:\n            return self.__dict__[attr]\n        raise AttributeError(\"'{}' object has no attribute '{}'\".format(\n            type(self).__name__, attr))\n\n    def __str__(self):\n        loss_str = []\n        for name, meter in self.meters.items():\n            loss_str.append(\n                \"{}: {}\".format(name, str(meter))\n            )\n        return self.delimiter.join(loss_str)\n\n    def synchronize_between_processes(self):\n        for meter in self.meters.values():\n            meter.synchronize_between_processes()\n\n    def add_meter(self, name, meter):\n        self.meters[name] = meter\n\n    def log_every(self, iterable, print_freq, header=None):","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_keypoint/HRNet/train_utils/distributed_utils.py#L119-L155","documentation":"This is the standard Python __getattr__ fallback on a SmoothedValue-style AverageMeter container: attribute lookups first check self.meters, then instance __dict__, and raise AttributeError naming the object type and missing attribute. Any code doing e.g. metric_logger.loss when 'loss' was never logged triggers it.","triggerScenarios":"Accessing an attribute on the metric logger that was never added via meters: metric_logger.some_metric before update_loss/meter_dict registered it; typo like metet_logger.lr when only 'lr' was tracked; accessing on a freshly constructed logger with no meters.","commonSituations":"Typos in metric names between train and eval loops; reading a loss key that differs across model versions; copying tutorial code whose logged keys don't match this repo's; __getattr__ interplay with copy/pickle probing __deepcopy__/__getstate__ which the container lacks.","solutions":["Check the exact attribute name against the keys registered in meter_dict/update (print(metric_logger.meters.keys())).","Fix the typo or log the missing metric before accessing it.","Use getattr(metric_logger, name, default) for optional metrics.","If this fires for dunder attributes during copy/pickle, add explicit __copy__/__deepcopy__/__getstate__ methods to the class.","Return None instead of raising if optional semantics are desired (modify __getattr__ fallback)."],"exampleFix":"# before\nprint(metric_logger.losses)  # AttributeError: 'MetricLogger' object has no attribute 'losses'\n# after\nprint(metric_logger.loss)","handlingStrategy":"type-guard","validationCode":"available = set(metric_logger.meters.keys())\nassert \"loss\" in available, f\"'loss' never logged; available: {available}\"","typeGuard":"def has_metric(logger, name: str) -> bool:\n    return name in logger.meters or name in logger.__dict__","tryCatchPattern":"try:\n    value = metric_logger.loss\nexcept AttributeError as e:\n    logging.warning(\"Metric missing: %s — using default\", e)\n    value = float(\"nan\")","preventionTips":["Print metric_logger.meters.keys() once when wiring new metrics.","Centralize metric name constants shared between train/eval loops.","Use getattr(logger, name, default) for optional metrics."],"tags":["python","attributeerror","metrics","logging"],"backgroundTag":"attribute-not-found","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}