Question: 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.

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 tex
Now, in this task, you needs to build another MLP model that consists of two linear layers and given a data x in R^2, we need the following process:
1. transfer it into hidden feature h in R^100
2. transfer to h in R^10 via linear layer
3. transfer to h'' in R via another linear layer
4. convert to output o in [0,1] via sigmoid.
class MultilayerPerceptron_Q2(torch.nn.Module):
def __init__(self):
super(MultilayerPerceptron_Q2, self).__init__()
#Your code goes to here
def forward(self, x):
#Your code goes to here
return None
# gradient descend
import torch.optim as optim
model = MultilayerPerceptron_Q2()
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 4: 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()], dtype=torch.float32)
# Make predictions for the grid points using the model
Z = model(grid_tensor)
Z = Z.detach().numpy()
Z = Z.reshape(xx.shape)
# Plot the decision boundary
plt.imshow(Z>0.5)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Decision Boundary of MLP')
plt.legend()
plt.show()
[Your answer goes to here. Answer it in plain text]

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!