Understanding Forward Pass in MLP

Created by Sanasam Ranbir Singh

image.png

In [2]:
import numpy as np 
In [11]:
# ---- Assumptions -----------------
# 
# There is only one hidden layer
# x: input vector [3 nodes]
# y: output vector [2 nodes]
# Hidden layer: 4
# W: weight matrix between input layer and hidden layer
# V: weight matrix between hidden layer and out layer
#
# Description: Given a sample vector x, demonstrate forward pass i.e., sequence of computation at different stages.
#
# -----------------------------------


x = np.array([1,2,3])     # let us say x is the input sample
print("Input Sample vector: x")
print(x)

# let us define the weights W and V. I have randomely initialized
np.random.seed(1)
W = np.random.random((3,4))
V = np.random.random((4,2))
print("Weight Matrix W: ")
print(W)
print("Weight Matrix V: ")
print(V)


# Forward Pass
# Output of the hidden layer
hPer = np.dot(x,W)         # Perceptron
hOut = 1/(1+np.exp(-hPer)) # Sigmoid
print("Output Vector of the hidden layer :")
print(hOut)

# Output of the output layer
oPer = np.dot(hOut,V)         # Perceptron
oOut = 1/(1+np.exp(-oPer)) # Sigmoid
print("Output Vector of the output layer :")
print(oOut)
Input Sample vector: x
[1 2 3]
Weight Matrix W: 
[[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01]
 [1.46755891e-01 9.23385948e-02 1.86260211e-01 3.45560727e-01]
 [3.96767474e-01 5.38816734e-01 4.19194514e-01 6.85219500e-01]]
Weight Matrix V: 
[[0.20445225 0.87811744]
 [0.02738759 0.67046751]
 [0.4173048  0.55868983]
 [0.14038694 0.19810149]]
Output Vector of the hidden layer :
[0.86998614 0.92563206 0.83619955 0.9547442 ]
Output Vector of the output layer :
[0.66512221 0.88502169]
In [ ]:
In [ ]: