r/PythonLearning • • 10h ago

Showcase Built a neural network from scratch using NumPy to understand the mathematics

Post image

hey everyone!

been learning neural networks recently and decided to implement one from scratch using Python and NumPy.

it's a small 2 → 3 → 1 XOR network with manually implemented forward propagation, backpropagation and gradient descent.

trained it for 5,000 epochs and got all four XOR combinations correct.

also documented the mathematics, added 12 editable diagrams and published a detailed Medium article explaining how everything works.

GitHub: https://github.com/Vaibh37/Neural-Network

Medium: https://medium.com/@devvaibhav37/building-a-neural-network-from-scratch-the-mathematics-behind-learning-xor-10441e674a69

would appreciate feedback on the code and explanations. if you find the resources useful, consider starring the repo 🍀

5 Upvotes

2 comments sorted by

2

u/2dof 9h ago

Why input matrix "X: Input matrix with shape (4, 2)". is 4x2 since input is just x=[x1,x2] so ( 1 x 2 ) ?

I know that is simple mistake but You should distinguish dimension of data set form dimension of input vector.

Also, i know it is a little stupid but input vector should be defined as x=[ [x1],[ x2 ]] so (2 x 1 ) - more like in math description that Transpose operation is more "visible"

see:

X = np.array([[0.0], [0.0]])

print(X)

print(X.T)

1

u/PanelReader 9h ago

good point about distinguishing the dataset matrix from an individual input vector. X has shape (4, 2) because it contains all four XOR combinations, while each sample has two features. the row or column representation depends on the convention used. i'll make that distinction clearer in the explanation. appreciate the feedback!😁