def test_force_cuda(self): mnist_cnn.to("cuda") results = maxloss( mnist_cnn, nn.CrossEntropyLoss(), batch_X, batch_y_cuda, device="cuda", )
import seaborn as sns import torch from torch import nn from clustre.attacking import fgsm_perturbs, maxloss_perturbs, pgd_perturbs from clustre.helpers.datasets import mnist_trainloader from clustre.models import mnist_cnn, mnist_resnet18 from clustre.models.state_dicts import mnist_cnn_state, mnist_resnet18_state sns.set() # %% mnist_cnn.load_state_dict(mnist_cnn_state) mnist_resnet18.load_state_dict(mnist_resnet18_state) mnist_cnn.to("cuda") mnist_resnet18.to("cuda") # %% criterion = nn.CrossEntropyLoss() # %% cnn_fgsm = [] for X, y in iter(mnist_trainloader): X = X.to("cuda") y = y.to("cuda") p = fgsm_perturbs(mnist_cnn, criterion, X, y) cnn_fgsm.append(p) # %% cnn_pgd = []
def test_cuda(self): mnist_cnn.to("cuda") results = maxloss(mnist_cnn, nn.CrossEntropyLoss(), batch_X_cuda, batch_y_cuda) self.assertTrue(results.is_cuda)
def test_maxloss_range(self): mnist_cnn.to("cuda") results = maxloss(mnist_cnn, nn.CrossEntropyLoss(), batch_X_cuda, batch_y_cuda) self.assertTrue(((results >= -1) & (results <= 1)).all())
def test_maxloss_shape(self): mnist_cnn.to("cuda") results = maxloss(mnist_cnn, nn.CrossEntropyLoss(), batch_X_cuda, batch_y_cuda) self.assertTupleEqual(results.shape, batch_X.shape)
def test_force_cpu(self): mnist_cnn.to("cuda") results = pgd( mnist_cnn, nn.CrossEntropyLoss(), batch_X, batch_y_cuda, device="cpu" )