Least-squares¶
This subpackage contains modules for least-squares problems.
Classes:
-
GaussNewton
–Gauss-newton method.
-
SumOfSquares
–Sets loss to be the sum of squares of values returned by the closure.
GaussNewton ¶
Bases: torchzero.core.module.Module
Gauss-newton method.
To use this, the closure should return a vector of values to minimize sum of squares of.
Please add the backward
argument, it will always be False but it is required.
Gradients will be calculated via batched autograd within this module, you don't need to
implement the backward pass. Please see below for an example.
Note
This method requires ndim^2
memory, however, if it is used within tz.m.TrustCG
trust region,
the memory requirement is ndim*m
, where m
is number of values in the output.
Parameters:
-
reg
(float
, default:1e-08
) –regularization parameter. Defaults to 1e-8.
-
batched
(bool
, default:True
) –whether to use vmapping. Defaults to True.
Examples:
minimizing the rosenbrock function:
def rosenbrock(X):
x1, x2 = X
return torch.stack([(1 - x1), 100 * (x2 - x1**2)])
X = torch.tensor([-1.1, 2.5], requires_grad=True)
opt = tz.Modular([X], tz.m.GaussNewton(), tz.m.Backtracking())
# define the closure for line search
def closure(backward=True):
return rosenbrock(X)
# minimize
for iter in range(10):
loss = opt.step(closure)
print(f'{loss = }')
training a neural network with a matrix-free GN trust region:
X = torch.randn(64, 20)
y = torch.randn(64, 10)
model = nn.Sequential(nn.Linear(20, 64), nn.ELU(), nn.Linear(64, 10))
opt = tz.Modular(
model.parameters(),
tz.m.TrustCG(tz.m.GaussNewton()),
)
def closure(backward=True):
y_hat = model(X) # (64, 10)
return (y_hat - y).pow(2).mean(0) # (10, )
for i in range(100):
losses = opt.step(closure)
if i % 10 == 0:
print(f'{losses.mean() = }')
Source code in torchzero/modules/least_squares/gn.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
SumOfSquares ¶
Bases: torchzero.core.module.Module
Sets loss to be the sum of squares of values returned by the closure.
This is meant to be used to test least squares methods against ordinary minimization methods.
To use this, the closure should return a vector of values to minimize sum of squares of.
Please add the backward
argument, it will always be False but it is required.