nnrl/optim/kfac.py
def save_grad_out(self, mod, _, grad_outputs):
        """Saves grad on output of layer to compute covariance.

        Note: grads are already properly weighted when the final loss function uses
        .mean() to aggregate element-wise losses. Since this is always the case when
        computing the entropy (average negative log-likelihood), we don't weight them
        here.
        """
        grad_outputs = grad_outputs[0].detach()
        if isinstance(mod, nn.Linear):
            grad_outputs = grad_outputs.reshape(-1, grad_outputs.shape[-1])
        elif isinstance(mod, nn.Conv2d):
            grad_outputs = grad_outputs.reshape(-1, *grad_outputs.shape[-3:])
        self.state[mod.weight]["gy"] = grad_outputs
Saves grad on output of layer to compute covariance.

        Note: grads are already properly weighted when the final loss function uses
        .mean() to aggregate element-wise losses. Since this is always the case when
        computing the entropy (average negative log-likelihood), we don't weight them
        here.
        
Saves grad on output of layer to compute covariance.
Note: grads are already properly weighted when the final loss function uses
.mean() to aggregate element-wise losses. Since this is always the case when
computing the entropy (average negative log-likelihood), we don't weight them
here.
Saves grad on output of layer to compute covariance . Note : grads are already properly weighted when the final loss function uses . mean () to aggregate element - wise losses . Since this is always the case when computing the entropy ( average negative log - likelihood ) we don ' t weight them here .
def save_grad_out(self, mod, _, grad_outputs):
        grad_outputs = grad_outputs[0].detach()
        if isinstance(mod, nn.Linear):
            grad_outputs = grad_outputs.reshape(-1, grad_outputs.shape[-1])
        elif isinstance(mod, nn.Conv2d):
            grad_outputs = grad_outputs.reshape(-1, *grad_outputs.shape[-3:])
        self.state[mod.weight]["gy"] = grad_outputs
def save_grad_out ( self , mod , _ , grad_outputs ) : grad_outputs = grad_outputs [ 0 ] . detach ( ) if isinstance ( mod , nn . Linear ) : grad_outputs = grad_outputs . reshape ( - 1 , grad_outputs . shape [ - 1 ] ) elif isinstance ( mod , nn . Conv2d ) : grad_outputs = grad_outputs . reshape ( - 1 , * grad_outputs . shape [ - 3 : ] ) self . state [ mod . weight ] [ "gy" ] = grad_outputs
Saves grad on output of layer to compute covariance.
Saves grad on output of layer to compute covariance .
"""Saves grad on output of layer to compute covariance.

        Note: grads are already properly weighted when the final loss function uses
        .mean() to aggregate element-wise losses. Since this is always the case when
        computing the entropy (average negative log-likelihood), we don't weight them
        here.
        """
self
mod
_
grad_outputs