BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

机器学习代写|Appendix D Competition: Resilience to Adversarial Attack

机器学习代写|Appendix D Competition: Resilience to Adversarial Attack

这是一篇关于附录D比赛:对对抗性攻击的弹性的机器学习代写

 

This is a student competition to address two key issues in modern deep learning, i.e.,O1 how to find better adversarial attacks, and O2 how to train a deep learning model with better robustness to the adversarial attacks.

We provide a template code (Competition/Competition.py), where there are two code blocks corresponding to the training and the attack, respectively. The two code blocks are filled with the simplest implementations representing the baseline methods,and the participators are expected to replace the baseline methods with their own implementations, in order to achieve better performance regarding the above O1 and O2.

D.1 Submissions

In the end, we will collect submissions from the students and rank them according to a pre-specified metric taking into consideration both O1 and O2. Assume that we have 𝑛 students participating in this competition, and we have a set 𝑆 of submissions.

Every student with student number 𝑖 will submit a package 𝑖.zip, which includes two files:

  1. 𝑖.pt, which is the file to save the trained model, and
  2. competition_𝑖.py, which is your script after updating the two code blocks

in Competition.py with your implementations.

NB: Please carefully follow the naming convention as indicated above, and we will not accept submissions which do not follow the naming convention.

D.2 Source Code

The template source code of the competition is available at

https://github.com/xiaoweih/

AISafetyLectureNotes/tree/main/Competition

In the following, we will explain each part of the code.

Load packages

First of all, the following code piece imports a few packages that are needed.

1 import numpy as np

2 import pandas as pd

3 import torch

4 import torch.nn as nn

5 import torch.nn. functional as F

6 from torch.utils.data import Dataset , DataLoader

7 import torch.optim as optim

8 import torchvision

9 from torchvision import transforms

10 from torch.autograd import Variable

11 import argparse

12 import time

13 import copy

Note: You can add necessary packages for your implementation.

Define competition ID

The below line of code defines the student number. By replacing it with your own student number, it will automatically output the file 𝑖.pt once you trained a model.

1 # input id

2 id_ = 1000

Set training parameters

The following is to set the hyper-parameters for training. It considers e.g., batch size, number of epochs, whether to use CUDA, learning rate, and random seed. You may change them if needed.

1 # setup training parameters

2 parser = argparse. ArgumentParser ( description =’PyTorch MNIST

Training’)

3 parser. add_argument (‘–batch -size’, type=int, default =128 ,

metavar=’N’,

4

help=’input batch size for training (default:

128)’)

5 parser. add_argument (‘–test -batch -size’, type=int, default =128 ,

metavar=’N’,

6

help=’input batch size for testing (default:

128)’)

7 parser. add_argument (‘–epochs’, type=int, default =10, metavar=’N’

,

8

help=’number of epochs to train’)

9 parser. add_argument (‘–lr’, type=float , default =0.01 , metavar=’LR

‘,

10

help=’learning rate’)

11 parser. add_argument (‘–no-cuda’, action=’store_true’, default=

False ,

12

help=’disables CUDA training’)

13 parser. add_argument (‘–seed’, type=int, default =1, metavar=’S’,

14

help=’random seed (default: 1)’)

15 args = parser. parse_args (args =[])

Toggle GPU/CPU

Depending on whether you have GPU in your computer, you may toggle between

devices with the below code. Just to remark that, for this competition, the usual CPU

is sufficient and a GPU is not needed.

1 # judge cuda is available or not

2 use_cuda = not args.no_cuda and torch.cuda. is_available ()

3 #device = torch.device(“cuda” if use_cuda else “cpu”)

4 device = torch.device(“cpu”)

5

6 torch. manual_seed (args.seed)

7 kwargs = {‘num_workers’: 1, ‘pin_memory’: True} if use_cuda else

{}

Loading dataset and define network structure

In this competition, we use the same dataset (FashionMNIST) and the same network architecture. The following code specify how to load dataset and how to construct a

3-layer neural network. Please do not change this part of code.

1 ####################################################don’t change

the below code

####################################################

2

3 train_set = torchvision .datasets. FashionMNIST (root=’data’, train=

True , download =True , transform = transforms .Compose ([ transforms

.ToTensor ()]))

4 train_loader = DataLoader (train_set , batch_size =args.batch_size ,

shuffle=True)

5

6 test_set = torchvision .datasets. FashionMNIST (root=’data’, train=

False , download=True , transform = transforms .Compose ([

transforms . ToTensor ()]))

7 test_loader = DataLoader (test_set , batch_size =args.batch_size ,

shuffle=True)

8

9 # define fully connected network

10 class Net(nn.Module):

11

def __init__(self):

12

super(Net , self).__init__ ()

13

self.fc1 = nn.Linear (28*28 , 128)

14

self.fc2 = nn.Linear (128 , 64)

15

self.fc3 = nn.Linear (64, 32)

16

self.fc4 = nn.Linear (32, 10)

17

18

def forward(self , x):

19

x = self.fc1(x)

20

x = F.relu(x)

21

x = self.fc2(x)

22

x = F.relu(x)

23

x = self.fc3(x)

24

x = F.relu(x)

25

x = self.fc4(x)

26

output = F. log_softmax (x, dim =1)

27

return output

28

29 ####################################################end of “don’t

change the below code”

####################################################

Adversarial Attack

The part is the place needing your implementation, for O1. In the template code, it includes a baseline method which uses random sampling to find adversarial attacks.

bestdaixie

评论已关闭。