Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created March 2, 2023 19:17
Show Gist options
  • Save Muqsit/2b85711f8b8d7cad63b764c201432693 to your computer and use it in GitHub Desktop.
Save Muqsit/2b85711f8b8d7cad63b764c201432693 to your computer and use it in GitHub Desktop.
Simple Neural Network (Python)
import numpy as np
from numpy import exp, array, random, dot
class NeuralNetwork():
def __init__(self):
self.synaptic_weights = 2 * random.random((3, 1)) - 1
def __sigmoid(self, x):
return 1 / (1 + exp(-x))
def __sigmoid_derivative(self, x):
return x * (1 - x)
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in range(number_of_training_iterations):
output = self.think(training_set_inputs)
error = training_set_outputs - output
adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))
self.synaptic_weights += adjustment
def think(self, inputs):
return self.__sigmoid(dot(inputs, self.synaptic_weights))
neural_network = NeuralNetwork()
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
neural_network.train(training_set_inputs, training_set_outputs, 10000)
print("Consider new situation [1, 0, 0] -> ?:")
print(neural_network.think(array([1, 0, 0])))
# Consider new situation [1, 0, 0] -> ?:
# [0.99993701]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment