Added the files.
This commit is contained in:
parent
1dc07ec52f
commit
2dac32eab0
20 changed files with 146 additions and 62 deletions
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="PyUnnecessaryCastInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
7
.idea/misc.xml
generated
Normal file
7
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.13" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
42
.idea/workspace.xml
generated
42
.idea/workspace.xml
generated
|
|
@ -4,35 +4,47 @@
|
||||||
<option name="autoReloadType" value="SELECTIVE" />
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="53d2c8fc-09f6-4596-950a-66eac2662d99" name="Changes" comment="" />
|
<list default="true" id="53d2c8fc-09f6-4596-950a-66eac2662d99" name="Changes" comment="">
|
||||||
|
<change afterPath="$PROJECT_DIR$/experiment-8.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/multilayer-perceptron.py" beforeDir="false" afterPath="$PROJECT_DIR$/multilayer-perceptron.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/results/MLP-output.png" beforeDir="false" afterPath="$PROJECT_DIR$/results/MLP-output.png" afterDir="false" />
|
||||||
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="FileTemplateManagerImpl">
|
||||||
|
<option name="RECENT_TEMPLATES">
|
||||||
|
<list>
|
||||||
|
<option value="Python Script" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
<component name="Git.Settings">
|
<component name="Git.Settings">
|
||||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectColorInfo"><![CDATA[{
|
<component name="ProjectColorInfo">{
|
||||||
"associatedIndex": 7
|
"associatedIndex": 7
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="ProjectId" id="35RSHS8xmtnia7nWZhfabhC6peP" />
|
<component name="ProjectId" id="35RSHS8xmtnia7nWZhfabhC6peP" />
|
||||||
<component name="ProjectViewState">
|
<component name="ProjectViewState">
|
||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||||
"Python.Unnamed.executor": "Run",
|
"Python.Unnamed.executor": "Run",
|
||||||
"Python.multilayer-perceptron.executor": "Run",
|
"Python.multilayer-perceptron.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||||
"RunOnceActivity.git.unshallow": "true",
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"last_opened_file_path": "/home/arctichawk1/Desktop/Projects/Private/Classification-of-Image-Data-with-MLP-and-CNN"
|
"last_opened_file_path": "/home/arctichawk1/Desktop/Projects/Private/Classification-of-Image-Data-with-MLP-and-CNN"
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="SharedIndexes">
|
<component name="SharedIndexes">
|
||||||
<attachedChunks>
|
<attachedChunks>
|
||||||
<set>
|
<set>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
# Classification-of-Image-Data-with-MLP-and-CNN
|
# Classification of Image Data with MLP and CNN
|
||||||
|
|
||||||
This project implements both a multilayer perceptron and a convolutional neural network in Python;
|
This project implements both a multilayer perceptron and a convolutional neural network in Python;
|
||||||
the perceptron comprises an input layer, one or more hidden layers, and an output layer,
|
the perceptron comprises an input layer, one or more hidden layers, and an output layer,
|
||||||
|
|
|
||||||
0
experiment-1-1.py
Normal file
0
experiment-1-1.py
Normal file
0
experiment-1-2.py
Normal file
0
experiment-1-2.py
Normal file
0
experiment-1-3.py
Normal file
0
experiment-1-3.py
Normal file
0
experiment-2-leaky-relu.py
Normal file
0
experiment-2-leaky-relu.py
Normal file
0
experiment-2-tanh.py
Normal file
0
experiment-2-tanh.py
Normal file
0
experiment-3-l1.py
Normal file
0
experiment-3-l1.py
Normal file
0
experiment-3-l2.py
Normal file
0
experiment-3-l2.py
Normal file
0
experiment-4.py
Normal file
0
experiment-4.py
Normal file
0
experiment-5.py
Normal file
0
experiment-5.py
Normal file
0
experiment-6.py
Normal file
0
experiment-6.py
Normal file
0
experiment-7.py
Normal file
0
experiment-7.py
Normal file
0
experiment-8.py
Normal file
0
experiment-8.py
Normal file
|
|
@ -1,8 +1,12 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
from torchvision import datasets
|
from torchvision import datasets
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class MLP:
|
class MLP:
|
||||||
def __init__(self, input_size, hidden_size1, hidden_size2, output_size, weight_scale):
|
def __init__(self, input_size, hidden_size1, hidden_size2, output_size, weight_scale):
|
||||||
|
# initializes weights and biases for each layer
|
||||||
self.W1 = np.random.randn(input_size, hidden_size1) * weight_scale
|
self.W1 = np.random.randn(input_size, hidden_size1) * weight_scale
|
||||||
self.b1 = np.zeros((1, hidden_size1))
|
self.b1 = np.zeros((1, hidden_size1))
|
||||||
self.W2 = np.random.randn(hidden_size1, hidden_size2) * weight_scale
|
self.W2 = np.random.randn(hidden_size1, hidden_size2) * weight_scale
|
||||||
|
|
@ -11,31 +15,35 @@ class MLP:
|
||||||
self.b3 = np.zeros((1, output_size))
|
self.b3 = np.zeros((1, output_size))
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
self.x = x
|
# forwards pass through the network
|
||||||
self.z1 = x @ self.W1 + self.b1
|
self.x = x # input for backpropagation
|
||||||
self.a1 = self.relu(self.z1)
|
self.z1 = x @ self.W1 + self.b1 # linear transformation for layer 1
|
||||||
self.z2 = self.a1 @ self.W2 + self.b2
|
self.a1 = self.relu(self.z1) # ReLU activation
|
||||||
self.a2 = self.relu(self.z2)
|
self.z2 = self.a1 @ self.W2 + self.b2 # linear transformation for layer 2
|
||||||
self.z3 = self.a2 @ self.W3 + self.b3
|
self.a2 = self.relu(self.z2) # ReLU activation
|
||||||
self.a3 = self.softmax(self.z3)
|
self.z3 = self.a2 @ self.W3 + self.b3 # linear transformation for layer 3
|
||||||
return self.a3
|
self.a3 = self.softmax(self.z3) # applies softmax to get class probabilities
|
||||||
|
return self.a3 # output of the network
|
||||||
|
|
||||||
def backward(self, y, lr):
|
def backward(self, y, lr):
|
||||||
|
# backwards pass for weight updates using gradient descent
|
||||||
m = y.shape[0]
|
m = y.shape[0]
|
||||||
y_one_hot = self.one_hot_encode(y, self.W3.shape[1])
|
y_one_hot = self.one_hot_encode(y, self.W3.shape[1]) # converts labels to one-hot encoding
|
||||||
|
|
||||||
dz3 = self.a3 - y_one_hot
|
# computes gradients for each layer
|
||||||
|
dz3 = self.a3 - y_one_hot # gradient for output layer
|
||||||
dw3 = (self.a2.T @ dz3) / m
|
dw3 = (self.a2.T @ dz3) / m
|
||||||
db3 = np.sum(dz3, axis=0, keepdims=True) / m
|
db3 = np.sum(dz3, axis=0, keepdims=True) / m
|
||||||
|
|
||||||
dz2 = (dz3 @ self.W3.T) * self.relu_deriv(self.z2)
|
dz2 = (dz3 @ self.W3.T) * self.relu_deriv(self.z2) # gradient for layer 2
|
||||||
dw2 = (self.a1.T @ dz2) / m
|
dw2 = (self.a1.T @ dz2) / m
|
||||||
db2 = np.sum(dz2, axis=0, keepdims=True) / m
|
db2 = np.sum(dz2, axis=0, keepdims=True) / m
|
||||||
|
|
||||||
dz1 = (dz2 @ self.W2.T) * self.relu_deriv(self.z1)
|
dz1 = (dz2 @ self.W2.T) * self.relu_deriv(self.z1) # gradient for layer 1
|
||||||
dw1 = (self.x.T @ dz1) / m
|
dw1 = (self.x.T @ dz1) / m
|
||||||
db1 = np.sum(dz1, axis=0, keepdims=True) / m
|
db1 = np.sum(dz1, axis=0, keepdims=True) / m
|
||||||
|
|
||||||
|
# updates weights and biases using gradient descent
|
||||||
self.W3 -= lr * dw3
|
self.W3 -= lr * dw3
|
||||||
self.b3 -= lr * db3
|
self.b3 -= lr * db3
|
||||||
self.W2 -= lr * dw2
|
self.W2 -= lr * dw2
|
||||||
|
|
@ -45,32 +53,41 @@ class MLP:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def relu(x):
|
def relu(x):
|
||||||
|
# ReLU activation
|
||||||
return np.maximum(0, x)
|
return np.maximum(0, x)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def relu_deriv(x):
|
def relu_deriv(x):
|
||||||
|
# derivation of ReLU activation for backpropagation
|
||||||
return (x > 0).astype(float)
|
return (x > 0).astype(float)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def softmax(x):
|
def softmax(x):
|
||||||
e_x = np.exp(x - np.max(x, axis=1, keepdims=True))
|
# softmax function normalizes outputs to probabilities
|
||||||
return e_x / np.sum(e_x, axis=1, keepdims=True)
|
e_x = np.exp(x - np.max(x, axis=1, keepdims=True)) # exponentiates inputs
|
||||||
|
return e_x / np.sum(e_x, axis=1, keepdims=True) # normalizes to get probabilities
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def one_hot_encode(y, num_classes):
|
def one_hot_encode(y, num_classes):
|
||||||
|
# converts labels to one-hot encoded format
|
||||||
return np.eye(num_classes)[y]
|
return np.eye(num_classes)[y]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def cross_entropy_loss(y, y_hat):
|
def cross_entropy_loss(y, y_hat):
|
||||||
|
# computes cross-entropy loss between true labels and predicted probabilities
|
||||||
|
m = y.shape[0]
|
||||||
m = y.shape[0]
|
m = y.shape[0]
|
||||||
eps = 1e-12
|
eps = 1e-12
|
||||||
y_hat_clipped = np.clip(y_hat, eps, 1. - eps)
|
y_hat_clipped = np.clip(y_hat, eps, 1. - eps)
|
||||||
log_probs = -np.log(y_hat_clipped[np.arange(m), y])
|
log_probs = -np.log(y_hat_clipped[np.arange(m), y])
|
||||||
return np.mean(log_probs)
|
return np.mean(log_probs)
|
||||||
|
|
||||||
def train_model(self, x_train, y_train, x_val, y_val, lr, epochs, batch_size):
|
def fit(self, x_train, y_train, x_val, y_val, lr, epochs, batch_size):
|
||||||
|
train_losses = []
|
||||||
|
val_accuracies = []
|
||||||
|
|
||||||
for epoch in range(1, epochs + 1):
|
for epoch in range(1, epochs + 1):
|
||||||
perm = np.random.permutation(x_train.shape[0])
|
perm = np.random.permutation(x_train.shape[0]) # Shuffle the training data
|
||||||
x_train_shuffled, y_train_shuffled = x_train[perm], y_train[perm]
|
x_train_shuffled, y_train_shuffled = x_train[perm], y_train[perm]
|
||||||
|
|
||||||
epoch_loss = 0.0
|
epoch_loss = 0.0
|
||||||
|
|
@ -79,37 +96,65 @@ class MLP:
|
||||||
for i in range(num_batches):
|
for i in range(num_batches):
|
||||||
start = i * batch_size
|
start = i * batch_size
|
||||||
end = start + batch_size
|
end = start + batch_size
|
||||||
x_batch = x_train_shuffled[start:end]
|
x_batch = x_train_shuffled[start:end] # batch of inputs
|
||||||
y_batch = y_train_shuffled[start:end]
|
y_batch = y_train_shuffled[start:end] # batch of labels
|
||||||
|
|
||||||
|
# Forward pass, backward pass, and weight update
|
||||||
self.forward(x_batch)
|
self.forward(x_batch)
|
||||||
self.backward(y_batch, lr)
|
self.backward(y_batch, lr)
|
||||||
|
|
||||||
epoch_loss += self.cross_entropy_loss(y_batch, self.a3)
|
epoch_loss += self.cross_entropy_loss(y_batch, self.a3) # updating the epoch loss
|
||||||
|
|
||||||
epoch_loss /= num_batches
|
epoch_loss /= num_batches # average loss is defined
|
||||||
|
train_losses.append(epoch_loss)
|
||||||
|
|
||||||
val_pred = self.predict(x_val)
|
val_pred = self.predict(x_val)
|
||||||
val_acc = np.mean(val_pred == y_val)
|
val_acc = np.mean(val_pred == y_val)
|
||||||
|
val_accuracies.append(val_acc) \
|
||||||
|
|
||||||
print(f"Epoch {epoch:02d} | Training Loss: {epoch_loss:.4f} | Value Accuracy: {val_acc:.4f}")
|
print(f"Epoch {epoch:02d} | Training Loss: {epoch_loss:.4f} | Value Accuracy: {val_acc:.4f}")
|
||||||
|
|
||||||
return val_acc
|
self.plot_graph(train_losses, val_accuracies)
|
||||||
|
return val_accuracies[-1]
|
||||||
|
|
||||||
def predict(self, x):
|
def plot_graph(self, train_losses, val_accuracies):
|
||||||
probs = self.forward(x)
|
if not os.path.exists('results'):
|
||||||
return np.argmax(probs, axis=1)
|
os.makedirs('results')
|
||||||
|
|
||||||
|
fig, ax1 = plt.subplots()
|
||||||
|
|
||||||
|
ax1.set_xlabel('Epochs')
|
||||||
|
ax1.set_ylabel('Training Loss', color='tab:blue')
|
||||||
|
ax1.plot(range(1, len(train_losses) + 1), train_losses, color='tab:blue', label='Training Loss')
|
||||||
|
ax1.tick_params(axis='y', labelcolor='tab:blue')
|
||||||
|
|
||||||
|
ax2 = ax1.twinx()
|
||||||
|
ax2.set_ylabel('Validation Accuracy', color='tab:orange')
|
||||||
|
ax2.plot(range(1, len(val_accuracies) + 1), val_accuracies, color='tab:orange', label='Validation Accuracy')
|
||||||
|
ax2.tick_params(axis='y', labelcolor='tab:orange')
|
||||||
|
|
||||||
|
plt.title('Training Loss and Validation Accuracy over Epochs')
|
||||||
|
|
||||||
|
result_path = 'results/MLP-output.png'
|
||||||
|
fig.savefig(result_path)
|
||||||
|
print(f"Graph saved to: {result_path}")
|
||||||
|
|
||||||
|
def predict(self, x): # predicts class labels for the input data
|
||||||
|
probs = self.forward(x) # forwards pass to get probabilities
|
||||||
|
return np.argmax(probs, axis=1) # returns the class with highest probability
|
||||||
|
|
||||||
|
# acquiring the FashionMNIST dataset
|
||||||
train_set = datasets.FashionMNIST(root='.', train=True, download=True)
|
train_set = datasets.FashionMNIST(root='.', train=True, download=True)
|
||||||
test_set = datasets.FashionMNIST(root='.', train=False, download=True)
|
test_set = datasets.FashionMNIST(root='.', train=False, download=True)
|
||||||
|
|
||||||
|
# preprocessing the data by flattening images and normalizing them.
|
||||||
x_train = train_set.data.numpy().reshape(-1, 28 * 28).astype(np.float32) / 255.0
|
x_train = train_set.data.numpy().reshape(-1, 28 * 28).astype(np.float32) / 255.0
|
||||||
y_train = train_set.targets.numpy()
|
y_train = train_set.targets.numpy()
|
||||||
|
|
||||||
x_test = test_set.data.numpy().reshape(-1, 28 * 28).astype(np.float32) / 255.0
|
x_test = test_set.data.numpy().reshape(-1, 28 * 28).astype(np.float32) / 255.0
|
||||||
y_test = test_set.targets.numpy()
|
y_test = test_set.targets.numpy()
|
||||||
|
|
||||||
|
# MLP Initialization
|
||||||
mlp = MLP(
|
mlp = MLP(
|
||||||
input_size=28 * 28,
|
input_size=28 * 28,
|
||||||
hidden_size1=128,
|
hidden_size1=128,
|
||||||
|
|
@ -118,7 +163,8 @@ mlp = MLP(
|
||||||
weight_scale=1e-2
|
weight_scale=1e-2
|
||||||
)
|
)
|
||||||
|
|
||||||
mlp.train_model(
|
# trains the model
|
||||||
|
mlp.fit(
|
||||||
x_train=x_train,
|
x_train=x_train,
|
||||||
y_train=y_train,
|
y_train=y_train,
|
||||||
x_val=x_test,
|
x_val=x_test,
|
||||||
|
|
@ -128,6 +174,7 @@ mlp.train_model(
|
||||||
batch_size=128
|
batch_size=128
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# tests the model
|
||||||
test_pred = mlp.predict(x_test)
|
test_pred = mlp.predict(x_test)
|
||||||
test_acc = np.mean(test_pred == y_test)
|
test_acc = np.mean(test_pred == y_test)
|
||||||
print(f"\nFinal test accuracy: {test_acc:.4f}")
|
print(f"\nFinal test accuracy: {test_acc:.4f}")
|
||||||
BIN
results/MLP-output.png
Normal file
BIN
results/MLP-output.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
Loading…
Add table
Add a link
Reference in a new issue