Skip to content

Instantly share code, notes, and snippets.

@miloharper
Created July 20, 2015 15:57
Show Gist options
  • Save miloharper/62fe5dcc581131c96276 to your computer and use it in GitHub Desktop.
Save miloharper/62fe5dcc581131c96276 to your computer and use it in GitHub Desktop.
A neural network in 9 lines of Python code.
from numpy import exp, array, random, dot
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
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) - 1
for iteration in xrange(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))))
@soundmasteraj
Copy link

Excellent article, easily more interesting than the last 100 I've read recently. Made my day :) Thanks for writing it up!

@henrymendis
Copy link

Thank you sooooo much for putting in such a small code explaining input, training, synaptic weights and output with the relevant formula. Have gone thru other parts too they are very helpful to understand the working of Deep Learning. Do share your latest work too....

@rusert
Copy link

rusert commented Apr 11, 2018

Hello everyone, i am new on Python. I've cloned codes to Atom and started on Terminal, but i got these syntax error, anyone can help me ?

C:\Users\LENOVO\Desktop\Github_Clone_Projects\62fe5dcc581131c96276>short_version.py
File "C:\Users\LENOVO\Desktop\Github_Clone_Projects\62fe5dcc581131c96276\short_version.py", line 9
print 1 / (1 + exp(-(dot(array([1, 0, 0]), synaptic_weights))))
^
SyntaxError: invalid syntax

@solueverest
Copy link

@rusert
You have to give parenthesis (small brackets to print function in your code.
only then it runs on your terminal in python 3.

@knaggita
Copy link

knaggita commented Jun 6, 2018

You are running python2 code with python3. print function in python3 requires parenthesis. e.g print("hello"). You are also likely to face an issue with xrange() function. It was changed to just range () in python3.

@parijatpurohitFE
Copy link

Worthy!!!

@ejamshidiasl
Copy link

Hello
my python is not good but i need this code.
please help me:
1- synaptic_weights is same for all inputs? or is a 2D array?
2- it has no hidden layer.isn't it?

@Leesinbaka
Copy link

python 3 version:
remember python -m pip install numpy
from numpy import exp, array, random, dot
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
random.seed(1)
synaptic_weights = (2 * random.random((3, 1)) - 1)
for iteration 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)))))

@Barberrys
Copy link

python 3 version:
remember python -m pip install numpy
from numpy import exp, array, random, dot
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
random.seed(1)
synaptic_weights = (2 * random.random((3, 1)) - 1)
for iteration 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)))))

I am novice in python. Installed numpy via command line, updated to newest version to work with Python v3.
Copied/pasted proposed updated code with parentheses (starting from: from numpy import exp, array, random, dot... ending with: print (1 / (1 + exp(-(dot(array([1, 0, 0]), synaptic_weights))))).
Executed the code in Pycharm.
It shows me the Error:

xxxxxxxxxxxxxxxs/App.py", line 7
output = (1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights)))))
^
IndentationError: expected an indented block

which indent is meant here? please advice..
thank you((-::

@Leesinbaka
Copy link

Leesinbaka commented Nov 28, 2019

if we using if or for in python
we need to write it like this
for xx in xx:
....output = xx
put 4 (blank space?) in front of the ( )output
sorry for my grammar _(:3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment