Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Last active May 22, 2024 13:25
Show Gist options
  • Save CodeByAidan/b9961129a10124b0bacdd840e3bf9c37 to your computer and use it in GitHub Desktop.
Save CodeByAidan/b9961129a10124b0bacdd840e3bf9c37 to your computer and use it in GitHub Desktop.
A simple neural network - example of a simple perceptron using a logistic (sigmoid) activation function for binary classification - in 12 lines of Python3.11 code
from numpy import array, dot, exp, random
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
rng = random.default_rng(1)
synaptic_weights = 2 * rng.random((3, 1)) - 1
for _ in range(10000):
output = 1 / (1 + exp((dot(-training_set_inputs, synaptic_weights))))
synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))
print(1 / (1 + exp((dot(-array([1, 0, 0]), synaptic_weights)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment