Skip to content
Snippets Groups Projects
Commit 46e8b4cc authored by Bourry Malo's avatar Bourry Malo
Browse files

question 10 - une itération de la descente du gradient

parent cfaf9737
Branches
Tags
No related merge requests found
mlp.py 0 → 100644
import numpy as np
import math
def learn_once_mse(w1: np.ndarray, b1: np.ndarray, w2: np.ndarray, b2: np.ndarray, data: np.ndarray, targets: np.ndarray, learning_rate: float):
# Forward pass
N = np.size(data, 0)
a0 = data # the data are the input of the first layer
z1 = np.matmul(a0, w1) + b1 # input of the hidden layer
a1 = 1 / (1 + math.exp(-z1)) # output of the hidden layer (sigmoid activation function)
z2 = np.matmul(a1, w2) + b2 # input of the output layer
a2 = 1 / (1 + math.exp(-z2)) # output of the output layer (sigmoid activation function)
predictions = a2 # the predicted values are the outputs of the output layer
# Compute loss (MSE)
loss = np.mean((predictions - targets)**2)
print(loss)
#Compute gradient dW
da2 = 2/N*(a2-targets)
dz2 = da2*a2*(1-a2)
dw2 = dz2*a1
db2 = dz2
da1 = dz2*np.sum(w2, axis=1)
dz1 = da1*a1*(1*a1)
dw1 = dz1*a0
db1 = dz1
w1 -= learning_rate*dw1
w2 -= learning_rate*dw2
b1 -= learning_rate*db1
b2 -= learning_rate*db2
return w1, b1, w2, b2, loss
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment