Introduction to ANN with steps 10 25
Introduction to ANN with steps 10 25
1. Input Layer:
2. Hidden Layers:
o Intermediate layers that transform inputs into something the output layer can
use.
3. Output Layer:
o Bias: Allows shifting the activation function to better fit the data.
5. Activation Functions:
We'll build a simple neural network using the popular Keras library to classify handwritten
digits from the MNIST dataset.
Step-by-Step Process
Sample Code:
python
Copy code
import tensorflow as tf
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential([
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.legend()
plt.subplot(1, 2, 2)
plt.legend()
Expected Output
4o
You said:
yes
ChatGPT said:
ChatGPT
Analysis errored
python
Copy code
import numpy as np
layer_sizes = [784, 128, 10] # Input (784 nodes), Hidden (128 nodes), Output (10 nodes)
spacing = 3
x_pos = spacing * i
# Aesthetic adjustments
ax.set_xlim(-1, 2 * spacing + 1)
ax.set_ylim(-1.5, 1.5)
ax.axis('off')
# Title
plt.show()
Analyzed
python
Copy code
import numpy as np
layer_sizes = [784, 128, 10] # Input (784 nodes), Hidden (128 nodes), Output (10 nodes)
spacing = 3
x_pos = spacing * i
# Aesthetic adjustments
ax.set_xlim(-1, 2 * spacing + 1)
ax.set_ylim(-1.5, 1.5)
ax.axis('off')
# Title
plt.show()
STDOUT/STDERR
• Output Layer: 10 neurons representing digits (0-9) with Softmax activation function.
Weights and biases are adjusted during training to minimize prediction error. Arrows
represent connections with trainable weights.
step-by-step example demonstrating the complete cycle of a neural network built using
TensorFlow and Keras to classify handwritten digits (MNIST dataset).
1. Import Libraries
python
Copy code
import numpy as np
import tensorflow as tf
python
Copy code
# Load data
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
python
Copy code
model = Sequential([
Dense(128, activation='relu'), # Hidden layer with 128 neurons and ReLU activation
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
python
Copy code
Copy code
6. Make Predictions
python
Copy code
sample_image = X_test[0]
actual_label = np.argmax(y_test[0])
python
Copy code
plt.imshow(sample_image, cmap='gray')
plt.axis('off')
plt.show()
Output Explanation
The network works by gradually adjusting weights and biases through backpropagation to
predict correct labels for unseen data.
A neural network is a computational model inspired by the way biological neural networks in
the human brain function. It consists of layers of interconnected "neurons" (nodes) that
process input data and adjust internal parameters (weights and biases) to learn patterns.
1. Input Layer:
2. Hidden Layers:
o Can have multiple hidden layers, with each layer learning increasingly
complex features.
3. Output Layer:
4. Weights:
5. Biases:
o Constants added to the weighted sum of inputs before applying the activation
function.
o Helps the model shift activation functions and better fit the data.
6. Activation Functions:
o Introduce non-linearity into the network, allowing it to learn complex
patterns.
Activation
Mathematical Form Usage Graph Characteristics
Function
f(xi)=exi∑exjf(x_i) =
Multi-class Outputs probabilities
Softmax \frac{e^{x_i}}{\sum e^{x_j}}f(xi
classification for each class
)=∑exjexi
1. Input Layer:
2. Hidden Layer 1:
3. Hidden Layer 2:
4. Output Layer:
Training Process
1. Forward Propagation:
o Inputs pass through each layer, activating neurons and generating outputs.
2. Loss Calculation:
3. Backward Propagation:
4. Optimization:
To understand how a neural network processes and learns from data, visual representations
are essential. These visuals help explain the layers, activations, weights, and learning
process.
Sigmoid Smooth curve between 0 and 1, resembling an "S." Useful for binary classification.
Straight line for positive inputs and flat at zero for negatives. Efficient for deep
ReLU
learning models.
• Loss vs. Iteration Plot: Shows how the loss reduces as the network trains over
epochs.
• Diagrams depicting how gradients are calculated and how weights are adjusted.
• 2D or 3D plots:
• Filters highlight edges, textures, or specific features from the input image.
• Visualizing how the generator creates fake samples and how the discriminator
distinguishes real from fake.
• Progress over epochs can be shown in image generation tasks (e.g., face generation).
Would you like a visual neural network diagram or interactive plotting examples using
Python libraries like Matplotlib and TensorFlow?
an approach to visually represent neural networks using Python and relevant plotting
techniques. This example walks through key concepts and provides visual outputs to help
illustrate components like layers, weights, activations, and outputs.
python
Copy code
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Visualization function
# Draw neurons
# Draw connections
ax.axis('off')
biases_output = np.array([0.05])
• Hidden Layer (Green): Processes information and applies weights, biases, and
activations.
• Connections: The gray dashed lines represent weights between neurons in adjacent
layers.
Analyzed
python
Copy code
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Visualization function
# Draw neurons
# Draw connections
ax.axis('off')
plt.show()
biases_output = np.array([0.05])
• Input Layer: Contains two input neurons (representing the input features).
• Hidden Layer: Includes two neurons in the hidden layer, connected to the input layer
by dashed lines.
To understand the mathematical operations within an ANN, key components like cost
functions, derivatives, chain rule, and gradient descent play a vital role.
1. Cost Function
The cost function quantifies the error between the predicted output and the actual output.
For regression problems, a common choice is the Mean Squared Error (MSE):
Where:
Activation functions introduce non-linearity into the network. Common activation functions
include:
Backpropagation computes gradients for the weights and biases by applying the chain rule
of calculus.
2. Backward Pass: Calculate gradients of the cost function with respect to weights and
biases.
If the output a[L]a^{[L]}a[L] is a function of weights W[L]W^{[L]}W[L] and input xxx, using the
chain rule:
Gradient descent updates weights and biases to minimize the cost function:
Where:
0.2 0.8 0
0.4 0.6 0
0.6 0.4 1
0.8 0.2 1
Problem Example
Step-by-Step Process
a=σ(z)a = \sigma(z)a=σ(z)
2. Compute Cost Function: Calculate the error using MSE or binary cross-entropy.
This process continues iteratively until the cost function converges to a minimum, allowing
the network to learn patterns in the data and make accurate predictions.
Problem Statement
We will use a simple dataset where each point has two features and a binary target (either 0
or 1). The goal is to train a neural network to classify these points correctly using
backpropagation and gradient descent.
1. Dataset Preparation
python
Copy code
import numpy as np
import matplotlib.pyplot as plt
X = np.array([[0.2, 0.8],
[0.4, 0.6],
[0.6, 0.4],
[0.8, 0.2]])
# Plot dataset
plt.title("Training Data")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
python
Copy code
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def sigmoid_derivative(z):
return z * (1 - z)
Copy code
np.random.seed(1)
weights = np.random.randn(2, 1)
bias = np.random.randn(1)
learning_rate = 0.1
epochs = 10000
# Training loop
# Forward propagation
predictions = sigmoid(z)
# Backpropagation
error = predictions - y
if epoch % 1000 == 0:
python
Copy code
plt.title("Decision Boundary")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
Final Output
• Decision Boundary: A visualization will clearly separate points with labels 0 and 1.
Diagram Explanation
The decision boundary plot shows how the neural network separates the data points based
on learned weights and biases.
Extending the Neural Network with More Layers (Deep Learning Concepts)
To build a more powerful neural network, we introduce hidden layers, nonlinear activation
functions, and deeper architectures. Let's break it down step by step using modern libraries
for scalability and efficiency.
1. Hidden Layers: Intermediate layers between input and output to capture complex
patterns.
We will use TensorFlow to create a deep neural network for the same classification problem.
python
Copy code
import numpy as np
import tensorflow as tf
Copy code
X = np.array([[0.2, 0.8],
[0.4, 0.6],
[0.6, 0.4],
[0.8, 0.2]])
python
Copy code
model = Sequential([
])
python
Copy code
plt.plot(history.history['loss'])
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.show()
python
Copy code
grid_predictions = model.predict(grid_points)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
1. Hidden Layers:
3. Optimizers:
4. Model Loss: