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
- Compute the following gradients:
dz
,d_weights
,d_biases
, andda_prev
in thebackward()
method of theLayer
class. - Compute the
output
of the model in thefit()
method of thePerceptron
class. - Compute
da
(dan) before the loop, which is the gradient of the loss with respect to output activations. - Compute
da
and perform backpropagation in the loop by calling the appropriate method for each of the layers.
If you implemented training correctly, given the learning rate of 0.01
, the loss should steadily decrease with each epoch.
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
- Compute the following gradients:
dz
,d_weights
,d_biases
, andda_prev
in thebackward()
method of theLayer
class. - Compute the
output
of the model in thefit()
method of thePerceptron
class. - Compute
da
(dan) before the loop, which is the gradient of the loss with respect to output activations. - Compute
da
and perform backpropagation in the loop by calling the appropriate method for each of the layers.
If you implemented training correctly, given the learning rate of 0.01
, the loss should steadily decrease with each epoch.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 4single