<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def grad_hook(module, name, writer, bins):
 """ Factory for grad_hook closures """

    def hook(grad):
        writer.add_histogram('{}/grad'.format(name.replace('.','/')),
                             grad.detach().cpu().numpy(),
                             module.global_step-1,
                             bins=bins)
    return hook