代写GR5242 HW01 Problem 1: Basics代写R语言

GR5242 HW01 Problem 1: Basics

Instructions: This problem is an individual assignment -- you are to complete this problem on your own, without conferring with your classmates. You should submit a completed and published notebook to Courseworks; no other files will be accepted.

Description: The goal of this problem is to get your familiar with neural network training from end to end.

Our main tool is torch , especially torch.nn and torch.optim , that helps us with model building and automatic differentiation / backpropagation.

There are 4 questions in this notebook, including 3 coding quesitons and 1 text question. Each coding question expects 1~3 lines of codes, and the text question expects just 1 sentence of explanation.

In [ ]: # PyTorch imports: # # torch is the base package, nn gives nice classes for Neural Networks, # F contains our ReLU function, optim gives our SG method, # DataLoader allows us to do batches efficiently, # and torchvision is for downloading MNIST data directly from PyTorch import torch from torch import nn from torch.nn import functional as F import torch.optim as optim from torch.utils.data import DataLoader import torchvision.transforms as transforms import torchvision.datasets as datasets # Helper libraries import numpy as np import matplotlib.pyplot as plt print(torch.__version__)

Dataset

We will working on mnist dataset, which contain images of written digits of 0-9 and corresponding labels.

We have it set up to download the data directly from the torch library.

In [ ]: # First, we will define a way of transforming the dataset automatically # upon downloading from pytorch # first convert an image to a tensor and then scale its values to be between -1 transform. = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)),]) # Next, we fetch the data mnist_train = datasets.MNIST(root='./data', train=True, download=True, transform=transform) mnist_test = datasets.MNIST(root='./data', train=False, download=True, transform=transform) # and define our DataLoaders train_loader = DataLoader(mnist_train, batch_size=32, shuffle=True) test_loader = DataLoader(mnist_test, batch_size=32, shuffle=True)

Each image is represented as a 28x28 matrix of pixel values, and each label is the corresponding digit.

Let's show an image of a random one! Try running the below cell a few times to see different examples and how the DataLoaders will be shuffling batches.

Note: Why is this random, when there is no random code in the next cell? The randomness comes from shuffle=True in the train_loader !

In [ ]: inputs, classes = next(iter(train_loader)) plt.imshow(inputs[23].squeeze()) plt.title('Training label: '+str(classes[23].item())) plt.show()

Let's now show 25 of them in black and white:

In [ ]: plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(inputs[i].squeeze(), cmap=plt.cm.binary) plt.xlabel(classes[i].item()) plt.show()

By printing out the shapes, we see there are 60,000 training data and 10,000 test data. Each image is represented as a 28x28 matrix of pixel values, and each label is the corresponding digit.

In [ ]: # For training data train_data_example, train_label_example = mnist_train[0] print("Shape of a single training image:", train_data_example.shape) # For test data test_data_example, test_label_example = mnist_test[0] print("Shape of a single test image:", test_data_example.shape) # The total number of images in each dataset print("Total number of training images:", len(mnist_train)) print("Total number of test images:", len(mnist_test)) Recap of classification task

In a classification task with K classes, suppose the predicted logits for an image are s1, ..., sK. The predicted probabilities are then

The CrossEntropy (CE) loss is defined as

where ti = 1 if the image belongs to the th class or otherwise ti = 0.

Model

Now, we will build a model to predict the logits of images for the classificaiton task.

Question 1: Building the Model

In the following, we will write a class for a basic one-hidden-layer, ReLU, feedforward network. There are a few components to a model in Pytorch, and we will break them down step by step.

First, we need to define the class. As with any class definition, we start with an __init__ method. Since Pytorch provides us with many useful features within the torch.nn.Module class, we will use inheritence to pass these down to our Net class. This involves putting nn.Module inside the parenthesis in the class definition, and a super().__init__() call in the __init__() method.

Within the initialization, we then define two layers: one hidden layer with 128 neurons, and one output layer with 10 class logits. The hidden layer should take an input of size 28 x 28 and give an output of size 128 , while the output layer takes input of size 128 and gives output of size 10 . It is suggested to use the nn.Linear() object to accomplish this, which applies a transformation z = xWT + b.

Next, we define a special method called forward(), which defines how data propagate through the model. This method will be called either by model.forward(x) or by model(x) , and is where Pytorch looks for the information for its automatic derivative computation capabilities.

In the forward method, we first will reshape our image img using img.view() .

Then, we will apply the hidden layer (the one we defined) and the ReLU function F.relu .

Finally, we apply the output layer and return our output. Importantly, do not apply SoftMax to the output just yet. We will handle that part later

In [ ]: class Net(nn.Module): def __init__(self): super(Net, self).__init__() ### YOUR CODE HERE ### # define hidden layer and output layer below: ###################### def forward(self, img): x = img.view(-1, 28*28) # reshape the image to be a single row # pass x through both layers, with ReLU in between ### YOUR CODE HERE ### ###################### return x model = Net()

Question 2: Defining the Loss and Optimizer

When training a torch model, typically you need to specify the following two items:

optimizer: specifies a way to apply gradient descent update of model parameters. We will use the optim.Adam optimizer with a learning rate of 0.001 in this example.

loss_fn: the objective function to minimize over. In classification task, the cross-entropy loss is used.

Please fill in the optimizer with an appropriate learning rate lr , and choose an appropriate number of epochs (number of passes through the data) in the following code.

Note: remember that the neural network outputs the logits instead of the class probabilities (why? answer the question below), and make sure to specify this in the loss function .

In [ ]: loss_fn = nn.CrossEntropyLoss() ### YOUR CODE HERE ### ######################

Question 3: The neural network specified above does not output class probabilities, because the last layer of the neural network is a linear layer which outputs value ranging from (-∞, ∞). Your choice of loss function above should take care of that, but what mathematical function maps these logit values to class probabilities?

#

YOUR ANSWER HERE

#

Training

Now let's train the model for your chosen number of epochs. By the end of the training, you should expect an accuracy above 0.98.

In each step, we need to:

1.) grab x and y from the batch (note that each batch is a tuple of x and y )

2.) zero the optimizer's gradients

3.) make a prediction y_pred

4.) call the loss_fn between y and y_pred

5.) backpropagate

6.) make the approprite step calculated by the optimizer

In [ ]: epochs = 10 for epoch in range(epochs): losses = [] accuracies = [] for batch in train_loader: correct, total = 0, 0 x_batch, y_batch = batch optimizer.zero_grad() ### YOUR CODE HERE ### ###################### for index, output in enumerate(y_logit): y_pred = torch.argmax(output) if y_pred == y_batch[index]: correct += 1 total += 1 ### YOUR CODE HERE ### ###################### loss.backward() optimizer.step() losses.append(loss.item()) accuracies.append(correct/total) avg_loss = np.mean(np.array(losses)) avg_accuracy = np.mean(np.array(accuracies)) print('epoch ' + str(epoch+1) + ' average loss: ', avg_loss, '-- average accuracy: ', avg_accuracy) Test Evaluation

Finally, we evaluate our model on the test set. You could expect the test accuracy to be slightly lower than the training accuracy.

In [ ]: with torch.no_grad(): correct = 0 total = 0 for batch in test_loader: x_batch, y_batch = batch y_logit = model(x_batch) for index, output in enumerate(y_logit): y_pred = torch.argmax(output) if y_pred == y_batch[index]: correct += 1 total += 1 print('testing accuracy:', correct/total) Make Prediction

Question 4: fill in the following code block to estimate class probabilities and make predictions on test images. The results should be stored in class_probabilities and predicted_labels . Compare to the true labels, stored in true_labels by computing the accuracy. It should be the same as above.

(Hint: you can use much of the same structure from the cell above. You can use F.softmax to calculate probabilities from the logits, and store the results however you please.)

In [ ]: ### YOUR CODE HERE ### ######################## print('accuracy verification: ', sum(true_labels==predicted_labels)/len(true_la




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图