Creating Your First Neural Network in Python w/ Tensorflow
Tags: python, programming, tensorflow, neuralnetworks
Published: 2021-08-12 14:24:26


If you've looked into Machine Learning even a little bit, you've heard of Tensorflow, one of the most popular tools for designing, training, and using machine learning models.

[https://www.tensorflow.org/](https://www.tensorflow.org/)

While you can certainly perform machine learning tasks without any pre-built tools, Tensorflow has become a beloved ecosystem of tools in the ML community to help people bootstrap their ML projects.

> In this tutorial, I'll show you how to get started using Tensorflow, and we'll even build a pretty simple model to train and use.

If you want to see and run the finished product of what we'll be making, you can check out this [demo](https://link.codesphere.com/CQ) on Codesphere.

###How Does Tensorflow Work?

The basic building blocks of Tensorflow, and ML in general, are tensors. Without getting too mathematical, tensors are groups of algebraic objects (most commonly numbers) such as scalars, vectors, or matrices. Tensors can have any number of dimensions, making their formalization very helpful for traversing large and multidimensional datasets.

Tensorflow allows programmers to work with tensors in an incredibly easy and efficient way, and provides an additional library, called Keras, to build ML models out of tensors.

###What We'll Be Creating

To keep things simple, we're going to make a neural network that can predict the outcome of an XOR (either or) operator.
Our model is going to take in 2 values (both booleans) and output 1 value (also a boolean). We'll be using 1's and 0's in place of truth and false.

The inputs and outputs should follow the following logic table:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miftea77i94lkrwzede5.png)

Could this be done much easier with some if statements? Of course. But the point here is to understand how to use Tensorflow and Keras so that you can end up creating something useful yourself.

###Setting Up Our Environment

While this example should probably be fine on your local machine, building machine learning models can be incredibly memory intensive. It's therefore helpful to use a virtual machine. [Codesphere](https://link.codesphere.com/CM) offers an online development environment so that you can code and deploy your apps seamlessly in the cloud.

The only necessary install here is Tensorflow, which we can install with:

`pipenv install tensorflow`

And we are only going to need one python file, which we'll call `main.py`

###Creating and Testing Our Model

Now let's get coding!


{% gist https://gist.github.com/LiorB-D/21f65ad2614eb161dde9f7f654f1bc59 %}

Note the following steps:
1. Define our training data
2. Create our model
3. Add layers
4. Compile our model
5. Fit our model to our training data
6. Predict values

These are the steps you are going to want to take for virtually everything that you're building in TensorFlow.
Also, note a few things which are going to differ based on the purpose and design of your model:

* Layer type: The type of layer you are creating(ie: Dense, Bidirectional)
* Activation function: A function that modifies the input of a neuron(ie: relu, sigmoid)
* Loss function: A function that measures the inaccuracy of a model in correctly predicting the training data(ie: mean squared error, categorical cross entropy)
* Optimization function: The algorithm that is minimizing the loss value of the model. Is what is actually doing the training(ie: adam, stochastic gradient descent)

###Next Steps
If all of these terms feel a bit more daunting than most of the programming that you're doing, it's because it probably is. ML is one of the areas of software engineering that requires more than some nicely worded stack overflow questions. It's a lot of math, statistics, and CS theory.

That doesn't mean, however, that you should just give up on learning it. There are plenty of [books](https://www.google.com/search?q=Introduction+to+Machine+Learning+with+Python:+A+Guide+for+Data+Scientists&stick=H4sIAAAAAAAAAFVUPWwURxT2XLDx7ZnItw4SshBaH41jJbrbn9s9oDA_hsiSrcRgCqrV7c_s3P7M_sxwu3MVoko6ROEOgahAonBBgSgCSpcrUkRWkiqKIlFQ0SEaBHf23dyx1b753vve996befOztRN1ry7LlpHqVu5j5azlEipZcRxINJZCt51hKWrbqIPdI6uDvT44CrINDYZmhPtAqEf1hmYaSt4cg1aeZBCb3PZC1VIyTR_bqKEGsNtIR8G46fiywoNHcngm0tDPNaxeHywMbUXztYh4xhh2itQuDKiMYQsnDcejI2qlQJna4likI5P6fVA-xNIsCackEzvz9ZErsnHeCgw4YYWe1UVcUxJAhlJ_DDdN1GyF-VQJsmlgOoKxqueq3rB4dFMNIlnLOXmGktDrTVQ6zFML7i1HZuobvGAE9VRtNZ2pfqSaNnCfHxZl56Y6JZohk_VBZYjIsh8FAafFhhL5UcI1w6h5LlcVxm2bqqzbS6Z6RymvmA3G66uUXwekOYWXmHTSTtaFBJuTInIvRWQKdyw9S3n7PbPodRnpTrLRsAezSThOwpylXFzPkvOAJPwS9AhNbYenQ2rRKvKC40iB3Zz54YQ-wLrhTKmNcg_6nD5ghUqQPVHXQ1lT5gNEdgBVkrL_wctSZfHth3-XlvdL95_9_hd4WhKOXR68InEfCJUbLt2Nt2OnA5n4CIgPgLC4FcfEDdl1N2xT19mNxe-FuauYdigTTyxXhMN7KaPB1JRvhKNcgR7YyGkliw__2wPizS9Zr4kbQnnbjSw3Iz9C8awgXInD0LVpJ8biyeUloVq3-UF9-LJJ7evrh1ksatrqa1A6X1oFta8GPzWsmC__3Ptj7lZ1ZvDdOdi5uPztmigc3yRbsd0Oq79U_nn16e9362tLwvxuu4hxHLHqp1OnpffP363XVso_XzgzYx68Wa-C1Y-vTr5YuXiqLIHVkqx_KN3ZKX_3em1mkOXur09-mzs2DxZnFh7Pbm5imsXO7UN5w52zPdo2W6NtI-UdiqSfGEUxPi9dkn643XFcCcaZtNGmbemG3XEHzSOU3JsDnwEodzfC0AQAAA&sa=X) and [online courses](https://www.freecodecamp.org/news/search/?query=Neural%20networks) to help you learn!

---

When you're ready to start building, you're going to need a place to deploy your ground-breaking model. When that time comes, you can deploy in minutes to Codesphere, a zero-config cloud provider built to save you time.

Happy Coding!