# Creating Your First Neural Network in Python w/ Tensorflow

source link:https://dev.to/codesphere/creating-your-first-neural-network-in-python-w-tensorflow-4l5p

Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

# Creating Your First Neural Network in Python w/ Tensorflow

Aug 12

・3 min read

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.

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 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:

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 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!

import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers

# Define our training input and output data with type 16 bit float # Each input maps to an output

X = tf.constant([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=tf.float16) Y = tf.constant([[0], [1], [1], [0]], dtype=tf.float16)

# Create a new Sequential Model model = keras.Sequential()

# Add our layers

model.add(layers.Dense( 4, # Amount of Neurons input_dim=2, # Define an input dimension because this is the first layer activation='relu' # Use relu activation function because all inputs are positive ))

model.add(layers.Dense( 1, # Amount of Neurons. We want one output activation='sigmoid' # Use sigmoid because we want to output a binary classification ))

# Compile our layers into a model

model.compile( loss='mean_squared_error', # The loss function that is being minimized optimizer='adam', # Our optimization function metrics=['binary_accuracy'] # Metrics are different values that you want the model to track while training )

# Our function to take in two numerical inputs and output the relevant boolean def cleanPredict(a, b): inputTens = tf.constant([[a,b]]) return round(model.predict(inputTens)[0][0]) == 1 # model.predict(input) yields a 2d tensor

print(cleanPredict(1,0)) # Will yield a random value because model isn't yet trained

model.fit( X, # Input training data Y, # Output training data epochs=2000, # Amount of iterations we want to train for verbose=1 # Amount of detail you want shown in terminal while training )

print(cleanPredict(1,0)) # Should Yield True

Note the following steps:

- Define our training data
- Create our model
- Add layers
- Compile our model
- Fit our model to our training data
- 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 and online courses 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!

## Recommend

## About Joyk

Aggregate valuable and interesting links.

Joyk means Joy of geeK