Question: Please help me fill out the code. U may open it in Jupyter notebook. Thanks a lot~ class ConvNet(torch.nn.Module): def __init__(self): super(ConvNet, self).__init__() ############################################################################## #
Please help me fill out the code. U may open it in Jupyter notebook. Thanks a lot~
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)
# 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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
