summaryrefslogtreecommitdiffstats
path: root/training/lr.py
blob: 68e0f72139d5d7dbdaf3be98dbee232b1a0da50d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import math
from contextlib import _GeneratorContextManager, nullcontext
from typing import Callable, Any, Tuple, Union
from functools import partial

import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.optim.lr_scheduler import LambdaLR
from tqdm.auto import tqdm

from training.util import AverageMeter


def noop(*args, **kwards):
    pass


class LRFinder():
    def __init__(
        self,
        accelerator,
        model,
        optimizer,
        train_dataloader,
        val_dataloader,
        loss_fn: Union[Callable[[int, Any], Tuple[Any, Any, int]], Callable[[int, Any, bool], Tuple[Any, Any, int]]],
        on_train: Callable[[], _GeneratorContextManager] = nullcontext,
        on_clip: Callable[[], None] = noop,
        on_eval: Callable[[], _GeneratorContextManager] = nullcontext
    ):
        self.accelerator = accelerator
        self.model = model
        self.optimizer = optimizer
        self.train_dataloader = train_dataloader
        self.val_dataloader = val_dataloader
        self.loss_fn = loss_fn
        self.on_train = on_train
        self.on_clip = on_clip
        self.on_eval = on_eval

        # self.model_state = copy.deepcopy(model.state_dict())
        # self.optimizer_state = copy.deepcopy(optimizer.state_dict())

    def run(
        self,
        end_lr,
        skip_start: int = 10,
        skip_end: int = 5,
        num_epochs: int = 100,
        num_train_batches: int = 1,
        num_val_batches: int = math.inf,
        smooth_f: float = 0.05,
    ):
        best_loss = None
        best_acc = None

        lrs = []
        losses = []
        accs = []

        lr_scheduler = get_exponential_schedule(
            self.optimizer,
            end_lr,
            num_epochs * min(num_train_batches, len(self.train_dataloader))
        )

        steps = min(num_train_batches, len(self.train_dataloader))
        steps += min(num_val_batches, len(self.val_dataloader))
        steps *= num_epochs

        progress_bar = tqdm(
            range(steps),
            disable=not self.accelerator.is_local_main_process,
            dynamic_ncols=True
        )
        progress_bar.set_description("Epoch X / Y")

        for epoch in range(num_epochs):
            progress_bar.set_description(f"Epoch {epoch + 1} / {num_epochs}")

            avg_loss = AverageMeter()
            avg_acc = AverageMeter()

            self.model.train()

            with self.on_train():
                for step, batch in enumerate(self.train_dataloader):
                    if step >= num_train_batches:
                        break

                    with self.accelerator.accumulate(self.model):
                        loss, acc, bsz = self.loss_fn(step, batch)

                        self.accelerator.backward(loss)

                        if self.accelerator.sync_gradients:
                            self.on_clip()

                        self.optimizer.step()
                        lr_scheduler.step()
                        self.optimizer.zero_grad(set_to_none=True)

                    if self.accelerator.sync_gradients:
                        progress_bar.update(1)

            self.model.eval()

            with torch.inference_mode():
                with self.on_eval():
                    for step, batch in enumerate(self.val_dataloader):
                        if step >= num_val_batches:
                            break

                        loss, acc, bsz = self.loss_fn(step, batch, True)
                        avg_loss.update(loss.detach_(), bsz)
                        avg_acc.update(acc.detach_(), bsz)

                        progress_bar.update(1)

            loss = avg_loss.avg.item()
            acc = avg_acc.avg.item()

            if epoch == 0:
                best_loss = loss
                best_acc = acc
            else:
                if smooth_f > 0:
                    loss = smooth_f * loss + (1 - smooth_f) * losses[-1]
                    acc = smooth_f * acc + (1 - smooth_f) * accs[-1]
                if loss < best_loss:
                    best_loss = loss
                if acc > best_acc:
                    best_acc = acc

            lr = lr_scheduler.get_last_lr()[0]

            lrs.append(lr)
            losses.append(loss)
            accs.append(acc)

            self.accelerator.log({
                "loss": loss,
                "acc": acc,
                "lr": lr,
            }, step=epoch)

            progress_bar.set_postfix({
                "loss": loss,
                "loss/best": best_loss,
                "acc": acc,
                "acc/best": best_acc,
                "lr": lr,
            })

            # self.model.load_state_dict(self.model_state)
            # self.optimizer.load_state_dict(self.optimizer_state)

        fig, ax_loss = plt.subplots()
        ax_acc = ax_loss.twinx()

        ax_loss.plot(lrs, losses, color='red')
        ax_loss.set_xscale("log")
        ax_loss.set_xlabel(f"Learning rate")
        ax_loss.set_ylabel("Loss")

        ax_acc.plot(lrs, accs, color='blue')
        ax_acc.set_xscale("log")
        ax_acc.set_ylabel("Accuracy")

        print("LR suggestion: steepest gradient")
        min_grad_idx = None

        if skip_end == 0:
            lrs = lrs[skip_start:]
            losses = losses[skip_start:]
            accs = accs[skip_start:]
        else:
            lrs = lrs[skip_start:-skip_end]
            losses = losses[skip_start:-skip_end]
            accs = accs[skip_start:-skip_end]

        try:
            min_grad_idx = np.gradient(np.array(losses)).argmin()
        except ValueError:
            print(
                "Failed to compute the gradients, there might not be enough points."
            )

        try:
            max_val_idx = np.array(accs).argmax()
        except ValueError:
            print(
                "Failed to compute the gradients, there might not be enough points."
            )

        if min_grad_idx is not None:
            print("Suggested LR (loss): {:.2E}".format(lrs[min_grad_idx]))
            ax_loss.scatter(
                lrs[min_grad_idx],
                losses[min_grad_idx],
                s=75,
                marker="o",
                color="red",
                zorder=3,
                label="steepest gradient",
            )
            ax_loss.legend()

        if max_val_idx is not None:
            print("Suggested LR (acc): {:.2E}".format(lrs[max_val_idx]))
            ax_acc.scatter(
                lrs[max_val_idx],
                accs[max_val_idx],
                s=75,
                marker="o",
                color="blue",
                zorder=3,
                label="maximum",
            )
            ax_acc.legend()


def get_exponential_schedule(optimizer, end_lr: float, num_epochs: int, last_epoch: int = -1):
    def lr_lambda(base_lr: float, current_epoch: int):
        return (end_lr / base_lr) ** (current_epoch / num_epochs)

    lr_lambdas = [partial(lr_lambda, group["lr"]) for group in optimizer.param_groups]

    return LambdaLR(optimizer, lr_lambdas, last_epoch)