def monitor_module(module, summary_writer,
                   track_data=True,
                   track_grad=True,
                   track_update=True,
                   track_update_ratio=False, # this is usually unnecessary
                   bins=51):
    """ Allows for remote monitoring of a module's params and buffers.
    The following may be monitored:
      1. Forward Values - Histograms of the values for parameter and buffer tensors
      2. Gradient Values - Histograms of the gradients for parameter and buffer tensors
      3. Update Values - Histograms of the change in parameter and buffer tensor
           values from the last recorded forward pass
      4. Update Ratios - Histograms of the ratio of change in value for parameter
           and value tensors from the last iteration to the actual values.
           I.e., what is the relative size of the update.
           Generally we like to see values of about .001.
           See [cite Andrej Karpathy's babysitting dnn's blog post]
    """


    # The module will need additional information
    module.track_data = track_data
    module.track_grad = track_grad
    module.track_update = track_update
    module.track_update_ratio = track_update_ratio
    if not hasattr(module, 'global_step'):
        module.global_step = 0
    if not hasattr(module, 'is_monitoring'):
        module.is_monitoring = True
    if not hasattr(module, 'monitoring'):
        set_monitoring(module)
    if not hasattr(module, 'last_state_dict'):
        module.last_state_dict = dict()
    if not hasattr(module, 'var_hooks'):
        module.var_hooks = dict()
    if not hasattr(module, 'param_hooks'):
        module.param_hooks = dict()

    # All submodules need to have these
    for name, mod in module.named_modules():
        if not hasattr(mod, 'monitor'):
            set_monitor(mod)
        if not hasattr(mod, 'monitored_vars'):
            mod.monitored_vars = dict()

    module.monitoring(True)

    # remove previous grad hooks before handles go stale
    module.register_forward_pre_hook(remove_grad_hooks)

    # set forward hook that monitors forward activations and sets new grad hooks
    monitor_forward_and_backward = get_monitor_forward_and_backward(summary_writer, bins)
    module.register_forward_hook(monitor_forward_and_backward)
Allows for remote monitoring of a module's params and buffers.
    The following may be monitored:
      1. Forward Values - Histograms of the values for parameter and buffer tensors
      2. Gradient Values - Histograms of the gradients for parameter and buffer tensors
      3. Update Values - Histograms of the change in parameter and buffer tensor
           values from the last recorded forward pass
      4. Update Ratios - Histograms of the ratio of change in value for parameter
           and value tensors from the last iteration to the actual values.
           I.e., what is the relative size of the update.
           Generally we like to see values of about .001.
           See [cite Andrej Karpathy's babysitting dnn's blog post]