Last active
April 12, 2023 15:42
-
-
Save RafayAK/96bcf2ef63da0e79950500bc30c1c06a to your computer and use it in GitHub Desktop.
Class for Linear Layer
This file contains 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
import numpy as np # import numpy library | |
from util.paramInitializer import initialize_parameters # import function to initialize weights and biases | |
class LinearLayer: | |
""" | |
This Class implements all functions to be executed by a linear layer | |
in a computational graph | |
Args: | |
input_shape: input shape of Data/Activations | |
n_out: number of neurons in layer | |
ini_type: initialization type for weight parameters, default is "plain" | |
Opitons are: plain, xavier and he | |
Methods: | |
forward(A_prev) | |
backward(upstream_grad) | |
update_params(learning_rate) | |
""" | |
def __init__(self, input_shape, n_out, ini_type="plain"): | |
""" | |
The constructor of the LinearLayer takes the following parameters | |
Args: | |
input_shape: input shape of Data/Activations | |
n_out: number of neurons in layer | |
ini_type: initialization type for weight parameters, default is "plain" | |
""" | |
self.m = input_shape[1] # number of examples in training data | |
# `params` store weights and bias in a python dictionary | |
self.params = initialize_parameters(input_shape[0], n_out, ini_type) # initialize weights and bias | |
self.Z = np.zeros((self.params['W'].shape[0], input_shape[1])) # create space for resultant Z output | |
def forward(self, A_prev): | |
""" | |
This function performs the forwards propagation using activations from previous layer | |
Args: | |
A_prev: Activations/Input Data coming into the layer from previous layer | |
""" | |
self.A_prev = A_prev # store the Activations/Training Data coming in | |
self.Z = np.dot(self.params['W'], self.A_prev) + self.params['b'] # compute the linear function | |
def backward(self, upstream_grad): | |
""" | |
This function performs the back propagation using upstream gradients | |
Args: | |
upstream_grad: gradient coming in from the upper layer to couple with local gradient | |
""" | |
# derivative of Cost w.r.t W | |
self.dW = np.dot(upstream_grad, self.A_prev.T) | |
# derivative of Cost w.r.t b, sum across rows | |
self.db = np.sum(upstream_grad, axis=1, keepdims=True) | |
# derivative of Cost w.r.t A_prev | |
self.dA_prev = np.dot(self.params['W'].T, upstream_grad) | |
def update_params(self, learning_rate=0.1): | |
""" | |
This function performs the gradient descent update | |
Args: | |
learning_rate: learning rate hyper-param for gradient descent, default 0.1 | |
""" | |
self.params['W'] = self.params['W'] - learning_rate * self.dW # update weights | |
self.params['b'] = self.params['b'] - learning_rate * self.db # update bias(es) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment