Building an Artificial Neural Network in One Line of Code | by Kasper Müller | Towards Data Science

There are tons of great theoretical articles out there on the anatomy and mathematics of artificial neural networks, so I am going to take another approach to writing about and teaching this subject. In this article, we are going to get our hands dirty right away!
Let’s jump right in. You can choose a section below if you want to skip the installation section.
· Installation ∘ Option 1 ∘ Option 2· The data· The model· Why TensorFlow?
If you already have the requirements installed, then you can skip this section.
The first thing we need to do is to make sure that we have the Python programming language installed so if you don’t then go ahead and install it here.
Note that if in the installation process you are asked if you want to add Python to your PATH variable, then make sure to agree to that. The next thing to do is to open up a terminal or CMD depending on your operating system.
If you are on Windows, then press the windows button and type “cmd”. Then double click the black icon where it says “Command Prompt” at the top of the results.
Now simply type:
pip install numpypip install matplotlib
and
pip install tensorflow
Note that you might have to type “pip3” instead of “pip” if you are on Mac or Linux.
The next thing you need to do is to simply open up some kind of text editor or IDE like nodepad, Pycharm, VSCode, Sublime or some other editor.
Install Google’s Colaboratory in your Google Drive. You can get all the information needed to do so on the website.
colab.research.google.com
One of the benefits of Google Colab is that you get free access to a lot of computing power. Specifically, you can train your models on GPUs in the cloud which makes this a great research framework.
When we train a supervised machine learning algorithm, we need labeled data. That is, we need a dataset where for each example x we need a label y that the model should predict when it is being fed x.
In this article, we will do the simplest thing possible. We won’t be thinking of testing, so this is not about best practice! We will simply consider a linear relationship and try to fit the model by building a neural network of only one neuron in Google’s machine learning library TensorFlow.
Let’s open up our editor of choice and type in
This code makes sure that all the relevant libraries are loaded. You would like the version of TensorFlow to be 2.x for some x ≥ 0, so try a simple
print(tf.__version__)
You should see something like 2.5.0.
Let’s create the data. We will be creating our own data. Simply create a linear relationship yourself like this.
You might, as the intelligent human being that you are, notice the pattern already.
If you guessed f(x) = 1/2 + x/2, you were right.
The idea is this: we are going to show the network values from the xs array and tell the model the correct answers in the form of the ys array so that for each value of x, the model will make a guess, and it will correct itself by looking at the true corresponding y value.
Then the model will adjust itself through something called backpropagation (we will use stochastic gradient descent) and for each example, the model will improve.
We don’t have much data, so this will limit the accuracy of the model of course, but the only goal of this article is to capture the idea of a neural network and to get into some simple syntax of TensorFlow.
We are going to create an artificial neural network of a single node in one line of code, namely the following:
Now we have built the network, let’s train it.
The optimizer makes sure that the network improves its guesses and the loss function measures how good or bad a single guess was.
Now let’s make a prediction.
We get about 4.12. Now let’s plot some predictions.
The output we get is the following image, showing the ground truth in green and the model's predictions in red.
Note that this is not linear regression since this hasn’t anything to do with “best fit”, but rather the result of a model that tries to correct itself for each attempt. The lack of data and the simplicity of the model is telling, but we still manage to capture the essence of a neural network in this simple task.
To drive this point home, try to run this code again. You won’t get exactly the same predictions because this is a new model and a machine learning model is probabilistic in nature.
For instance, when I ran it the second time, I got a new prediction when I fed the model the number 7. I got about 4.08. And you will get something different from me!
You can try to play around with this code to familiarize yourself with the syntax and effect of the (hyper) parameters.
Try for example to set the epochs to 200 or 1000. Try other linear relationships.
There are several reasons why TensorFlow is a very good choice if you want to learn how to build machine learning models.
TensorFlow is one of the most in-demand and popular deep learning frameworks available today.
That it is a very mature library because it was built by Google and that it can be used with Python are two great reasons, but why don’t we just take a look at some of the companies using it to get a feel for its importance in the industry.
Companies using TensorFlow include
Airbnb
AIRBUS
Coca-Cola
Google
Intel
Lenovo
PayPal
Spotify
Twitter
and the list goes on...
So TensorFlow is here to stay and if you want a job as a data scientist or ML engineer in the future, there are plenty of companies that hire TensorFlow developers so learning this great framework is definitely worth your time.
We reached the end of this small and very simple introduction to TensorFlow and neural networks.
In the future, I will post increasingly more and more advanced machine learning posts but we need to start simple.
If you have any questions, comments or concerns please reach out on LinkedIn.
www.linkedin.com
See you next time.