01 logo

Building Neural Networks with TensorFlow: A Step-by-Step Tutorial

Building Neural Networks with TensorFlow to Classify MNIST Data

By Naveen PandeyPublished 11 months ago 3 min read
Like

Neural networks have changed the field of machine learning, allowing computers to learn and make predictions based on complex data. TensorFlow is one of the most popular deep learning frameworks you can use to build and train neural networks. In this article, we will see a step-by-step tutorial for building neural networks using TensorFlow in Python.

1 - Introduction to Neural Networks

Neural networks are a type of machine learning model that takes inspiration from how our brain works. They consist of interconnected artificial neurons, called nodes or units, organized in layers. Each node receives input, performs computations, and passes the output to the next layer. By adjusting the connections between nodes, neural networks can learn complex patterns and make predictions.

2 - Understanding TensorFlow

TensorFlow is an open-source deep learning library developed by Google. It provides a flexible and efficient framework for building and training neural networks. TensorFlow supports both high-level and low-level APIs, allowing users to define and customize neural network architectures with ease.

Step 1: Install TensorFlow

Before we start implementing our code, make sure to install TensorFlow installed on your system. You can install it using pip:

pip install tensorflow

Step 2: Import Necessary Libraries

We'll need TensorFlow and NumPy for this tutorial. Import them as follows:

Image from the author

Step 3: Load and Preprocess the Data

In this tutorial, we'll use the famous MNIST dataset, a collection of 70,000 grayscale images of handwritten digits. TensorFlow provides a convenient method to download and load this dataset:

Image from the author

Step 4: Build the Neural Network

We'll build a simple feed-forward neural network with one hidden layer. TensorFlow's Keras API makes this easy:

Image from the author

Step 5: Compile the Model

Before training the neural network, you need to compile it by specifying the loss function, optimizer, and evaluation metrics. For example:

Image from the author

6. Training the Neural Network

To train the neural network, you need to feed the training data to the model and specify the number of epochs (iterations over the dataset). TensorFlow provides the fit method for this purpose:

Image from the author

In this example, x_train represents the input features, y_train represents the corresponding labels, and epochs are set to 10

7. Evaluating the Model

After training, you can evaluate the performance of the neural network on unseen data. Use the evaluation method, providing the test data and labels:

Image from the author

And there you have it! You've just built and trained a neural network using TensorFlow. This is a simple example, but you can experiment with more complex models. It is a powerful framework for building and training a wide range of neural networks.

Step 8: Making Predictions

After evaluating the model, we can use it to make predictions. Since our model outputs logits, we need to add a softmax layer to convert these logits to probabilities, which are easier to interpret.

Image from the author

The predictions variable now contains the model's predicted probabilities for each digit for each image in the testing set. To get the digit with the highest probability, we can use np.argmax():

Step 9: Visualizing the Predictions

Visualizing the predictions can be helpful to understand how well our model is performing. We can use Matplotlib to plot the image along with the predicted label:

Image from the author

Prediction results

The above code will polt the first 15 test images, along with the predicted and true labels. You can see that correct predictions are in blue, and incorrect predictions are in red. The number gives the percentage (out of 100) for the predicted label.

Step 10: Save and Load the Model

Finally, after training and validating our model, we might want to save it for future use:

Image from the author

Conclusion

In this tutorial, we saw how we can build neural networks using TensorFlow. As you explore this field further, you will see more complex architectures and techniques. But the basic steps—data preprocessing, model building, training, evaluation, and prediction—remain the same.

I hope this article has been helpful to you, thank you for reading this article.

how to
Like

About the Creator

Naveen Pandey

I'm a Data Scientist who loves working with numbers and trends | Ex- Jr. Data Scientist | Ex- Deep Learning Application Developer | Reader | Writer | Founder

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.