Question: Assignment 7 & 8 : Training MLP In this assignment, you are asked to train two different MLPs and applies to the created XOR datasets.

Assignment 7 & 8: Training MLP In this assignment, you are asked to train two different MLPs and applies to the created XOR datasets. Additionally, you need to discuss your result. There are total 4 tasks Answer the question in the designed place and run all the code. For coding parts, ll in your answer in: #Your code goes to here
For text answer, ll in your answer in: Your answer goes to here. Answer it in plain text Consider a XOR dataset as below import numpy as np #Create XOR dataset. A classical non-linear seperatable dataset # Generate data for class A mean1=[-1,-1] cov1=[[0.1,0],[0,0.1]] class0_data1= np.random.multivariate_normal(mean1, cov1,100) mean2=[1,1] cov2=[[0.1,0],[0,0.1]] class0_data2= np.random.multivariate_normal(mean2, cov2,100) class0_data = np.concatenate((class0_data1, class0_data2), axis=0) class0_labels = np.zeros(200) # Generate data for class B mean3=[-1,1] cov3=[[0.1,0],[0,0.1]] class1_data1= np.random.multivariate_normal(mean3, cov3,100) mean4=[1,-1] cov4=[[0.1,0],[0,0.1]] class1_data2= np.random.multivariate_normal(mean4, cov4,100) class1_data = np.concatenate((class1_data1, class1_data2), axis=0) class1_labels = np.ones(200) # Combine data and labels X = np.concatenate((class0_data, class1_data), axis=0) y = np.concatenate((class0_labels, class1_labels), axis=0) Visualize the data import matplotlib.pyplot as plt plt.scatter(class0_data[:,0],class0_data[:,1]) plt.scatter(class1_data[:,0],class1_data[:,1]) Start coding or generate with AI. import torch c1_tensor = torch.from_numpy(class0_data).float() c2_tensor = torch.from_numpy(class1_data).float() Task 1: Training a MLP Model with 10 dimensional hidden layer Recall the example shown in class can be described as following: Given data , we doing the following process: 1. transfer it into hidden feature 2. pass through a sigmoid function 3. transfer to 4. convert to output via sigmoid. Now, in this task, you needs to build a MLP model that consists of two linear layer and given a data , we need the following process: 1. transfer it into hidden feature 2. pass through a sigmoid function 3. transfer to 4. convert to output via sigmoid. x in R2 h in R3 in R h o in [0,1] x in R2 h in R10 in R h o in [0,1] import torch class MultilayerPerceptron_Q1(torch.nn.Module): def __init__(self): super(MultilayerPerceptron_Q1, self).__init__() #Your code goes to here def forward(self, x): #Your code goes to here return None def negative_likeihood(model, katydid_tensor, grasshopper_tensor): o = model(katydid_tensor) #confidence of a data belongs to the katydid likeihood =- torch.log(o+0.000001) o = model(grasshopper_tensor) likeihood2=- torch.log((1-o)+0.000001) overall_loss = likeihood.sum()+ likeihood2.sum() return overall_loss # randomly hold out samples to evaluate model indices_holdout_c1= torch.randperm(len(c1_tensor))[:150] indices_holdout_c2= torch.randperm(len(c2_tensor))[:150] c1_tensor_holdout = c1_tensor[indices_holdout_c1] c2_tensor_holdout = c2_tensor[indices_holdout_c2] # remove holdout samples from training data (c1_tensor and c2_tensor) vector_c1= torch.zeros(200) for i in indices_holdout_c1: vector_c1[i]=1 c1_tensor_train = c1_tensor[vector_c1!=1] vector_c2= torch.zeros(200) for i in indices_holdout_c2: vector_c2[i]=1 c2_tensor_train = c2_tensor[vector_c2!=1] # gradient descend import torch.optim as optim model = MultilayerPerceptron_Q1() op = optim.SGD(model.parameters(),lr=0.01) loss_lst =[] loss_holdout_lst =[] n_epoch =5000 for i in range(n_epoch): # training loss = negative_likeihood(model, c1_tensor_train, c2_tensor_train) #compute loss (training data) op.zero_grad() #clean cache loss.backward() #compute gradient op.step() #gradient descend # vaildation: (see the performance in unknown data (unseen by model)) with torch.no_grad(): loss_holdout = negative_likeihood(model, c1_tensor_holdout, c2_tensor_holdout) #compute loss (vaildation/holdout data) loss_lst.append(loss.item()/100) loss_holdout_lst.append(loss_holdout.item()/300) print(loss) print(loss_holdout) Task 2: Write down your conclusion after visualize the loss change and the decision bound below: plt.plot(np.array(loss_lst[60:])) plt.plot(np.array(loss_holdout_lst[60:])) # prompt: draw decision boundary of logistic regression # Generate a grid of points for plotting the decision boundary x_min, x_max =-3,3 y_min, y_max =-3,3 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) # Create a tensor from the grid points grid_tensor = torch.tensor(np.c_[xx.ravel(), yy.ravel

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!