Shortcuts

Source code for openrl.modules.utils.util

import math


[docs]def get_grad_norm(it): sum_grad = 0 for x in it: if x.grad is None: continue sum_grad += x.grad.norm() ** 2 return math.sqrt(sum_grad)
[docs]def update_linear_schedule(optimizer, epoch, total_num_epochs, initial_lr): """Decreases the learning rate linearly""" lr = initial_lr - (initial_lr * (epoch / float(total_num_epochs))) for param_group in optimizer.param_groups: param_group["lr"] = lr
[docs]def huber_loss(e, d): a = (abs(e) <= d).float() b = (abs(e) > d).float() return a * e**2 / 2 + b * d * (abs(e) - d / 2)
[docs]def mse_loss(e): return e**2 / 2