A Neural Network fully-coded in Numpy and Tensorflow

Super simple, can’t be any easier, code provided

Dec 16, 2019 · 2 min read

In a previous post, we explained the mechanics behind Neural networks. In this post we will show a basic implementation in pure Numpy, and in TensorFlow.

As we previously explain, neural networks execution have 4 main steps:

  1. Forward step (where we go from inputs to outputs)
  2. Loss function (where we compare the calculated outputs with real outputs)
  3. Backward step (where we calculate the first delta at the loss function and then back-propagate)
  4. Optimization step (where we update the internal weights with deltas and learning rate)
Neural networks step-by-step

The easiest way to do a full working example, is to take only one operator (matrix multiplication), one loss function (RMSE), one optimizer(gradient descent) and execute a full running example.

def forwardMult(A,B):
return np.matmul(A,B)

def backwardMult(dC,A,B,dA,dB):
dA += np.matmul(dC,np.matrix.transpose(B))
dB += np.matmul(np.matrix.transpose(A),dC)

#Loss example in forward and backward (RMSE)
def forwardloss(predictedOutput,realOutput):
if(predictedOutput.shape == realOutput.shape):
loss = np.mean( 0.5*np.square(predictedOutput - realOutput))
else :
print("Shape of arrays not the same")
return loss

def backwardloss(predictedOutput,realOutput):
if(predictedOutput.shape == realOutput.shape):
deltaOutput = (predictedOutput - realOutput)/predictedOutput.size
else :
print("Shape of arrays not the same")
return deltaOutput

#Optimizer example (SGD)
def updateweights(W,dW,learningRate):
W -= learningRate * dW

The full code can be found in Numpy here, in Tensorflow here, and a comparision between both here.

The repo is made available on GitHub as a tutorial to understand how a neural network works.

In case you still have any questions, please do not hesitate to comment or contact me at: assaad.moawad@datathings.com

I would always be glad to reply to you, improve this article, or collaborate if you have any idea in mind. If you enjoyed reading, follow us on: Facebook, Twitter, LinkedIn


DataThings blog is where we post about our latest machine…


Written by

Interested in artificial intelligence, machine learning, neural networks, data science, blockchain, technology, astronomy. Co-founder of Datathings, Luxembourg


DataThings blog is where we post about our latest machine learning, big data analytics and neural networks experiments. Feel free to visit our website: www.datathings.com

More From Medium

More from DataThings

More from DataThings

More from DataThings

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade