Modify the code to create the same MLP and input instance in the figure below ( check the attached picture )
Requirements
Modify the code :
import numpy as np
class MLP:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# Initialize weights
self.weights1 = np.random.randn(self.input_size, self.hidden_size)
self.weights2 = np.random.randn(self.hidden_size, self.output_size)
# Initialize biases
self.bias1 = np.zeros((1, self.hidden_size))
self.bias2 = np.zeros((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 – x)
def forward(self, X):
# Forward pass through the network
self.z1 = np.dot(X, self.weights1) + self.bias1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.weights2) + self.bias2
self.a2 = self.sigmoid(self.z2)
return self.a2
def backward(self, X, y, output):
# Backward pass through the network
self.output_error = y – output
self.output_delta = self.output_error * self.sigmoid_derivative(output)
self.weights2 -= self.a1.T.dot(self.output_delta) #Multiply by Learning
rate
self.z2_error = self.output_delta.dot(self.weights2.T)
self.z2_delta = self.z2_error * self.sigmoid_derivative(self.a1)
self.weights1 -= X.T.dot(self.z2_delta) #Multiply by Learning rate
def train(self, X, y, epochs):
for i in range(epochs):
output = self.forward(X)
self.backward(X, y, output)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
mlp = MLP(2, 4, 1)
mlp.train(X, y, 10000)
# Test the trained model
output = mlp.forward(X)
print(output)
create the same MLP and input instance in the figure below ( check the attached picture ):
The code should output:
1- the correct output f(x) of the model (the same calculated in homework7)
2- run training for one epoch
3- correct the weight update lines in the code to multiply the change by the learning rate before
subtracting it from the old weight.
4- print the updated weights and bias ( the same as calculated in hm7)
5- print the new output of the model.
Requirements: as much as required | .doc file