Challenge: Training the Perceptron
Before proceeding with training the perceptron, keep in mind that it uses the binary cross-entropy loss function discussed earlier. The final key concept before implementing backpropagation is the formula for the derivative of this loss function with respect to the output activations, an. Below are the formulas for the loss function and its derivative:
Ldanβ=β(ylog(y^β)+(1βy)log(1βy^β))=y^β(1βy^β)y^ββyββwhere an=y^β
To verify that the perceptron is training correctly, the fit() method also prints the average loss at each epoch. This is calculated by averaging the loss over all training examples in that epoch:
for epoch in range(epochs):
loss = 0
for i in range(training_data.shape[0]):
loss += -(target * np.log(output) + (1 - target) * np.log(1 - output))
average_loss = loss[0, 0] / training_data.shape[0]
print(f'Loss at epoch {epoch + 1}: {average_loss:.3f}')
L=βN1βi=1βNβ(yiβlog(y^βiβ)+(1βyiβ)log(1βy^βiβ))Finally, the formulas for computing gradients are as follows:
dzldWldbldalβ1β=dalβfβ²l(zl)=dzlβ (alβ1)T=dzl=(Wl)Tβ dzlβThe sample training data (X_train) along with the corresponding labels (y_train) are stored as NumPy arrays in the utils.py file. Additionally, instances of the activation functions are also defined there:
relu = ReLU()
sigmoid = Sigmoid()
Swipe to start coding
Your goal is to complete the training process for a multilayer perceptron by implementing backpropagation and updating the model parameters.
Follow these steps carefully:
- Implement the
backward()method in theLayerclass:- Compute the following gradients:
dz: derivative of the loss with respect to the pre-activation values, using the derivative of the activation function;d_weights: gradient of the loss with respect to the weights, calculated as the dot product ofdzand the transposed input vector;d_biases: gradient of the loss with respect to the biases, equal todz;da_prev: gradient of the loss with respect to the activations of the previous layer, obtained by multiplying the transposed weight matrix bydz.
- Update the weights and biases using the learning rate.
- Compute the following gradients:
- Complete the
fit()method in thePerceptronclass:- Compute the model output by calling the
forward()method; - Calculate the loss using the cross-entropy formula;
- Compute dan β the derivative of the loss with respect to the output activations;
- Loop backward through the layers, performing backpropagation by calling each layer's
backward()method.
- Compute the model output by calling the
- Check the training behavior:
- If everything is implemented correctly, the loss should steadily decrease with each epoch when using a learning rate of
0.01.
- If everything is implemented correctly, the loss should steadily decrease with each epoch when using a learning rate of
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Challenge: Training the Perceptron
Swipe to show menu
Before proceeding with training the perceptron, keep in mind that it uses the binary cross-entropy loss function discussed earlier. The final key concept before implementing backpropagation is the formula for the derivative of this loss function with respect to the output activations, an. Below are the formulas for the loss function and its derivative:
Ldanβ=β(ylog(y^β)+(1βy)log(1βy^β))=y^β(1βy^β)y^ββyββwhere an=y^β
To verify that the perceptron is training correctly, the fit() method also prints the average loss at each epoch. This is calculated by averaging the loss over all training examples in that epoch:
for epoch in range(epochs):
loss = 0
for i in range(training_data.shape[0]):
loss += -(target * np.log(output) + (1 - target) * np.log(1 - output))
average_loss = loss[0, 0] / training_data.shape[0]
print(f'Loss at epoch {epoch + 1}: {average_loss:.3f}')
L=βN1βi=1βNβ(yiβlog(y^βiβ)+(1βyiβ)log(1βy^βiβ))Finally, the formulas for computing gradients are as follows:
dzldWldbldalβ1β=dalβfβ²l(zl)=dzlβ (alβ1)T=dzl=(Wl)Tβ dzlβThe sample training data (X_train) along with the corresponding labels (y_train) are stored as NumPy arrays in the utils.py file. Additionally, instances of the activation functions are also defined there:
relu = ReLU()
sigmoid = Sigmoid()
Swipe to start coding
Your goal is to complete the training process for a multilayer perceptron by implementing backpropagation and updating the model parameters.
Follow these steps carefully:
- Implement the
backward()method in theLayerclass:- Compute the following gradients:
dz: derivative of the loss with respect to the pre-activation values, using the derivative of the activation function;d_weights: gradient of the loss with respect to the weights, calculated as the dot product ofdzand the transposed input vector;d_biases: gradient of the loss with respect to the biases, equal todz;da_prev: gradient of the loss with respect to the activations of the previous layer, obtained by multiplying the transposed weight matrix bydz.
- Update the weights and biases using the learning rate.
- Compute the following gradients:
- Complete the
fit()method in thePerceptronclass:- Compute the model output by calling the
forward()method; - Calculate the loss using the cross-entropy formula;
- Compute dan β the derivative of the loss with respect to the output activations;
- Loop backward through the layers, performing backpropagation by calling each layer's
backward()method.
- Compute the model output by calling the
- Check the training behavior:
- If everything is implemented correctly, the loss should steadily decrease with each epoch when using a learning rate of
0.01.
- If everything is implemented correctly, the loss should steadily decrease with each epoch when using a learning rate of
Solution
Thanks for your feedback!
single