Simple Neural Network Implementation in Python
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Simple Neural Network runs in O(n*w) time and O(w) space, in Python as in every other language.
- ✓random.gauss and random.uniform are real, built-in Gaussian and uniform samplers, but the realistic tool for initializing a whole layer at once is numpy.random.default_rng(seed), the modern Generator API, with .normal(0, scale, size=(fan_in, fan_out)) for a single vectorized draw of an entire weight matrix.
- ✓Use numpy.random.default_rng(seed), an explicit, per-instance generator, rather than the older global numpy.random.seed()/np.random.randn() pair; the global form makes two networks built in the same process silently share and mutate one shared RNG state, exactly what NumPy's own documentation now recommends against.
- ✓The Python trap: Initializing every weight to the same constant, most naturally np.zeros(shape) because it is the first thing a beginner reaches for to 'keep it simple', makes every neuron in a layer compute an identical output and receive an identical gradient during backpropagation, so they update in lockstep forever.
- ✓The Python-level for epoch in range(...) loop's own overhead is negligible next to the numpy calls it wraps; the real lever is batching.
What Simple Neural Network actually is
Simple Neural Network in Python: what you actually reach for
Python implementation
import numpy as np
def init_network(sizes: list[int], seed: int = 0) -> list[dict]:
rng = np.random.default_rng(seed) # explicit generator, never the deprecated global
layers = []
for fan_in, fan_out in zip(sizes[:-1], sizes[1:]):
scale = np.sqrt(2.0 / fan_in) # He init, not np.zeros
layers.append({
"w": rng.normal(0, scale, size=(fan_in, fan_out)),
"b": np.zeros(fan_out),
})
return layers
def sigmoid(z): return 1.0 / (1.0 + np.exp(-z))
def forward(layers: list[dict], x: np.ndarray) -> np.ndarray:
a = x # x has shape (batch_size, features)
for layer in layers:
a = sigmoid(a @ layer["w"] + layer["b"])
return a
def train(layers, x, y, epochs, lr, backward_fn):
for _ in range(epochs):
pred = forward(layers, x) # one batched call, not a per-example loop
grads = backward_fn(layers, x, y, pred)
for layer, (dw, db) in zip(layers, grads):
layer["w"] -= lr * dw
layer["b"] -= lr * dbThink Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Python-specific trap
Performance in Python, beyond the O(n*w)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Python type or library should I use for Simple Neural Network?
random.gauss and random.uniform are real, built-in Gaussian and uniform samplers, but the realistic tool for initializing a whole layer at once is numpy.random.default_rng(seed), the modern Generator API, with .normal(0, scale, size=(fan_in, fan_out)) for a single vectorized draw of an entire weight matrix.
What is the most common Python mistake when implementing Simple Neural Network?
Initializing every weight to the same constant, most naturally np.zeros(shape) because it is the first thing a beginner reaches for to 'keep it simple', makes every neuron in a layer compute an identical output and receive an identical gradient during backpropagation, so they update in lockstep forever. This footgun is unusually easy to hit in Python specifically because the broken code and the correct code are both one-liners, np.zeros(shape) versus rng.normal(0, scale, shape), that run without any error or warning either way.
How does Simple Neural Network actually perform in Python?
The Python-level for epoch in range(...) loop's own overhead is negligible next to the numpy calls it wraps; the real lever is batching. Replacing an inner for example in batch: loop with a single array-shaped forward call over the whole batch, shape (batch_size, features), turns batch_size separate small matmuls, each paying numpy's fixed per-call dispatch cost, into one larger matmul that amortizes that dispatch cost across every example in the batch. Time complexity is O(n*w) and space is O(w).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.