<jupyter_start><jupyter_text>Forward Propagation<jupyter_code>def sigmoid(z):
    return 1/(1+np.exp(-z))

x = np.random.randn(4)
N_input = 4
N_hidden = 3
N_output = 2

w1 = np.random.randn(N_input,N_hidden)
w2 = np.random.randn(N_hidden,N_output)

a1 = sigmoid(np.dot(x,w1))
a2 = sigmoid(np.dot(a1,w2))

print("Output layer output: ", a2)<jupyter_output>Output layer output:  [0.53 0.57]<jupyter_text>Forward and Backward Propagration<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('binary.csv')
data.head()

df = pd.concat([data,pd.get_dummies(data['rank'],prefix='rank')],axis=1)
df = df.drop('rank',axis=1)

for field in ['gre','gpa']:
    mean, std = df[field].mean(), df[field].std()
    df.loc[:,field] = (df[field]-mean)/std
    
sample = np.random.choice(df.index, size=int(len(data)*0.9), replace=False)
data, test_data = df.ix[sample], df.drop(sample)

features, targets = data.drop('admit',axis=1), data['admit']
features_test, targets_test = test_data.drop('admit', axis=1), test_data['admit']
np.random.seed(21)
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
n_hidden = 2  
epochs = 900
learnrate = 0.005
n_records, n_features = features.shape
last_loss = None
weights_input_hidden = np.random.normal(scale=1 / n_features ** .5,
                                        size=(n_features, n_hidden))
weights_hidden_output = np.random.normal(scale=1 / n_features ** .5,
                                         size=n_hidden)
for e in range(epochs):
    del_w_input_hidden = np.zeros(weights_input_hidden.shape)
    del_w_hidden_output = np.zeros(weights_hidden_output.shape)
    for x, y in zip(features.values, targets):
        hidden_input = np.dot(x, weights_input_hidden)
        hidden_output = sigmoid(hidden_input)

        output = sigmoid(np.dot(hidden_output,
                                weights_hidden_output))
        error = y - output
        output_error_term = error * output * (1 - output)
        hidden_error = np.dot(output_error_term, weights_hidden_output)
        hidden_error_term = hidden_error * hidden_output * (1 - hidden_output)
        del_w_hidden_output += output_error_term * hidden_output
        del_w_input_hidden += hidden_error_term * x[:, None]
    weights_input_hidden += learnrate * del_w_input_hidden / n_records
    weights_hidden_output += learnrate * del_w_hidden_output / n_records
    if e % (epochs / 10) == 0:
        hidden_output = sigmoid(np.dot(x, weights_input_hidden))
        out = sigmoid(np.dot(hidden_output,
                             weights_hidden_output))
        loss = np.mean((out - targets) ** 2)

        if last_loss and last_loss < loss:
            print("Train loss: ", loss, "  WARNING - Loss Increasing")
        else:
            print("Train loss: ", loss)
        last_loss = loss
hidden = sigmoid(np.dot(features_test, weights_input_hidden))
out = sigmoid(np.dot(hidden, weights_hidden_output))
predictions = out > 0.5
accuracy = np.mean(predictions == targets_test)
print("Prediction accuracy: {:.3f}".format(accuracy*100))<jupyter_output>Train loss:  0.2789458953913737
Train loss:  0.277106414200543
Train loss:  0.2753148129773552
Train loss:  0.27357035427240906
Train loss:  0.27187226930439784
Train loss:  0.2702197618893545
Train loss:  0.26861201218319614
Train loss:  0.26704818023277704
Train loss:  0.2655274093328079
Train loss:  0.26404882918756606
Prediction accuracy: 47.500
{'fraction_non_alphanumeric': 0.12304120719674985, 'fraction_numerical': 0.06761462565293093, 'mean_word_length': 3.5716180371352784, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '254291', 'license': 'MIT', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1223, 'n_words': 238, 'path': 'gradient descent/Multi-layer Perceptron from Scratch.ipynb', 'repo_name': 'jsbentley34/deep-learning', 'size': '3446'}