Neural Networks & TensorFlow using python— Simplified | by Singh Dhirender | Sep, 2024


While Learning from DeepLearning.AI, I wanted to simplify the understanding for new learners & hence this wrote this article :
Let’s imagine we’re building a magical machine that can recognize pictures of animals. This magical machine is called a neural network, and we’ll use a special tool called TensorFlow to build it.

Think of a neural network as a big web of connected dots. Each dot is called a neuron, and these neurons work together to recognize patterns, like the shape of a cat or a dog.

These networks are inspired by the human brain and are designed to mimic how we learn and process information.

Layers of a Neural Network

A neural network consists of several layers:

  1. Input Layer: This layer receives the raw data. For example, if you’re working with images, each pixel of the image is an input.
  2. Hidden Layers: These layers are where the magic happens. They process the input data, identify patterns, and learn from them. The more hidden layers you have, the more complex patterns the network can learn.
  3. Output Layer: This layer produces the final result, such as classifying an image as a cat or a dog.

How Do Layers Work?

  1. Input Layer: This layer takes in the raw data. For instance, in an image recognition task, each pixel value is fed into the input layer.
  2. Hidden Layers:

Neurons: Each hidden layer contains neurons that are connected to neurons in the previous and next layers.

Weights and Biases: Weights determine the strength of the connections between neurons, while biases help adjust the output.

Activation Functions: These functions decide whether a neuron should be activated based on the input it receives. Common activation functions include ReLU (Rectified Linear Unit) and Sigmoid.

3. Output Layer: This layer takes the processed information from the hidden layers and produces the final output. For example, it might classify an image as a cat or a dog.

First, we need to install TensorFlow, which is like the magic wand that helps us build our neural network.

# Install TensorFlow
!pip install tensorflow

Let’s say we have pictures of cats and dogs. We’ll tell our neural network which pictures are cats and which are dogs so it can learn.

# Import TensorFlow
import tensorflow as tf

# Load example data (we'll use a built-in dataset for simplicity)
from tensorflow.keras.datasets import mnist

# Load data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

We’ll create a simple neural network with layers of neurons. Think of layers as different levels of understanding.

# Create the model
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)), # Flatten the images
tf.keras.layers.Dense(128, activation='relu'), # First layer of neurons
tf.keras.layers.Dense(10, activation='softmax') # Output layer
])

Now, we’ll teach our neural network to recognize cats and dogs by showing it lots of pictures.

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

# Train the model
model.fit(train_images, train_labels, epochs=5)

Let’s see if our neural network can guess if a new picture is a cat or a dog.

# Test the model
test_loss, test_acc = model.evaluate(test_images, test_labels)

print('\nTest accuracy:', test_acc)

And there you go! Our magical machine (neural network) can now recognize pictures of animals using TensorFlow. 🐱🐶



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*