<jupyter_start><jupyter_text>Simple Neural Network:In this notebook, a fully vectorized 2-layer neural network will be implemented from scratch.   This will 'learn' the XOR gate.<jupyter_code># IMPORTS:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import math
# DATA: The XOR truth table.
inputs = np.array([[0,0],[0,1],[1,0],[1,1]])
expected_output = np.array([[0],[1],[1],[0]])
# ARCHITECTURE:
n0 = 2
n1 = 2
n2 = 1
m = 4
# INPUTS & OUTPUTS:
A0 = inputs.T # Each datapoint is a column.
Y = expected_output.T # Converted to row vector
# WEIGHTS: 
W1 = np.random.uniform(size = (n1, n0))
W2 = np.random.uniform(size = (n2, n1))

# BIASES: 
# Gets added to each column, although mathematically, W*A and b aren't of the same order. Python broadcasting helps us here.
b1 = np.random.uniform(size = (n1, 1))
b2 = np.random.uniform(size = (n2, 1))
# SIGMOID ACTIVATION FUNCTION:
def sigmoid (z):
    return 1/(1 + np.exp(-z))

def sigmoidDerivative(z):
    return sigmoid(z)*(1 - sigmoid(z))
def cost(Y_hat, Y):
    L = -(1./m) * ( np.sum( np.multiply(np.log(Y_hat),Y) ) + np.sum( np.multiply(np.log(1-Y_hat),(1-Y)) ) )
    return L
# GRADIENT DESCENT PARAMETERS:
alpha = 1 # Learning rate.
N_ITERS = 1500 # Total iterations.
costList = []
for _ in range(N_ITERS):
    
    # FORWARD-PROPOGATION:
    Z1 = np.matmul(W1,A0) + b1
    A1 = sigmoid(Z1)
    
    Z2 = np.matmul(W2,A1) + b2
    A2 = sigmoid(Z2)
    Y_hat = A2 # The prediction for all datapoints.
    
    # FINDING OUT THE COST:
    costList.append(cost(Y_hat, Y))

    # BACKWARD-PROPOGATION:
    dZ2 = A2 - Y
    dW2 = (1/m)*np.matmul(dZ2, A1.T)
    db2 = (1/m)*np.sum(dZ2, axis=1, keepdims=True)

    dZ1 = np.matmul(W2.T, dZ2)*(sigmoidDerivative(Z1))
    dW1 = (1/m)*np.matmul(dZ1, A0.T)
    db1 = (1/m)*np.sum(dZ1, axis=1, keepdims=True)
    
    # GRADIENT-DESCENT:
    W1 -= alpha*dW1
    W2 -= alpha*dW2
    b1 -= alpha*db1
    b2 -= alpha*db2
print("Our predictions: ", Y_hat[0])
plt.plot(np.linspace(1, N_ITERS, N_ITERS), costList)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1581237911025145, 'fraction_numerical': 0.043036750483559, 'mean_word_length': 3.459051724137931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '621589', 'license': 'MIT', 'n_tokens_mistral': 922, 'n_tokens_neox': 890, 'n_words': 210, 'path': 'Simple-Neural-Network/Simple Neural Network.ipynb', 'repo_name': 'hrshtv/ML-Algorithms', 'size': '2068'}