Quick-Start Neural Networks with TensorFlow in Python 3: A Beginner’s Guide

Neural Network in tensorflow using Python | Innovate Yourself
2
0

Are you ready to dive into the exciting world of neural networks using Python 3 and TensorFlow? If you’re between the ages of 18 to 30 and aspire to become a Python pro, this is the perfect place to start. In this comprehensive guide, we’ll take you through the basics of neural networks, step by step, with detailed explanations, examples, and even some cool plots. By the end of this post, you’ll have a solid understanding of neural networks and be well on your way to mastering this powerful field of machine learning.

What are Neural Networks?

Before we jump into the code and start building our neural network, let’s first understand what neural networks are and why they are so important in the world of machine learning.

Neural networks are computational models inspired by the human brain’s structure and function. They consist of layers of interconnected nodes, which are called neurons. Each neuron processes input data and produces an output, which is then used as input for the next layer. Neural networks are used for a wide range of tasks, such as image and speech recognition, natural language processing, and even playing video games.

To work with neural networks in Python, we’ll be using the popular library, TensorFlow. TensorFlow is an open-source machine learning framework developed by Google. It provides a wide range of tools and resources for building and training neural networks.

Setting Up Your Environment

Before we get started with coding, you need to set up your Python environment. If you haven’t already, make sure you have Python 3 installed. Additionally, you’ll need to install TensorFlow. You can install it using pip with the following command:

pip install tensorflow

You’ll also need to install other libraries like NumPy, Matplotlib, and scikit-learn, which we’ll use for data manipulation and visualization.

pip install numpy matplotlib scikit-learn

With your environment all set up, we can now proceed to the fun part – building a basic neural network.

Building a Basic Neural Network

For this tutorial, we’ll create a simple neural network that can classify handwritten digits. We’ll use the famous MNIST dataset, which contains thousands of handwritten digit images. Our network will learn to recognize and classify these digits.

Importing Libraries

Let’s start by importing the necessary libraries:

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

Loading the Dataset

We’ll use TensorFlow’s built-in datasets to load the MNIST dataset. Here’s how you can do it:

mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

The mnist.load_data() function retrieves the dataset, and it’s split into training and testing sets.

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 6s 0us/step

Data Preprocessing

Data preprocessing is a crucial step in building neural networks. We need to normalize the data and reshape it to fit our neural network’s input requirements.

train_images = train_images / 255.0
test_images = test_images / 255.0

train_images = train_images.reshape(-1, 28, 28, 1)
test_images = test_images.reshape(-1, 28, 28, 1)

Building the Neural Network Model

Now, let’s create our neural network model. For this tutorial, we’ll build a simple convolutional neural network (CNN) consisting of three layers: the input layer, a hidden convolutional layer, and the output layer.

model = keras.Sequential([
    keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Flatten(),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

In this model, we use a convolutional layer to extract features from the input images, followed by max-pooling for down-sampling. The flattened layer converts the output to a one-dimensional vector, which is then passed to densely connected layers. The final layer has 10 neurons, each representing a digit from 0 to 9.

Compiling the Model

Now, we need to compile the model. This involves specifying the optimizer, loss function, and metrics.

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Training the Model

With the model compiled, we can train it on our training data:

model.fit(train_images, train_labels, epochs=5)

Training the model will take some time, depending on your hardware. Once training is complete, you’ll see the training accuracy improve over time.

Evaluating the Model

After training, it’s important to evaluate your model’s performance on the test data:

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)
Epoch 1/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.1443 - accuracy: 0.9568
Epoch 2/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0491 - accuracy: 0.9847
Epoch 3/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0308 - accuracy: 0.9905
Epoch 4/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0228 - accuracy: 0.9926
Epoch 5/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0148 - accuracy: 0.9956
313/313 - 1s - loss: 0.0485 - accuracy: 0.9844 - 547ms/epoch - 2ms/step

Test accuracy: 0.9843999743461609

This will give you an idea of how well your model generalizes to new, unseen data.

Visualizing the Results

It’s always helpful to visualize the results of your model. Let’s create some plots to see how well our neural network is performing.

Plotting Training History

We can use Matplotlib to plot the training history of our model, including the loss and accuracy during each epoch.

history = model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels))

plt.figure(figsize=(12, 4))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Accuracy')
plt.plot(history.history['val_accuracy'], label = 'Val Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc='upper right')

plt.show()
Epoch 1/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0106 - accuracy: 0.9965 - val_loss: 0.0423 - val_accuracy: 0.9877
Epoch 2/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0078 - accuracy: 0.9974 - val_loss: 0.0454 - val_accuracy: 0.9874
Epoch 3/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0068 - accuracy: 0.9979 - val_loss: 0.0490 - val_accuracy: 0.9875
Epoch 4/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0050 - accuracy: 0.9982 - val_loss: 0.0543 - val_accuracy: 0.9867
Epoch 5/5
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0040 - accuracy: 0.9987 - val_loss: 0.0572 - val_accuracy: 0.9872
313/313 [==============================] - 0s 1ms/step
Neural Network in tensorflow using Python | Innovate Yourself

These plots will help you understand how well your model is learning from the training data and how it’s performing on the test data.

Visualizing Predictions

Now, let’s see how our model predicts some images from the test dataset. We’ll use Matplotlib to plot the images and their predicted labels.

predictions = model.predict(test_images)

plt.figure(figsize=(10, 10))
for i in range(25):
    plt.subplot(5, 5, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(test_images[i].reshape(28, 28), cmap=plt.cm.binary)
    predicted_label = np.argmax(predictions[i])
    true_label = test_labels[i]
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
    plt.xlabel(f'Predicted: {predicted_label}\nTrue: {true_label}', color=color)
plt.show()

These visualizations provide insights into how your model is making predictions.

Evaluation of Neural Networks in tensorflow using Python | Innovate Yourself

Conclusion

In this extensive tutorial, we’ve covered the basics of building a neural network using Python 3 and TensorFlow.

We used the MNIST dataset to train a simple convolutional neural network, and we visualized the results to assess the model’s performance.

As you continue your journey to becoming a Python pro, remember that neural networks are a fascinating field with endless possibilities. The skills you’ve learned here are just the tip of the iceberg. There’s so much more to explore and discover in the world of machine learning and deep learning.

With practice and dedication, you’ll soon be able to tackle more complex problems and build advanced neural networks that can handle a wide range of tasks. So, keep coding, keep learning, and keep pushing the boundaries of what’s possible with Python and neural networks.

Good luck on your journey, and don’t forget to have fun along the way! Happy coding!

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMachine LearningMQTTTech NewsESP-IDF etc.
Become a member of our social family on youtube here.
Stay tuned and Happy Learning. ✌🏻😃
Happy coding! ❤️🔥

Leave a Reply