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 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 we need the following process:
transfer it into hidden feature h in R
transfer to h in R via linear layer
transfer to h in R via another linear layer
convert to output o in via sigmoid.
class MultilayerPerceptronQtorchnnModule:
def initself:
superMultilayerPerceptronQ selfinit
#Your code goes to here
def forwardself x:
#Your code goes to here
return None
# gradient descend
import torch.optim as optim
model MultilayerPerceptronQ
op optim.SGDmodelparameterslr
losslst
lossholdoutlst
nepoch
for i in rangenepoch:
# training
loss negativelikeihoodmodel ctensortrain, ctensortrain #compute loss training data
opzerograd #clean cache
loss.backward #compute gradient
opstep #gradient descend
# vaildation: see the performance in unknown data unseen by model
with torch.nograd:
lossholdout negativelikeihoodmodel ctensorholdout, ctensorholdout #compute loss vaildationholdout data
losslstappendlossitem
lossholdoutlstappendlossholdout.item
printloss
printlossholdout
Task : Write down your conclusion after visualize the loss change and the decision bound below:
pltplotnparraylosslst:
pltplotnparraylossholdoutlst:
# prompt: draw decision boundary of logistic regression
# Generate a grid of points for plotting the decision boundary
xmin, xmax
ymin, ymax
xx yy npmeshgridnparangexmin, xmax,
nparangeymin, ymax,
# Create a tensor from the grid points
gridtensor torch.tensornpcxxravel yyravel dtypetorch.float
# Make predictions for the grid points using the model
Z modelgridtensor
Z Zdetachnumpy
Z Zreshapexxshape
# Plot the decision boundary
pltimshowZ
pltxlabelFeature
pltylabelFeature
plttitleDecision Boundary of MLP
pltlegend
pltshow
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
