openrl.modules.utils.util 源代码
import math
[文档]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)
[文档]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
[文档]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)