Skip to content

Instantly share code, notes, and snippets.

View PyDataBlog's full-sized avatar
🥷
Code with purpose

Bernard Brenyah PyDataBlog

🥷
Code with purpose
View GitHub Profile
"""
Update the paramaters of the model using the gradients (∇)
and the learning rate (η).
"""
function update_model_weights(parameters, ∇, η)
L = Int(length(parameters) / 2)
# Update the parameters (weights and biases) for all the layers
for l = 0: (L-1)
"""
Check the accuracy between predicted values (Ŷ) and the true values(Y).
"""
function assess_accuracy(Ŷ , Y)
@assert size(Ŷ) == size(Y)
return sum((Ŷ .> 0.5) .== Y) / length(Y)
end
"""
Compute the gradients (∇) of the parameters (master_cache) of the constructed model
with respect to the cost of predictions (Ŷ) in comparison with actual output (Y).
"""
function back_propagate_model_weights(Ŷ, Y, master_cache)
# Initiate the dictionary to store the gradients for all the components in each layer
∇ = Dict()
L = length(master_cache)
Y = reshape(Y , size(Ŷ))
"""
Partial derivatives of the components of linear forward function
using the linear output (∂Z) and caches of these components (cache).
"""
function linear_backward(∂Z, cache)
# Unpack cache
A_prev , W , b = cache
m = size(A_prev, 2)
# Partial derivates of each of the components
"""
Derivative of the Sigmoid function.
"""
function sigmoid_backwards(∂A, activated_cache)
s = sigmoid(activated_cache).A
∂Z = ∂A .* s .* (1 .- s)
@assert (size(∂Z) == size(activated_cache))
return ∂Z
end
"""
Computes the log loss (binary cross entropy) of the current predictions.
"""
function calculate_cost(Ŷ, Y)
m = size(Y, 2)
epsilon = eps(1.0)
# Deal with log(0) scenarios
Ŷ_new = [max(i, epsilon) for i in Ŷ]
Ŷ_new = [min(i, 1-epsilon) for i in Ŷ_new]
"""
Forward the design matrix through the network layers using the parameters.
"""
function forward_propagate_model_weights(DMatrix, parameters)
master_cache = []
A = DMatrix
L = Int(length(parameters) / 2)
# Forward propagate until the last (output) layer
for l = 1 : (L-1)
"""
Make a linear forward calculation
"""
function linear_forward(A, W, b)
# Make a linear forward and return inputs as cache
Z = (W * A) .+ b
cache = (A, W, b)
@assert size(Z) == (size(W, 1), size(A, 2))
"""
Sigmoid activation function
"""
function sigmoid(Z)
A = 1 ./ (1 .+ exp.(.-Z))
return (A = A, Z = Z)
end
"""
"""
Funtion to initialise the parameters or weights of the desired network.
"""
function initialise_model_weights(layer_dims, seed)
params = Dict()
# Build a dictionary of initialised weights and bias units
for l=2:length(layer_dims)
params[string("W_", (l-1))] = rand(StableRNG(seed), layer_dims[l], layer_dims[l-1]) * sqrt(2 / layer_dims[l-1])
params[string("b_", (l-1))] = zeros(layer_dims[l], 1)