def get_printer(msg):
    """
    returns a printer function, that prints information about a tensor's gradient
    Used by register_hook in the backward pass.
    :param msg:
    :return: printer function
    """
    def printer(tensor):
        if tensor.nelement == 1:
            print("{} {}".format(msg, tensor))
        else:
            print("{} shape: {}"
                  "max: {} min: {}"
                  "mean: {}"
                  .format(msg, tensor.shape, tensor.max(), tensor.min(), tensor.mean()))
    return printer