(self, vec)
return r
Performs two-loop recursion on given vector to obtain Hv.

        Inputs:
            vec (tensor): 1-D tensor to apply two-loop recursion to

        Output:
            r (tensor): matrix-vector product Hv
Performs two-loop recursion on given vector to obtain Hv.
Performs two - loop recursion on given vector to obtain Hv .
def two_loop_recursion(self, vec):
        """
        Performs two-loop recursion on given vector to obtain Hv.

        Inputs:
            vec (tensor): 1-D tensor to apply two-loop recursion to

        Output:
            r (tensor): matrix-vector product Hv

        """

        group = self.param_groups[0]
        history_size = group['history_size']

        state = self.state['global_state']
        old_dirs = state.get('old_dirs')    # change in gradients
        old_stps = state.get('old_stps')    # change in iterates
        H_diag = state.get('H_diag')

        # compute the product of the inverse Hessian approximation and the gradient
        num_old = len(old_dirs)

        if 'rho' not in state:
            state['rho'] = [None] * history_size
            state['alpha'] = [None] * history_size
        rho = state['rho']
        alpha = state['alpha']

        for i in range(num_old):
            rho[i] = 1. / old_stps[i].dot(old_dirs[i])

        q = vec
        for i in range(num_old - 1, -1, -1):
            alpha[i] = old_dirs[i].dot(q) * rho[i]
            q.add_(-alpha[i], old_stps[i])

        # multiply by initial Hessian
        # r/d is the final direction
        r = torch.mul(q, H_diag)
        for i in range(num_old):
            beta = old_stps[i].dot(r) * rho[i]
            r.add_(alpha[i] - beta, old_dirs[i])

        return r
def two_loop_recursion ( self , vec ) : group = self . param_groups [ 0 ] history_size = group [ 'history_size' ] state = self . state [ 'global_state' ] old_dirs = state . get ( 'old_dirs' ) # change in gradients old_stps = state . get ( 'old_stps' ) # change in iterates H_diag = state . get ( 'H_diag' ) # compute the product of the inverse Hessian approximation and the gradient num_old = len ( old_dirs ) if 'rho' not in state : state [ 'rho' ] = [ None ] * history_size state [ 'alpha' ] = [ None ] * history_size rho = state [ 'rho' ] alpha = state [ 'alpha' ] for i in range ( num_old ) : rho [ i ] = 1. / old_stps [ i ] . dot ( old_dirs [ i ] ) q = vec for i in range ( num_old - 1 , - 1 , - 1 ) : alpha [ i ] = old_dirs [ i ] . dot ( q ) * rho [ i ] q . add_ ( - alpha [ i ] , old_stps [ i ] ) # multiply by initial Hessian # r/d is the final direction r = torch . mul ( q , H_diag ) for i in range ( num_old ) : beta = old_stps [ i ] . dot ( r ) * rho [ i ] r . add_ ( alpha [ i ] - beta , old_dirs [ i ] ) return r