Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TensorFlow/Keras Neural Networks for Classification #11572

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
eec42ad
Add __init__.py file
asRot0 Sep 18, 2024
836c8ac
Add __init__.py file
asRot0 Sep 18, 2024
74b6229
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
f4e3b97
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
bc1f9b9
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 18, 2024
96c374a
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
e4dcab8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
1a69f8a
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
860c0c2
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 18, 2024
460ec8f
Merge remote-tracking branch 'origin/master'
asRot0 Sep 18, 2024
99a58be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
c004e54
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 18, 2024
35b13cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
83e5295
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
3a47c23
Merge remote-tracking branch 'origin/master'
asRot0 Sep 18, 2024
aa47df0
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 18, 2024
35ba5df
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
7d69b82
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
2b02162
Merge remote-tracking branch 'origin/master'
asRot0 Sep 18, 2024
2603c8a
fully_connected_mnist.py: For the basic fully connected neural network.
asRot0 Sep 18, 2024
0d9804a
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 18, 2024
037b007
fixed size sliding window technique
asRot0 Sep 19, 2024
947ff7f
Add __init__.py file
asRot0 Sep 19, 2024
8d068c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2024
33b6162
Add __init__.py file
asRot0 Sep 19, 2024
2cae465
Delete data_structures/sliding_window directory
asRot0 Sep 19, 2024
06e3443
cnn_mnist.py: For the Convolutional Neural Network (CNN) model.
asRot0 Sep 19, 2024
490e585
Merge remote-tracking branch 'origin/master'
asRot0 Sep 19, 2024
00f7f7c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
59 changes: 59 additions & 0 deletions data_structures/sliding_window/fixed_size_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Fixed-Size Sliding Window Algorithm

This module contains an implementation of the fixed-size
sliding window algorithm with doctests.

Examples:
>>> max_sum_subarray([1, 2, 3, 4, 5], 3)
12

>>> max_sum_subarray([2, 1, 5, 1, 3, 2], 4)
11
"""


def max_sum_subarray(arr: list[int], k: int) -> int:
asRot0 marked this conversation as resolved.
Show resolved Hide resolved
"""
Find the maximum sum of any subarray of size `k`.

Args:
arr: The input array of integers.
k: The size of the subarray.

Returns:
The maximum sum of a subarray of size `k`.

Raises:
ValueError: If the length of the array is less than `k`.

Examples:
>>> max_sum_subarray([1, 2, 3, 4, 5], 3)
12

>>> max_sum_subarray([2, 1, 5, 1, 3, 2], 4)
11

>>> max_sum_subarray([1, 2], 3)
Traceback (most recent call last):
...
ValueError: Array length must be at least as large as the window size.
"""
if len(arr) < k:
raise ValueError("Array length must be at least as large as the window size.")

max_sum = float("-inf")
window_sum = sum(arr[:k])
max_sum = max(max_sum, window_sum)

for i in range(len(arr) - k):
window_sum = window_sum - arr[i] + arr[i + k]
max_sum = max(max_sum, window_sum)

return max_sum


if __name__ == "__main__":
import doctest

doctest.testmod()
Empty file.
72 changes: 72 additions & 0 deletions machine_learning/neural_networks/cnn_mnist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Convolutional Neural Network (CNN) for MNIST Classification

Goal: This script builds a deep CNN to classify the MNIST dataset using
TensorFlow and Keras. It leverages convolutional layers for feature
extraction and pooling layers for down-sampling, followed by fully
connected layers for classification.

Objectives:
- Load and preprocess MNIST data (reshape for CNN input).
- Build a CNN with multiple convolutional, pooling, and batch normalization layers.
- Train the CNN, evaluate its accuracy, and display model performance.

"""

# import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load and preprocess the MNIST data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the pixel values (0 to 1)
x_train = x_train.reshape(-1, 28, 28, 1).astype("float32") / 255
x_test = x_test.reshape(-1, 28, 28, 1).astype("float32") / 255

# Convert labels to one-hot encoding
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Building the CNN model
model = models.Sequential()

# 1st Convolutional Layer
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.BatchNormalization())

# 2nd Convolutional Layer
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.BatchNormalization())

# 3rd Convolutional Layer
model.add(layers.Conv2D(128, (3, 3), activation="relu"))
model.add(layers.BatchNormalization())

# Flattening the data before fully connected layers
model.add(layers.Flatten())

# Fully Connected (Dense) Layer with Dropout for regularization
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dropout(0.5))

# Output Layer for classification
model.add(layers.Dense(10, activation="softmax"))

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

# Display the model summary
model.summary()

# Train the model
history = model.fit(
x_train, y_train, epochs=5, batch_size=32, validation_data=(x_test, y_test)
)

# Evaluate the model on test data
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"\nTest accuracy: {test_acc}")
77 changes: 77 additions & 0 deletions machine_learning/neural_networks/fully_connected_mnist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Fully Connected Neural Network for MNIST Classification

Goal: This script implements a fully connected feed-forward neural network
using TensorFlow and Keras to classify the MNIST dataset
(28x28 grayscale images of handwritten digits from 0 to 9).
The network has two hidden layers with ReLU activations and dropout
for regularization.

Objectives:
- Normalize and preprocess MNIST data.
- Build a basic neural network with dense layers.
- Train the model, evaluate its accuracy and loss at each epoch,
and predict sample outputs.

"""

# Standard library imports
# (None in this case)

# Third-party imports
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models

# Load the MNIST dataset from Keras
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the images from 0-255 to 0-1 by dividing by 255
x_train, x_test = x_train / 255.0, x_test / 255.0

# Print TensorFlow and Keras information
print(f"TensorFlow Version: {tf.__version__}")
print(f"Keras Layers Module: {layers.__name__}")
print(f"Keras Models Module: {models.__name__}")

# Build a simple Sequential model
model = models.Sequential()

# Flatten the 28x28 images into vectors of length 784
model.add(layers.Flatten(input_shape=(28, 28)))

# First hidden layer with 128 neurons and ReLU activation
model.add(layers.Dense(128, activation="relu"))

# Dropout layer to prevent overfitting (randomly drops 20% of neurons)
model.add(layers.Dropout(0.2))

# Second hidden layer with 64 neurons and ReLU activation
model.add(layers.Dense(64, activation="relu"))

# Output layer with 10 neurons (one for each digit class 0-9),
# softmax for probabilities
model.add(layers.Dense(10, activation="softmax"))

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

# Train the model on the MNIST training data
model.fit(x_train, y_train, epochs=5, batch_size=32)

# Evaluate the model on the test set
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"\nTest accuracy: {test_acc}")

# Make a prediction on a random test image
rng = np.random.default_rng()
random_index = rng.integers(0, len(x_test))
random_image = np.expand_dims(x_test[random_index], axis=0)
prediction = model.predict(random_image)
predicted_digit = np.argmax(prediction)

# Print the predicted result and actual label
print(f"Predicted digit: {predicted_digit}, Actual digit: {y_test[random_index]}")