Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me solve it in jupyter notebook. Code below is for Question 1. Thanks~ class ConvNet(torch.nn.Module): def __init__(self): super(ConvNet, self).__init__() ############################################################################## # TODO: Define

Please help me solve it in jupyter notebook. Code below is for Question 1. Thanks~

class ConvNet(torch.nn.Module): def __init__(self): super(ConvNet, self).__init__() ############################################################################## # TODO: Define a simple CNN contraining Conv, Pooling, and FC layers. # ############################################################################## # Block 1: 3 x 32 x 32 --> 32 x 16 x 16

# Block 2: 32 x 16 x 16 --> 64 x 8 x 8

# Block 3: 64 x 8 x 8 --> 128 x 4 x 4

# Block 4: 128 x 4 x 4 --> 256 x 2 x 2

# Linear layers: 256 x 2 x 2 --> 1024 --> 2048 --> 4096 --> 4096 --> 10

############################################################################## # END OF YOUR CODE # ##############################################################################

def forward(self, x): ############################################################################## # TODO: Implement forward path turning an input image to class probability. # # For activation function, please use ReLU. # ##############################################################################

# Block 1: 3 x 32 x 32 --> 32 x 16 x 16

# Block 2: 32 x 16 x 16 --> 64 x 8 x 8

# Block 3: 64 x 8 x 8 --> 128 x 4 x 4

# Block 4: 128 x 4 x 4 --> 256 x 2 x 2

# Linear layers: 256 x 2 x 2 --> 1024 --> 2048 --> 4096 --> 4096 --> 10

############################################################################## # END OF YOUR CODE # ############################################################################## return x

model = ConvNet() model.to(device) print(model)

# Hyperparameters epochs = 20 batch_size = 128 learning_rate = 0.25

# Set up optimizer optimizer = optim.SGD(model.parameters(), lr=learning_rate) # Define loss function criterion = torch.nn.CrossEntropyLoss() # Build data loaders train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=1) test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=1) data_loaders = {"train": train_loader, "test": test_loader} dataset_sizes = {"train": train_size, "test": test_size}

def eval_on_test_set(model): model.eval() running_error = 0

for data in test_loader: pass ############################################################################## # TODO: Implement the evaluation process on test set. # ##############################################################################

# Load inputs and labels and deploy to running device

# Forward batch data through the net

# Compute the error made on this batch and add it to the running error

############################################################################## # END OF YOUR CODE # ############################################################################## total_error = running_error / test_size print('error rate on test set = {:.2f}%'.format(total_error * 100)) model.train()

def train_net(model): start=time.time() model.train()

for epoch in range(epochs): # set the running quatities to zero at the beginning of the epoch running_loss = 0 running_error = 0 for data in train_loader: pass ############################################################################## # TODO: Implement the training process. # ##############################################################################

# Load inputs and labels and deploy to running device

# Set the gradients to zeros

# Forward the batch data through the net

# Compute the average of the losses of the data points in the minibatch

# Backward pass to compute gradients

# Do one step of stochastic gradient descent

# Add the loss of this batch to the running loss

# Compute the error made on this batch and add it to the running error ############################################################################## # END OF YOUR CODE # ############################################################################## # Compute stats for the full training set total_loss = running_loss / train_size total_error = running_error / train_size elapsed = (time.time()-start) / 60 print('epoch= {} \t time= {:.2f} min \t loss= {:.3f} \t error= {:.2f}%'.format(epoch, elapsed, total_loss, total_error * 100)) eval_on_test_set(model)

# Start training train_net(model) # Save the trained model torch.save(model.state_dict(), './model_cnn.pt')

assert os.path.exists('./model_cnn.pt'), 'train the model first' # Load the trained model model.load_state_dict(torch.load('./model_cnn.pt', map_location=torch.device('cpu'))) model.to(device) model.eval()

# Choose a picture at random idx = randint(0, test_size-1) print(idx) im, label = test_set[idx] org_im = inverse_transform(im)

# Send to device, rescale, and view as a batch of 1 im = im.to(device) im = im.view(1,3,32,32)

# Feed it to the net and display the confidence scores scores = model(im) probs = F.softmax(scores, dim=1) show_prob_cifar(org_im, label, probs)image text in transcribed

1 Introduction Welcome to CEG5304/EE6934 Project 2! In this project, you are going to start an exciting journey to explore Deep Learning and Neural Networks by completing the following tasks: - Task 1. Building Neural Network (30\%) - Task 2. Exploring Model Training (30\%) - Task 3. Adversarial Attack (40\%) 2 Requirements Before doing the project, please read the requirements carefully (failure to do so will be penalized): - Complete your project in the notebook "Project2.ipynb"; - Implement your codes within "TODO" and "END OF YOUR CODE", do NOT modify any codes outside the answer area; - Make sure your codes clean, easily readable (add meaningful comments if needed), and runnable; - Write your answers in the given markdown cells, keep your answers clear and concise

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

List the commonly used accuracy metrics for KNN.

Answered: 1 week ago

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago