Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@
* [Apriori Algorithm](machine_learning/apriori_algorithm.py)
* [Astar](machine_learning/astar.py)
* [Automatic Differentiation](machine_learning/automatic_differentiation.py)
* [Cnn](machine_learning/cnn.py)
* [Data Transformations](machine_learning/data_transformations.py)
* [Decision Tree](machine_learning/decision_tree.py)
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
Expand All @@ -712,14 +713,17 @@
* [Loss Functions](machine_learning/loss_functions.py)
* Lstm
* [Lstm Prediction](machine_learning/lstm/lstm_prediction.py)
* [Mean Shift](machine_learning/mean_shift.py)
* [Mfcc](machine_learning/mfcc.py)
* [Mini Batch Gradient Descent](machine_learning/mini_batch_gradient_descent.py)
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
* [Naive Bayes Text Classification](machine_learning/naive_bayes_text_classification.py)
* [Polynomial Regression](machine_learning/polynomial_regression.py)
* [Principle Component Analysis](machine_learning/principle_component_analysis.py)
* [Q Learning](machine_learning/q_learning.py)
* [Random Forest Classifier](machine_learning/random_forest_classifier.py)
* [Random Forest Regressor](machine_learning/random_forest_regressor.py)
* [Rmsprop](machine_learning/rmsprop.py)
* [Scoring Functions](machine_learning/scoring_functions.py)
* [Self Organizing Map](machine_learning/self_organizing_map.py)
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
Expand All @@ -736,6 +740,7 @@
* [Arc Length](maths/arc_length.py)
* [Area](maths/area.py)
* [Area Under Curve](maths/area_under_curve.py)
* [Autocorrelation](maths/autocorrelation.py)
* [Average Absolute Deviation](maths/average_absolute_deviation.py)
* [Average Mean](maths/average_mean.py)
* [Average Median](maths/average_median.py)
Expand Down Expand Up @@ -781,6 +786,7 @@
* [Fibonacci](maths/fibonacci.py)
* [Find Max](maths/find_max.py)
* [Find Min](maths/find_min.py)
* [First Fundamental Form](maths/first_fundamental_form.py)
* [Floor](maths/floor.py)
* [Gamma](maths/gamma.py)
* [Gaussian](maths/gaussian.py)
Expand Down Expand Up @@ -843,6 +849,7 @@
* [Square Root](maths/numerical_analysis/square_root.py)
* [Weierstrass Method](maths/numerical_analysis/weierstrass_method.py)
* [Odd Sieve](maths/odd_sieve.py)
* [Padovan Sequence](maths/padovan_sequence.py)
* [Pell Number](maths/pell_number.py)
* [Perfect Cube](maths/perfect_cube.py)
* [Perfect Number](maths/perfect_number.py)
Expand Down Expand Up @@ -872,6 +879,8 @@
* [Reverse Factorial Recursive](maths/reverse_factorial_recursive.py)
* [Segmented Sieve](maths/segmented_sieve.py)
* Series
* [Alternate Harmonic Series](maths/series/alternate_harmonic_series.py)
* [Alternating Harmonic Series](maths/series/alternating_harmonic_series.py)
* [Arithmetic](maths/series/arithmetic.py)
* [Geometric](maths/series/geometric.py)
* [Geometric Series](maths/series/geometric_series.py)
Expand Down Expand Up @@ -911,6 +920,7 @@
* [Polygonal Numbers](maths/special_numbers/polygonal_numbers.py)
* [Pronic Number](maths/special_numbers/pronic_number.py)
* [Proth Number](maths/special_numbers/proth_number.py)
* [Spy Number](maths/special_numbers/spy_number.py)
* [Triangular Numbers](maths/special_numbers/triangular_numbers.py)
* [Trimorphic Number](maths/special_numbers/trimorphic_number.py)
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
Expand Down
67 changes: 67 additions & 0 deletions machine_learning/cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Convolutional Neural Network (CNN) implementation for image classification.

Reference: https://en.wikipedia.org/wiki/Convolutional_neural_network

>>> import numpy as np
>>> model = SimpleCNN(input_shape=(1, 28, 28), num_classes=10)
>>> dummy_input = np.random.rand(1, 28, 28)
>>> output = model.forward(dummy_input)
>>> output.shape
(10,)
"""

import numpy as np


class SimpleCNN:
def __init__(self, input_shape: tuple[int, int, int], num_classes: int) -> None:
"""
Initialize a simple CNN model.

Args:
input_shape: Tuple of (channels, height, width)
num_classes: Number of output classes
"""
self.input_shape = input_shape
self.num_classes = num_classes
rng = np.random.default_rng()
self.filters = rng.normal(0, 0.1, size=(8, input_shape[0], 3, 3)) # 8 filters
self.fc_weights = rng.normal(0, 0.1, size=(8 * 26 * 26, num_classes))

def relu(self, feature_map: np.ndarray) -> np.ndarray:
"""Apply ReLU activation to the feature map."""
return np.maximum(0, feature_map)

def convolve(self, input_tensor: np.ndarray, filters: np.ndarray) -> np.ndarray:
"""Apply convolution operation to the input tensor."""
_, height, width = input_tensor.shape
num_filters, _, fh, fw = filters.shape
output = np.zeros((num_filters, height - fh + 1, width - fw + 1))

for f in range(num_filters):
for i in range(height - fh + 1):
for j in range(width - fw + 1):
region = input_tensor[:, i : i + fh, j : j + fw]
output[f, i, j] = np.sum(region * filters[f])
return output

def flatten(self, feature_map: np.ndarray) -> np.ndarray:
"""Flatten the feature map into a 1D array."""
return feature_map.reshape(-1)

def forward(self, input_tensor: np.ndarray) -> np.ndarray:
"""
Forward pass through the CNN.

Args:
input_tensor: Input image of shape (channels, height, width)

Returns:
Output logits of shape (num_classes,)
"""
conv_out = self.convolve(input_tensor, self.filters)
activated = self.relu(conv_out)
flattened = self.flatten(activated)
logits = flattened @ self.fc_weights
return logits
Loading