A Neural Network Model is a Linear Algebra Equation with Weights and Bias for each Features of the Data Set. When we train the Neural Network Model, we multiply our data set Matrix of Features at first with Random Weights and Bias. After each iteration of Matrix Multiplication we
- Compare our Results with the Correct Prediction
- Calculate the Off Shoot deviation
- Update the random Weight and Bias with new values resulting in more accurate Predictions
After 1000s of Iteration, our Matrix Multiplication of the whole Dataset become increasingly more accurate and matching closely with the target values. At that point we arrive with the Near Optimal Weight and Bias value for all features of our Dataset.
Complex large datasets with MANY FEATURES and MANY DATA POINTS, requires millions or billions of Linear Algebra MATRIX MULTIPLICATION operations. As the result, computers with powerful GPUs performs these MATRIX MULTIPLICATION thousands time faster than traditional LOOP serial execution methods.
### Calculate Predicted Values for All Data Points
def forward_propagation(X, parameters):
W = parameters['W']
b = parameters['b']
Z = W@X + b
Y_hat = Z
return Y_hat
### Calculate the Off Target Deviation
def compute_cost(Y_hat, Y):
data_size = Y.shape[1]
## Cost of Off Target Function
cost = np.sum((Y_hat - Y)**2)/(2*data_size)
return cost
### Define Neural Network Model
def nn_model(X, Y, num_iterations=1000, print_cost=False):
n_x = X.shape[0]
print("Number of Samples",n_x)
### Initiate Params with Random Number
W = np.random.randn(1, n_x) * 0.01 # @REPLACE EQUALS None
print("Samples Weight",W, W.shape)
b = np.zeros((1, 1)) # @REPLACE EQUALS None
parameters = {"W": W,"b": b}
### Loop for 1000 Iteration and Gradually Update the Params to More Accurate Prediction of Y
for i in range(0, num_iterations):
Y_hat = forward_propagation(X, parameters) ### Calculate Prediction
cost = compute_cost(Y_hat, Y) ### Calculate Off Target
parameters = NN_utils.train_nn(
parameters,
Y_hat,
X,
Y,
learning_rate=1/1000
) ### Update the Params
if print_cost:
if i%100 == 0:
print ("Cost after iteration %i: %f" %(i, cost))
return parameters