neural_network_1.py

#!/usr/bin/python3
# ==================================================================
# Create a Simple Neural Network in Python from Scratch
# From: www.youtube.com/watch?v=kft1AJ9WVDk
# See Diagram: neural_network_1.png
# ------------------------------------------------------------------
# Part 2: www.youtube.com/watch?v=Py4xvZx-A1E
# ==================================================================

import numpy as np


# ---- normalizing function
def sigmoid(x):
    return 1 /( 1 + np.exp(-x))

# ---- deritivarive function
def sigmoid_derivatives(x):
    return x + (1-x)


# ---- training matrix data

training_inputs = np.array([[0,0,1],
                            [1,1,1],
                            [1,0,1],
                            [0,1,1]])

training_outputs = np.array([[0,1,1,0]]).T

# ---- seed the random number generator so the results
# ---- can be repeated (for testing, etc)

np.random.seed(1)

# ---- create random starting synaptic weights

synaptic_weights = 2 * np.random.random((3,1)) - 1


print('-------------------------------------------')

print('Random starting synaptic weights:')
print(synaptic_weights)

# ---- main loop
# ---- increate iteration to improve accuracy (100000?)

for iteration in range(20000):

    input_layer = training_inputs

    outputs = sigmoid(np.dot(input_layer,synaptic_weights))

    # ---- adjust sigmoid weights

    error = training_outputs - outputs

    adjustments = error * sigmoid_derivatives(outputs)

    synaptic_weights += np.dot(input_layer.T,adjustments)

print('Synaptic weights after training:')
print(synaptic_weights)


print('-------------------------------------------')

print('Actual output values:')
print(training_outputs)

print('Outputs after training:')
print(outputs)