Fix My Tutorial
Report

A Neural Network in 11 lines of Python (Part 1)

Open the original tutorial
Fixed Python 3.12 · checked 17 Oct 2026 · model cost $0.203

After the fixes, the tutorial runs successfully under Python 3.12. It originally failed because the scripts lacked the numpy import and used Python 2 constructs such as xrange and print statements, causing a NameError, and the random number generation produced different results due to API changes. The repairs added the missing import, converted the code to Python 3 syntax, and updated the random seeding to use NumPy’s new Generator API with the legacy MT19937 algorithm to preserve the original output.

3 changes to make it work today

Write terse network code

s2 · write_file migrate

Add missing numpy import and change xrange to range for Python 3 compatibility.; Replace deprecated np.random.random with the new default generator; terse.py has no fixed seed so algorithm change is acceptable.

@@ -1,7 +1,10 @@
+import numpy as np
+
X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ])
y = np.array([[0,1,1,0]]).T
-syn0 = 2*np.random.random((3,4)) - 1
-syn1 = 2*np.random.random((4,1)) - 1
-for j in xrange(60000):
+rng = np.random.default_rng()
+syn0 = 2*rng.random((3,4)) - 1
+syn1 = 2*rng.random((4,1)) - 1
+for j in range(60000):
l1 = 1/(1+np.exp(-(np.dot(X,syn0))))
l2 = 1/(1+np.exp(-(np.dot(l1,syn1))))

Sources

  • How To Import Numpy As Np - GeeksforGeeksOnce NumPy is installed, you can import it into your Python script or interactive environment. The standard convention is to use the alias "np" for NumPy. This not only makes your code more concise but also follows a widely adopted practice in the Python community.
  • How to Fix: NameError name 'np' is not defined``` import ``` [...] ``` #define numpy array x = np.random.normal(loc=0, scale=1, size=20) #attempt to print values in arrary print(x) Traceback (most recent call last): ----> 1 x = np.random.normal(loc=0, scale=1, size=20) 2 print(x) NameError: name 'np' is not defined #define numpy array random normal #attempt to print values in arrary print ----> NameError ```
  • How to Fix: NameError name ‘np’ is not defined - GeeksforGeeksHere np is an alias of the NumPy module so we can either import the NumPy module with the alias or import NumPy without the alias and use the name directly.
  • Neural Network returns 0.5 instead of 0The logistic function has a value of 0.5 at the origin. They train the network until the output is … 0.99211957]]
  • A simple neural net with explanation#This neural net is learning to predict the output based on the inputs and output relation. Output After Training: [ 0.99211957]] In
  • A modern neural network in 11 lines of PythonMy title here refers to it as a “modern neural network” because while neural nets have been around since the 1950s, the use of backpropagation, a sigmoid function and the sigmoid’s derivative in Andrew’s script highlight the advances that have made neural nets so popular in machine learning today. For some excellent background on how we got from Frank Rosenblatt’s 1957 hard-wired Mark I Perceptron (pictured here) to how derivatives and backpropagation addressed the limitations of these early neural nets, see Andrey Kurenkov’s A ‘Brief’ History of Neural Nets and Deep Learning, Part 1. The stor
  • Build your own AI Neural Networka Neural Network is used to build predictive models. All the code here is in Python, [ 0.99211957] basically our network has converged statistically to the

Write 2-layer network code

s4 · write_file migrate

Change xrange to range and print statements to function calls for Python 3 compatibility.; Use Generator with MT19937 algorithm and seed 1 to exactly replicate the original random sequence.

@@ -19,10 +19,10 @@
# seed random numbers to make calculation
# deterministic (just a good practice)
-np.random.seed(1)
+rng = np.random.Generator(np.random.MT19937(1))
# initialize weights randomly with mean 0
-syn0 = 2*np.random.random((3,1)) - 1
+syn0 = 2*rng.random((3,1)) - 1
-for iter in xrange(10000):
+for iter in range(10000):
# forward propagation
@@ -40,4 +40,4 @@
syn0 += np.dot(l0.T,l1_delta)
-print "Output After Training:"
-print l1
+print("Output After Training:")
+print(l1)

Sources

  • Neural Network returns 0.5 instead of 0The logistic function has a value of 0.5 at the origin. They train the network until the output is … 0.99211957]]
  • A simple neural net with explanation#This neural net is learning to predict the output based on the inputs and output relation. Output After Training: [ 0.99211957]] In
  • A modern neural network in 11 lines of PythonMy title here refers to it as a “modern neural network” because while neural nets have been around since the 1950s, the use of backpropagation, a sigmoid function and the sigmoid’s derivative in Andrew’s script highlight the advances that have made neural nets so popular in machine learning today. For some excellent background on how we got from Frank Rosenblatt’s 1957 hard-wired Mark I Perceptron (pictured here) to how derivatives and backpropagation addressed the limitations of these early neural nets, see Andrey Kurenkov’s A ‘Brief’ History of Neural Nets and Deep Learning, Part 1. The stor
  • Build your own AI Neural Networka Neural Network is used to build predictive models. All the code here is in Python, [ 0.99211957] basically our network has converged statistically to the

Write 3-layer network code

s6 · write_file migrate

Change xrange to range and print statement to function call for Python 3 compatibility.; Use Generator with MT19937 algorithm and seed 1 to exactly replicate the original random sequence.

@@ -2,8 +2,8 @@
def nonlin(x,deriv=False):
- if(deriv==True):
- return x*(1-x)
+ if(deriv==True):
+ return x*(1-x)
- return 1/(1+np.exp(-x))
+ return 1/(1+np.exp(-x))
X = np.array([[0,0,1],
@@ -13,17 +13,17 @@
y = np.array([[0],
- [1],
- [1],
- [0]])
+ [1],
+ [1],
+ [0]])
-np.random.seed(1)
+rng = np.random.Generator(np.random.MT19937(1))
# randomly initialize our weights with mean 0
-syn0 = 2*np.random.random((3,4)) - 1
-syn1 = 2*np.random.random((4,1)) - 1
+syn0 = 2*rng.random((3,4)) - 1
+syn1 = 2*rng.random((4,1)) - 1
-for j in xrange(60000):
+for j in range(60000):
- # Feed forward through layers 0, 1, and 2
+ # Feed forward through layers 0, 1, and 2
l0 = X
l1 = nonlin(np.dot(l0,syn0))
@@ -33,6 +33,6 @@
l2_error = y - l2
- if (j% 10000) == 0:
- print "Error:" + str(np.mean(np.abs(l2_error)))
+ if (j % 10000) == 0:
+ print("Error:" + str(np.mean(np.abs(l2_error))))
# in what direction is the target value?

Sources

  • Neural Network returns 0.5 instead of 0The logistic function has a value of 0.5 at the origin. They train the network until the output is … 0.99211957]]
  • A simple neural net with explanation#This neural net is learning to predict the output based on the inputs and output relation. Output After Training: [ 0.99211957]] In
  • A modern neural network in 11 lines of PythonMy title here refers to it as a “modern neural network” because while neural nets have been around since the 1950s, the use of backpropagation, a sigmoid function and the sigmoid’s derivative in Andrew’s script highlight the advances that have made neural nets so popular in machine learning today. For some excellent background on how we got from Frank Rosenblatt’s 1957 hard-wired Mark I Perceptron (pictured here) to how derivatives and backpropagation addressed the limitations of these early neural nets, see Andrey Kurenkov’s A ‘Brief’ History of Neural Nets and Deep Learning, Part 1. The stor
  • Build your own AI Neural Networka Neural Network is used to build predictive models. All the code here is in Python, [ 0.99211957] basically our network has converged statistically to the

Tell the author

Send them what broke and the fix, so the next reader doesn't hit the same error.

Badge

Add it to the tutorial so readers know when it was last checked.

Fix My Tutorial badge
Markdown
[![Fix My Tutorial](https://fix-my-tutorial.onrender.com/badge/ri-0l4vsbQW.svg)](https://fix-my-tutorial.onrender.com/r/ri-0l4vsbQW)
HTML
<a href="https://fix-my-tutorial.onrender.com/r/ri-0l4vsbQW"><img src="https://fix-my-tutorial.onrender.com/badge/ri-0l4vsbQW.svg" alt="Fix My Tutorial"></a>