Added the experiments 6 to 7 lol.
This commit is contained in:
parent
6df733aab5
commit
d4ae94a2b2
5 changed files with 273 additions and 19 deletions
22
.idea/workspace.xml
generated
22
.idea/workspace.xml
generated
|
|
@ -5,26 +5,8 @@
|
||||||
</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-2.py" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-2-leaky-relu.png" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-2-tanh.png" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-3-l1.png" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-3-l2.png" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-4.png" afterDir="false" />
|
|
||||||
<change afterPath="$PROJECT_DIR$/results/experiment-5.png" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-1.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-1.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/experiment-6-convolutional-neural-network.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-6-convolutional-neural-network.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-2-leaky-relu.py" beforeDir="false" afterPath="$PROJECT_DIR$/convolutional-neural-network-experiment-6.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-2-tanh.py" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-3-l1.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-3-l1.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-3-l2.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-3-l2.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-4.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-4.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-5.py" beforeDir="false" afterPath="$PROJECT_DIR$/experiment-5.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/experiment-6.py" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/results/MLP-output.png" beforeDir="false" afterPath="$PROJECT_DIR$/results/MLP-output.png" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/results/experiment-1-1.png" beforeDir="false" afterPath="$PROJECT_DIR$/results/experiment-1-1.png" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/results/experiment-1-2.png" beforeDir="false" afterPath="$PROJECT_DIR$/results/experiment-1-2.png" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/results/experiment-1-3.png" beforeDir="false" afterPath="$PROJECT_DIR$/results/experiment-1-3.png" afterDir="false" />
|
|
||||||
</list>
|
</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" />
|
||||||
|
|
@ -61,8 +43,10 @@
|
||||||
"Python.experiment-2.executor": "Run",
|
"Python.experiment-2.executor": "Run",
|
||||||
"Python.experiment-3-l1.executor": "Run",
|
"Python.experiment-3-l1.executor": "Run",
|
||||||
"Python.experiment-3-l2.executor": "Run",
|
"Python.experiment-3-l2.executor": "Run",
|
||||||
|
"Python.experiment-3.executor": "Run",
|
||||||
"Python.experiment-4.executor": "Run",
|
"Python.experiment-4.executor": "Run",
|
||||||
"Python.experiment-5.executor": "Run",
|
"Python.experiment-5.executor": "Run",
|
||||||
|
"Python.experiment-6-convolutional-neural-network.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",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,134 @@
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.optim as optim
|
||||||
|
from torchvision import datasets, transforms
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import os
|
||||||
|
|
||||||
|
class CNN:
|
||||||
|
def __init__(self, device=None):
|
||||||
|
self.device = device or torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||||
|
self.model = self._build_model().to(self.device)
|
||||||
|
self.criterion = nn.CrossEntropyLoss() # cross entropy is used for loss from torch
|
||||||
|
self.optimizer = optim.Adam(self.model.parameters(), lr=0.001) # adam is used for optimization from torch
|
||||||
|
|
||||||
|
def _build_model(self):
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(1, 32, 3, padding=1) # convolutional layer 1
|
||||||
|
self.conv2 = nn.Conv2d(32, 64, 3, padding=1) # convolutional layer 2
|
||||||
|
self.fc1 = nn.Linear(64 * 7 * 7, 256) # hidden layer with 256 units
|
||||||
|
self.fc2 = nn.Linear(256, 10) # output layer
|
||||||
|
self.relu = nn.ReLU() # ReLU activation from torch
|
||||||
|
self.pool = nn.MaxPool2d(2, 2) # pooling from torch
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# forwards pass through the network
|
||||||
|
x = self.relu(self.conv1(x))
|
||||||
|
x = self.pool(x)
|
||||||
|
x = self.relu(self.conv2(x))
|
||||||
|
x = self.pool(x)
|
||||||
|
x = x.view(x.size(0), -1)
|
||||||
|
x = self.relu(self.fc1(x))
|
||||||
|
x = self.fc2(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
return Net()
|
||||||
|
|
||||||
|
def fit(self, train_loader, val_loader, epochs=10):
|
||||||
|
self.train_losses = []
|
||||||
|
self.val_accuracies = []
|
||||||
|
|
||||||
|
for epoch in range(1, epochs + 1):
|
||||||
|
self.model.train()
|
||||||
|
epoch_loss = 0.0
|
||||||
|
|
||||||
|
for images, labels in train_loader:
|
||||||
|
images, labels = images.to(self.device), labels.to(self.device)
|
||||||
|
self.optimizer.zero_grad()
|
||||||
|
outputs = self.model(images)
|
||||||
|
loss = self.criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
self.optimizer.step()
|
||||||
|
epoch_loss += loss.item() # updating the epoch loss
|
||||||
|
|
||||||
|
self.train_losses.append(epoch_loss)
|
||||||
|
|
||||||
|
val_acc = self.evaluate(val_loader)
|
||||||
|
self.val_accuracies.append(val_acc)
|
||||||
|
print(f"Epoch {epoch:02d} | Training Loss: {epoch_loss:.4f} | Validation Accuracy: {val_acc:.4f}")
|
||||||
|
|
||||||
|
self.plot_graph(self.train_losses, self.val_accuracies)
|
||||||
|
return self.val_accuracies[-1]
|
||||||
|
|
||||||
|
def evaluate(self, loader):
|
||||||
|
# measures the accuracy of your model on a given dataset
|
||||||
|
self.model.eval()
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
with torch.no_grad():
|
||||||
|
for images, labels in loader:
|
||||||
|
images, labels = images.to(self.device), labels.to(self.device)
|
||||||
|
outputs = self.model(images)
|
||||||
|
preds = torch.argmax(outputs, dim=1)
|
||||||
|
correct += (preds == labels).sum().item()
|
||||||
|
total += labels.size(0)
|
||||||
|
return correct / total
|
||||||
|
|
||||||
|
def plot_graph(self, train_losses, val_accuracies):
|
||||||
|
if not os.path.exists('results'):
|
||||||
|
os.makedirs('results') # creates results director
|
||||||
|
|
||||||
|
fig, ax1 = plt.subplots() # initializes the plot
|
||||||
|
|
||||||
|
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') # defines loss subplot
|
||||||
|
|
||||||
|
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') # defines accuracy subplot
|
||||||
|
|
||||||
|
plt.title('CNN Training Loss and Validation Accuracy over Epochs')
|
||||||
|
|
||||||
|
result_path = 'results/experiment-6-convolutional-neural-network.png' # defines the file name
|
||||||
|
fig.savefig(result_path)
|
||||||
|
print(f"Graph saved to: {result_path}")
|
||||||
|
|
||||||
|
def predict(self, loader):
|
||||||
|
# returns the predicted class labels for all samples in the loader, instead of summarizing them as accuracy.
|
||||||
|
self.model.eval()
|
||||||
|
all_preds = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for images, _ in loader:
|
||||||
|
images = images.to(self.device)
|
||||||
|
outputs = self.model(images)
|
||||||
|
preds = torch.argmax(outputs, dim=1)
|
||||||
|
all_preds.append(preds.cpu())
|
||||||
|
return torch.cat(all_preds)
|
||||||
|
|
||||||
|
# pre-processing the images
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize((0.5,), (0.5,))
|
||||||
|
])
|
||||||
|
|
||||||
|
# acquiring the FashionMNIST dataset
|
||||||
|
train_set = datasets.FashionMNIST(root='.', train=True, download=True, transform=transform)
|
||||||
|
test_set = datasets.FashionMNIST(root='.', train=False, download=True, transform=transform)
|
||||||
|
|
||||||
|
# splits data using dataloader with 256 batches
|
||||||
|
train_loader = DataLoader(train_set, batch_size=256, shuffle=True)
|
||||||
|
test_loader = DataLoader(test_set, batch_size=256, shuffle=False)
|
||||||
|
|
||||||
|
# CNN initialization and training the model
|
||||||
|
cnn = CNN()
|
||||||
|
cnn.fit(train_loader, test_loader, epochs=10)
|
||||||
|
|
||||||
|
# tests the model
|
||||||
|
test_acc = cnn.evaluate(test_loader)
|
||||||
|
print(f"\nFinal test accuracy: {test_acc:.4f}")
|
||||||
136
experiment-7.py
136
experiment-7.py
|
|
@ -0,0 +1,136 @@
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.optim as optim
|
||||||
|
from torchvision import datasets, transforms
|
||||||
|
from torch.utils.data import DataLoader
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import os
|
||||||
|
|
||||||
|
class CNN:
|
||||||
|
def __init__(self, device=None):
|
||||||
|
self.device = device or torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||||
|
self.model = self._build_model().to(self.device)
|
||||||
|
self.criterion = nn.CrossEntropyLoss() # cross entropy is used for loss from torch
|
||||||
|
self.optimizer = optim.Adam(self.model.parameters(), lr=0.001) # adam is used for optimization from torch
|
||||||
|
|
||||||
|
def _build_model(self):
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(1, 32, 3, padding=1) # convolutional layer 1
|
||||||
|
self.conv2 = nn.Conv2d(32, 64, 3, padding=1) # convolutional layer 2
|
||||||
|
self.fc1 = nn.Linear(64 * 7 * 7, 256) # hidden layer with 256 units
|
||||||
|
self.fc2 = nn.Linear(256, 10) # output layer
|
||||||
|
self.relu = nn.ReLU() # ReLU activation from torch
|
||||||
|
self.pool = nn.MaxPool2d(2, 2) # pooling from torch
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
# forwards pass through the network
|
||||||
|
x = self.relu(self.conv1(x))
|
||||||
|
x = self.pool(x)
|
||||||
|
x = self.relu(self.conv2(x))
|
||||||
|
x = self.pool(x)
|
||||||
|
x = x.view(x.size(0), -1)
|
||||||
|
x = self.relu(self.fc1(x))
|
||||||
|
x = self.fc2(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
return Net()
|
||||||
|
|
||||||
|
def fit(self, train_loader, val_loader, epochs=10):
|
||||||
|
self.train_losses = []
|
||||||
|
self.val_accuracies = []
|
||||||
|
|
||||||
|
for epoch in range(1, epochs + 1):
|
||||||
|
self.model.train()
|
||||||
|
epoch_loss = 0.0
|
||||||
|
|
||||||
|
for images, labels in train_loader:
|
||||||
|
images, labels = images.to(self.device), labels.to(self.device)
|
||||||
|
self.optimizer.zero_grad()
|
||||||
|
outputs = self.model(images)
|
||||||
|
loss = self.criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
self.optimizer.step()
|
||||||
|
epoch_loss += loss.item() # updating the epoch loss
|
||||||
|
|
||||||
|
self.train_losses.append(epoch_loss)
|
||||||
|
|
||||||
|
val_acc = self.evaluate(val_loader)
|
||||||
|
self.val_accuracies.append(val_acc)
|
||||||
|
print(f"Epoch {epoch:02d} | Training Loss: {epoch_loss:.4f} | Validation Accuracy: {val_acc:.4f}")
|
||||||
|
|
||||||
|
self.plot_graph(self.train_losses, self.val_accuracies)
|
||||||
|
return self.val_accuracies[-1]
|
||||||
|
|
||||||
|
def evaluate(self, loader):
|
||||||
|
# measures the accuracy of your model on a given dataset
|
||||||
|
self.model.eval()
|
||||||
|
correct = 0
|
||||||
|
total = 0
|
||||||
|
with torch.no_grad():
|
||||||
|
for images, labels in loader:
|
||||||
|
images, labels = images.to(self.device), labels.to(self.device)
|
||||||
|
outputs = self.model(images)
|
||||||
|
preds = torch.argmax(outputs, dim=1)
|
||||||
|
correct += (preds == labels).sum().item()
|
||||||
|
total += labels.size(0)
|
||||||
|
return correct / total
|
||||||
|
|
||||||
|
def plot_graph(self, train_losses, val_accuracies):
|
||||||
|
if not os.path.exists('results'):
|
||||||
|
os.makedirs('results') # creates results director
|
||||||
|
|
||||||
|
fig, ax1 = plt.subplots() # initializes the plot
|
||||||
|
|
||||||
|
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') # defines loss subplot
|
||||||
|
|
||||||
|
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') # defines accuracy subplot
|
||||||
|
|
||||||
|
plt.title('CNN Training Loss and Validation Accuracy over Epochs')
|
||||||
|
|
||||||
|
result_path = 'results/experiment-7' # defines the file name
|
||||||
|
fig.savefig(result_path)
|
||||||
|
print(f"Graph saved to: {result_path}")
|
||||||
|
|
||||||
|
def predict(self, loader):
|
||||||
|
# returns the predicted class labels for all samples in the loader, instead of summarizing them as accuracy.
|
||||||
|
self.model.eval()
|
||||||
|
all_preds = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for images, _ in loader:
|
||||||
|
images = images.to(self.device)
|
||||||
|
outputs = self.model(images)
|
||||||
|
preds = torch.argmax(outputs, dim=1)
|
||||||
|
all_preds.append(preds.cpu())
|
||||||
|
return torch.cat(all_preds)
|
||||||
|
|
||||||
|
# pre-processing the images and defining the data augmentation transformations
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.RandomRotation(20), # random rotations between -20 and 20 degrees
|
||||||
|
transforms.RandomHorizontalFlip(), # random horizontal flip
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize((0.5,), (0.5,))
|
||||||
|
])
|
||||||
|
|
||||||
|
# acquiring the FashionMNIST dataset
|
||||||
|
train_set = datasets.FashionMNIST(root='.', train=True, download=True, transform=transform)
|
||||||
|
test_set = datasets.FashionMNIST(root='.', train=False, download=True, transform=transform)
|
||||||
|
|
||||||
|
# splits data using dataloader with 256 batches
|
||||||
|
train_loader = DataLoader(train_set, batch_size=256, shuffle=True)
|
||||||
|
test_loader = DataLoader(test_set, batch_size=256, shuffle=False)
|
||||||
|
|
||||||
|
# CNN initialization and training the model
|
||||||
|
cnn = CNN()
|
||||||
|
cnn.fit(train_loader, test_loader, epochs=10)
|
||||||
|
|
||||||
|
# tests the model
|
||||||
|
test_acc = cnn.evaluate(test_loader)
|
||||||
|
print(f"\nFinal test accuracy: {test_acc:.4f}")
|
||||||
BIN
results/experiment-6-convolutional-neural-network.png
Normal file
BIN
results/experiment-6-convolutional-neural-network.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
BIN
results/experiment-7.png
Normal file
BIN
results/experiment-7.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
Loading…
Add table
Add a link
Reference in a new issue