gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
is this a correct implementation of the ReduceLROnPlateau scheduler? Does the scaler…
- User
is this a correct implementation of the ReduceLROnPlateau scheduler? Does the scaler require special attention?
opt = optim.SGD(model.parameters(), lr=args_lr_max, weight_decay=args_wd, momentum=0.9) criterion = nn.CrossEntropyLoss() scaler = torch.cuda.amp.GradScaler() lr_schedule = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, 'min', patience=2) for epoch in range(args_epochs): start = time.time() train_loss, n = 0, 0 for i in range(total_train_batches): X, y = get_batch(enwik8_train_section, batch_size, input_chunk_size) model.train() X, y = X.cuda(), y.cuda() opt.zero_grad() with torch.cuda.amp.autocast(): output = model(X) loss = criterion(output, y) scaler.scale(loss).backward() if args_clip_norm: scaler.unscale_(opt) nn.utils.clip_grad_norm_(model.parameters(), 1.0) scaler.step(opt) scaler.update() if torch.isnan(loss): print("Loss = nan happened...") train_loss += loss.item() * y.size(0) n += y.size(0) lr_schedule.step(train_loss) - Assistant
Yes, this is a correct implementation of the ReduceLROnPlateau scheduler. The scaler is also used correctly with its recommended methods of scaling the loss, unscale the optimizer if necessary, clipping, step, and update. Therefore, there is no special attention required for the scaler in this implementation.
- User
How do I extract the learning rate after lr_schedule.step(train_loss)?
- Assistant
You can use
opt.param_groupsto access the current learning rate after callinglr_schedule.step(train_loss). Here's an example:lr = opt.param_groups[0]['lr'] print("Current learning rate:", lr)This will print the current learning rate used by the optimizer.