Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply learning rate scaling to min_lr #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stes
Copy link

@stes stes commented Jun 2, 2021

I wanted to clarify how the linear learning rate scaling should be handled; this is sort of an edge case, but I think in order to obtain the correct learning rate when continuing the training from checkpoints on a different GPU count/with a different batch size, it would be required to also scale the min_lr?

Here's a visualization of the difference between the current & the proposed implementation (for a few example parameters, epochs = 42, niter_per_ep = 100, lr = 1e-4, lr_scale = 5):

image

It would be great to confirm which implementation produces the desired behavior; if it is the current version, I would propose to add a short inline comment to clarify. Thanks for looking into it!


To repro the plot and play around with other values, here's a small script:

import matplotlib.pyplot as plt
import numpy as np

def cosine_scheduler(base_value, final_value, epochs, niter_per_ep, warmup_epochs=0, start_warmup_value=0):
    warmup_schedule = np.array([])
    warmup_iters = warmup_epochs * niter_per_ep
    if warmup_epochs > 0:
        warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters)

    iters = np.arange(epochs * niter_per_ep - warmup_iters)
    schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters)))

    schedule = np.concatenate((warmup_schedule, schedule))
    assert len(schedule) == epochs * niter_per_ep
    return schedule

def plot_comparison(
        lr_scale = 5,
        lr = 1e-4,
        min_lr = 1e-6,
        epochs = 42,
        niter_per_ep = 100
    ):

    schedule_current = cosine_scheduler(
        lr * lr_scale,
        min_lr,
        epochs=epochs,
        niter_per_ep=niter_per_ep
    )

    schedule_fixed = cosine_scheduler(
        lr * lr_scale,
        min_lr * lr_scale,
        epochs=epochs,
        niter_per_ep=niter_per_ep
    )

    plt.figure(figsize=(2,2), dpi = 160)
    plt.plot(schedule_current, label = "current")
    plt.plot(schedule_fixed, label = "proposed")
    plt.yscale("log")
    plt.xlabel("# steps")
    plt.ylabel("learning rate")
    plt.legend()
    plt.show()
    
plot_comparison()

@facebook-github-bot
Copy link

Hi @stes!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 2, 2021
@mathildecaron31
Copy link
Contributor

Hi @stes

Thanks for raising this point. To be honest I don't really know what is the optimal choice about scaling the minimum learning rate... Have you run some models to see what gives the best performance ?

For my experiments I kept it fixed regardless of the batch size considered but I definitely agree that it is weird since we do scale the max lr. However I'd like to keep the fixed min lr for enabling perfect reproducibility with the runs and logs I provide in the repo. But I think it's worth adding an inline comment about that in the code as you suggest. I let you rebase and update the PR.

Anyway thanks again for contributing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants