This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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(Ŷ)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Sigmoid activation function | |
| """ | |
| function sigmoid(Z) | |
| A = 1 ./ (1 .+ exp.(.-Z)) | |
| return (A = A, Z = Z) | |
| end | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |