Giter Site home page Giter Site logo

Comments (9)

Tony-Y avatar Tony-Y commented on June 9, 2024

Do you want one like an LR scheduler with warmup provided by Hugging Face?

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

I'm sorry for misunderstanding your request. You have to combine a global-step and epoch LR scheduler, but it could not be as easy as it sounds. I don't know such example codes, but I'll let you know if I find.

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

I made sample code: https://gist.github.com/Tony-Y/49d6cffa21e60095fdf9b1bec31cdbaa

    for batch_idx, (data, target) in enumerate(progressbar(train_loader)):
        ...
        extra_params["global_step"] += 1
        if extra_params["global_step"] <= extra_params["warmup_period"]:
            with warmup_scheduler.dampening():
                pass
        elif (extra_params["global_step"] - extra_params["warmup_period"]) % len(train_loader) == 0:
            lr_scheduler.step()

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

I revised the code: https://gist.github.com/Tony-Y/1aa2196ce161d8a4da90cf027ec0f260

New code:

class EpochSchedulerWithWarmup:
  def __init__(self, warmup_period, every_n_steps, steps_per_epoch, warmup_scheduler, lr_scheduler):
    self.global_step = 0
    self.warmup_period = warmup_period
    self.every_n_steps = every_n_steps
    self.steps_per_epoch = steps_per_epoch
    self.warmup_scheduler = warmup_scheduler
    self.lr_scheduler = lr_scheduler

  def step(self):
    self.global_step += 1
    if self.global_step <= self.warmup_period and self.global_step % self.every_n_steps == 0:
      with self.warmup_scheduler.dampening():
        pass
    elif (self.global_step - self.warmup_period) % self.steps_per_epoch == 0:
      self.lr_scheduler.step()

Usage:

lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[num_epochs//3], gamma=0.1)
warmup_scheduler = warmup.UntunedLinearWarmup(optimizer)
lr_scheduler_with_warmup = EpochSchedulerWithWarmup(
    warmup_period=2000, every_n_steps=1,
    steps_per_epoch=len(dataloader),
    warmup_scheduler=warmup_scheduler,
    lr_scheduler=lr_scheduler)

for epoch in range(1,num_epochs+1):
    for iter, batch in enumerate(dataloader):
        optimizer.zero_grad()
        loss = ...
        loss.backward()
        optimizer.step()
        lr_scheduler_with_warmup.step()

Does this code resolve your issue?

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

EpochSchedulerWithWarmup has a bug. Its step() should be:

  def step(self):
    self.global_step += 1
    if self.global_step <= self.warmup_period:
      if self.global_step % self.every_n_steps == 0:
        with self.warmup_scheduler.dampening():
          pass
    elif (self.global_step - self.warmup_period) % self.steps_per_epoch == 0:
      self.lr_scheduler.step()

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

@JohnHerry

I checked every_n_steps works well: https://gist.github.com/Tony-Y/6c79267cab84f3d0a2309f25a9123da4

MultiStepLRWithWarmup
MultiStepLRWithWarmup-ZoomUp

I think this issue was resolved. Reopen this issue if not.

from pytorch_warmup.

JohnHerry avatar JohnHerry commented on June 9, 2024

Thank you very much for the kindly help. I will have a try.

from pytorch_warmup.

JohnHerry avatar JohnHerry commented on June 9, 2024

Thank you very much for the kindly help. I will have a try.

Thank you for all the help๏ผŒ They are very effective! the training process is stable now. thanks

from pytorch_warmup.

Tony-Y avatar Tony-Y commented on June 9, 2024

@JohnHerry

I'm happy to hear that.

from pytorch_warmup.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.