def kfac(self, mc_samples=1):
        with backpack(new_ext.KFAC(mc_samples=mc_samples)):
            _, _, loss = self.problem.forward_pass()
            loss.backward()
            kfac = [p.kfac for p in self.problem.model.parameters()]

        return kfac
示例#2
0
def test_interface_kfac_conv():
    interface_test(new_ext.KFAC(), use_conv=True)
示例#3
0
def test_interface_kfac():
    interface_test(new_ext.KFAC())
示例#4
0
# %%
# We can now evaluate the loss and do a backward pass with Backpack
# -----------------------------------------------------------------

loss = lossfunc(model(X), y)

with backpack(
    extensions.BatchGrad(),
    extensions.Variance(),
    extensions.SumGradSquared(),
    extensions.BatchL2Grad(),
    extensions.DiagGGNMC(mc_samples=1),
    extensions.DiagGGNExact(),
    extensions.DiagHessian(),
    extensions.KFAC(mc_samples=1),
    extensions.KFLR(),
    extensions.KFRA(),
):
    loss.backward()

# %%
# And here are the results
# -----------------------------------------------------------------

for name, param in model.named_parameters():
    print(name)
    print(".grad.shape:             ", param.grad.shape)
    print(".grad_batch.shape:       ", param.grad_batch.shape)
    print(".variance.shape:         ", param.variance.shape)
    print(".sum_grad_squared.shape: ", param.sum_grad_squared.shape)
示例#5
0
from torch.nn import CrossEntropyLoss, Flatten, Linear, Sequential

from backpack import backpack, extend, extensions
from backpack.utils.examples import load_mnist_data

B = 4
X, y = load_mnist_data(B)

print("# Gradient with PyTorch, KFAC approximation with BackPACK | B =", B)

model = Sequential(
    Flatten(),
    Linear(784, 10),
)
lossfunc = CrossEntropyLoss()

model = extend(model)
lossfunc = extend(lossfunc)

loss = lossfunc(model(X), y)

# number of MC samples is optional, defaults to 1
with backpack(extensions.KFAC(mc_samples=1)):
    loss.backward()

for name, param in model.named_parameters():
    print(name)
    print(".grad.shape:             ", param.grad.shape)
    print(".kfac (shapes):          ", [kfac.shape for kfac in param.kfac])
示例#6
0
 def kfac(self, mc_samples: int = 1) -> List[List[Tensor]]:  # noqa:D102
     with backpack(new_ext.KFAC(mc_samples=mc_samples)):
         _, _, loss = self.problem.forward_pass()
         loss.backward()
     return self.problem.collect_data("kfac")