DRW
论文代码
elif args.train_rule == 'DRW':
train_sampler = None
idx = epoch // 160
betas = [0, 0.9999]
effective_num = 1.0 - np.power(betas[idx], cls_num_list)
print(f"\neffective_num:{effective_num}")
per_cls_weights = (1.0 - betas[idx]) / np.array(effective_num)
per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len(cls_num_list)
per_cls_weights = torch.FloatTensor(per_cls_weights).to(args.device)
print(f"\nper_cls_weights:{per_cls_weights}")
160epoch之前,每一类的权重是1;
160epoch之后,每一类的权重经过带β公式计算的,更偏重与少数类的样本。
案例代码
import numpy as np
import torch
# 模拟的类别样本数量
cls_num_list = np.array([5, 50, 100])
# Beta值用于不同训练阶段
betas = [0, 0.9999]
# 总训练轮次
total_epochs = 3
# 模拟训练过程
for epoch in range(total_epochs):
# 根据当前训练轮次计算索引
idx = epoch // 2 # 分割成两个阶段
current_beta = betas[idx]
# 计算有效样本数
effective_num = 1.0 - np.power(current_beta, cls_num_list)
# 计算每类的权重
per_cls_weights = (1.0 - current_beta) / effective_num
per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len(cls_num_list)
# 转为Tensor
per_cls_weights_tensor = torch.FloatTensor(per_cls_weights)
# 输出当前轮次和对应的类别权重
print(f"Epoch {epoch + 1}:")
print(f"effective_num:{effective_num}")
print(f" Beta value: {current_beta:.4f}")
print(f" Class weights: {per_cls_weights_tensor}")