Sidebar
Search tutorials
Get Started
Documentation
AdvAUCOptimizer
The following class is an optimizer used in conjunction withAdversarialAUROC
Loss:
CLASS AdvAUCOptimizer(Optimizer): (params, a, b, alpha, lr=required, momentum=0.9, dampening=0, weight_decay=0, nesterov=False) [SOURCE]
|
This is a list or an iterable that contains the parameters of the neural network or model that you want to optimize. These parameters include the weights and biases of the model's layers. The optimizer will update these parameters during the optimization process based on the computed gradients. initial values for a, b, $\alpha$ in instance-wise AUROC loss (Default=0.0). It determines the step size taken in the direction of the gradient during each optimization step. It is a parameter that helps accelerate the convergence by adding a fraction of the previous update vector to the current update (Default=0.9). It is a parameter that can be used to reduce the influence of past updates on the current update when momentum is used (Default=0). It is a parameter that represents the coefficient of L2 regularization (Default=0). It is a parameter that indicates whether to use Nesterov Accelerated Gradient (Default=False). |
---|
RegAdvAUCOptimizer
The following class is an optimizer used in conjunction withAdversarialAUROC
Loss:
CLASS RegAdvAUCOptimizer(Optimizer): (params, a, b, alpha, lambda1, lambda2, c1=1, c2=1, gamma=1, lamda=1 ,lr=required) [SOURCE]
|
This is a list or an iterable that contains the parameters of the neural network or model that you want to optimize. These parameters include the weights and biases of the model's layers. The optimizer will update these parameters during the optimization process based on the computed gradients. initial values for a, b, $\alpha$ in instance-wise AUROC loss (Default=0.0). initial coefficient values for regularization (Default=1.0). These parameters control the momentum updates' exponential decay rates (Default=1.0). It is used to scale the gradient updates during optimization. (Default=1.0). It is used in the calculations involving $\alpha$ parameter update. (Default=1.0). It determines the step size taken in the direction of the gradient during each optimization step. |
---|
Example:
from easydict import EasyDict as edict
from torchvision import transforms
import torch
import random
import numpy as np
from XCurve.AUROC.dataloaders import StratifiedSampler # for imbalanced dataset sampling
from XCurve.AUROC.dataloaders import IMBALANCECIFAR10, get_data_loaders # dataloader of Xcurve
from XCurve.AUROC.losses import AdvAUROCLoss, PGDAdversary
from XCurve.AUROC.optimizer import AdvAUCOptimizer
from XCurve.AUROC.models import generate_net # create model or you can adopt any DNN models by Pytorch
seed = 1024
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# set params to create model
args = edict({
"model_type": "resnet18", # (support resnet, densenet121 and mlp)
"num_classes": 2, # number of class
"pretrained": None # if the model is pretrained
})
# Or you can adopt any DNN models by Pytorch
model = generate_net(args).cuda() # generate pytorch model
p_hat = 0.1
criterion = AdvAUROCLoss(imratio=p_hat)
########################
# create optimizer #
########################
optimizer = AdvAUCOptimizer(model.parameters(),
criterion.a, criterion.b, criterion.alpha,
lr=0.1, momentum=0.9,
weight_decay=1e-5)
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
trainset = IMBALANCECIFAR10(root='dataset',
train=True,
download=True,
transform=transform_train)
testset = IMBALANCECIFAR10(root='dataset',
train=False,
transform=transform_test)
sampler = StratifiedSampler(trainset.targets,
batch_size = 32,
rpos = 1,
rneg = 10)
train_loader = torch.utils.data.DataLoader(
trainset,
batch_size=sampler.real_batch_size,
shuffle=(sampler is None),
num_workers=0,
pin_memory=True,
sampler=sampler,
drop_last=True)
# forward of model for one epoch
model.train()
lower_limit, upper_limit = 0.0, 1.0
for index, (X, y) in enumerate(train_loader):
X, y = X.cuda(), y.cuda()
# target.shape => [batch_size, ]
# Note that we ask for the prediction of the model among [0,1]
# for any binary (i.e., sigmoid) or multi-class (i.e., softmax) AUROC optimization.
# obtain adversarial examples
delta = PGDAdversary(model, X, y, criterion, epsilon=8.0/255, alpha=2.0/255,
attack_iters=10, restarts=1, norm='linf')
adv_input = torch.clamp(X + delta, min=lower_limit, max=upper_limit)
adv_input.requires_grad_(requires_grad=False)
# forward
robust_output = model(adv_input).view_as(y)
pred = torch.sigmoid(robust_output) # [batch_size, num_classess] when num_classes > 2, o.w. output [batch_size, ]
robust_loss = criterion(pred, y)
if index % 30 == 0:
print("loss:", robust_loss.item())
# backward
optimizer.zero_grad()
robust_loss.backward()
# If U use RegAdvAUROCLoss, firstly U should use `optimizer.record_grad()`, then use `optimizer.step()` !!!!
# optimizer.record_grad()
optimizer.step()