nnrl/optim/hessian_free.py
def hessian_vector_product(output, params, vector):
    """Computes the Hessian vector product w.r.t to a scalar loss.

    Args:
        output (Tensor): loss tensor w.r.t. which the Hessian will be computed.
        params (list): the parameters of the module, usually from a call to
            `module.parameters()`.
        vector (Tensor): The flattened vector to compute the Hessian product with.
            This must have the same total number of elements in `params`.
    """
    # pylint:disable=missing-docstring
    params = list(params)
    vecs, idx = [], 0
    for par in params:
        vecs += [vector[idx : idx + par.numel()].reshape_as(par)]
        idx += par.numel()
    grads = grad(output, params, allow_unused=True, create_graph=True)
    hvp = grad([g for g in grads if g is not None], params, vecs, allow_unused=True)
    zeros = torch.zeros
    return torch.cat(
        [(zeros(p.numel()) if v is None else v.flatten()) for v, p in zip(hvp, params)]
    )
Computes the Hessian vector product w.r.t to a scalar loss.

    Args:
        output (Tensor): loss tensor w.r.t. which the Hessian will be computed.
        params (list): the parameters of the module, usually from a call to
            `module.parameters()`.
        vector (Tensor): The flattened vector to compute the Hessian product with.
            This must have the same total number of elements in `params`.
    
Computes the Hessian vector product w.r.t to a scalar loss.
Computes the Hessian vector product w . r . t to a scalar loss .
def hessian_vector_product(output, params, vector):
    params = list(params)
    vecs, idx = [], 0
    for par in params:
        vecs += [vector[idx : idx + par.numel()].reshape_as(par)]
        idx += par.numel()
    grads = grad(output, params, allow_unused=True, create_graph=True)
    hvp = grad([g for g in grads if g is not None], params, vecs, allow_unused=True)
    zeros = torch.zeros
    return torch.cat(
        [(zeros(p.numel()) if v is None else v.flatten()) for v, p in zip(hvp, params)]
    )
def hessian_vector_product ( output , params , vector ) : params = list ( params ) vecs , idx = [ ] , 0 for par in params : vecs += [ vector [ idx : idx + par . numel ( ) ] . reshape_as ( par ) ] idx += par . numel ( ) grads = grad ( output , params , allow_unused = True , create_graph = True ) hvp = grad ( [ g for g in grads if g is not None ] , params , vecs , allow_unused = True ) zeros = torch . zeros return torch . cat ( [ ( zeros ( p . numel ( ) ) if v is None else v . flatten ( ) ) for v , p in zip ( hvp , params ) ] )
Computes the Hessian vector product w.r.t to a scalar loss.
Computes the Hessian vector product w . r . t to a scalar loss .
"""Computes the Hessian vector product w.r.t to a scalar loss.

    Args:
        output (Tensor): loss tensor w.r.t. which the Hessian will be computed.
        params (list): the parameters of the module, usually from a call to
            `module.parameters()`.
        vector (Tensor): The flattened vector to compute the Hessian product with.
            This must have the same total number of elements in `params`.
    """ # pylint:disable=missing-docstring
output
loss tensor w.r.t. which the Hessian will be computed.
loss tensor w . r . t . which the Hessian will be computed .
params
the parameters of the module, usually from a call to
`module.parameters()`.
the parameters of the module usually from a call to ` module . parameters () ` .
vector
The flattened vector to compute the Hessian product with.
This must have the same total number of elements in `params`.
The flattened vector to compute the Hessian product with . This must have the same total number of elements in ` params ` .