# Buggy code
def __call__(self, engine, logger, event_name):
    if not isinstance(logger, TensorboardLogger):
        raise RuntimeError(
            "Handler 'WeightsHistHandler' works only with TensorboardLogger"
        )

    global_step = engine.state.get_event_attrib_value(event_name)
    for name, p in self.model.named_parameters():
        name = name.replace(".", "/")
        logger.writer.add_histogram(
            tag="weights/{}".format(name),
            values=p.data.detach().cpu().numpy(),
            global_step=global_step,
        )


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-5ffffec84f3c> in <module>
65     print("Done.")
66
---> 67 example()

<ipython-input-3-5ffffec84f3c> in example()
62
63
---> 64     trainer.run(DataLoader(FakeDataset(), batch_size=16), max_epochs=5)
65     print("Done.")
66

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in run(self, data, max_epochs)
357         except BaseException as e:
358             self._logger.error("Engine run is terminating due to exception: %s.", str(e))
--> 359             self._handle_exception(e)
360
361         return self.state

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in _handle_exception(self, e)
322             self._fire_event(Events.EXCEPTION_RAISED, e)
323         else:
--> 324             raise e
325
326     def run(self, data, max_epochs=1):

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in run(self, data, max_epochs)
344                 self.state.epoch += 1
345                 self._fire_event(Events.EPOCH_STARTED)
--> 346                 hours, mins, secs = self._run_once_on_dataset()
347                 self._logger.info("Epoch[%s] Complete. Time taken: %02d:%02d:%02d", self.state.epoch, hours, mins, secs)
348                 if self.should_terminate:

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in _run_once_on_dataset(self)
311         except BaseException as e:
312             self._logger.error("Current run is terminating due to exception: %s.", str(e))
--> 313             self._handle_exception(e)
314
315         time_taken = time.time() - start_time

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in _handle_exception(self, e)
322             self._fire_event(Events.EXCEPTION_RAISED, e)
323         else:
--> 324             raise e
325
326     def run(self, data, max_epochs=1):

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in _run_once_on_dataset(self)
304                 self._fire_event(Events.ITERATION_STARTED)
305                 self.state.output = self._process_function(self, batch)
--> 306                 self._fire_event(Events.ITERATION_COMPLETED)
307                 if self.should_terminate or self.should_terminate_single_epoch:
308                     self.should_terminate_single_epoch = False

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/engine/engine.py in _fire_event(self, event_name, *event_args, **event_kwargs)
257             for func, args, kwargs in self._event_handlers[event_name]:
258                 kwargs.update(event_kwargs)
--> 259                 func(self, *(event_args + args), **kwargs)
260
261     def fire_event(self, event_name):

/anaconda3/envs/ignite/lib/python3.7/site-packages/pytorch_ignite-0.2.0-py3.7.egg/ignite/contrib/handlers/tensorboard_logger.py in __call__(self, engine, logger, event_name)
219             name = name.replace('.', '/')
220             logger.writer.add_scalar("grads_{}/{}".format(self.reduction.__name__, name),
--> 221                                      self.reduction(p.grad),
222                                      global_step)
223

/anaconda3/envs/ignite/lib/python3.7/site-packages/torch/functional.py in norm(input, p, dim, keepdim, out, dtype)
676         (tensor(3.7417), tensor(11.2250))
677     """
--> 678     ndim = input.dim()
679
680     # catch default case

AttributeError: 'NoneType' object has no attribute 'dim'

# Fixed code
def __call__(self, engine, logger, event_name):
    if not isinstance(logger, TensorboardLogger):
        raise RuntimeError(
            "Handler 'WeightsHistHandler' works only with TensorboardLogger"
        )

    global_step = engine.state.get_event_attrib_value(event_name)
    for name, p in self.model.named_parameters():
        if p.grad is None:
            continue

        name = name.replace(".", "/")
        logger.writer.add_histogram(
            tag="weights/{}".format(name),
            values=p.data.detach().cpu().numpy(),
            global_step=global_step,
        )