Skip to content

Commit

Permalink
Initialised Bayessian, we are already getting better accurices, still…
Browse files Browse the repository at this point in the history
… have not reached target accuracy but getting there ;)
  • Loading branch information
MartinNguyen03 committed Nov 6, 2024
1 parent ab35ef8 commit 35502bb
Show file tree
Hide file tree
Showing 5 changed files with 640 additions and 14 deletions.
229 changes: 229 additions & 0 deletions Bayessian.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import torch\n",
"import torch.nn as nn\n",
"import torchbnn as bnn # torchbnn library for BNN layers\n",
"from torch.utils.data import DataLoader, TensorDataset, random_split\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import accuracy_score\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class BayesianNN(nn.Module):\n",
" def __init__(self, input_dim):\n",
" super(BayesianNN, self).__init__()\n",
" # Define prior parameters\n",
" prior_mu = 0.0 # Mean of the prior distribution\n",
" prior_sigma = 0.1 # Standard deviation of the prior distribution\n",
" \n",
" # Initialize Bayesian layers with the specified priors\n",
" self.fc1 = bnn.BayesLinear(prior_mu, prior_sigma, in_features=input_dim, out_features=64)\n",
" self.fc2 = bnn.BayesLinear(prior_mu, prior_sigma, in_features=64, out_features=32)\n",
" self.fc3 = bnn.BayesLinear(prior_mu, prior_sigma, in_features=32, out_features=1)\n",
" self.sigmoid = nn.Sigmoid()\n",
" \n",
" def forward(self, x):\n",
" x = torch.relu(self.fc1(x))\n",
" x = torch.relu(self.fc2(x))\n",
" x = self.sigmoid(self.fc3(x))\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Training function\n",
"def train_bayesian_nn(model, loader, criterion, optimizer):\n",
" model.train()\n",
" total_loss = 0\n",
" for X_batch, y_batch in loader:\n",
" optimizer.zero_grad()\n",
" y_pred = model(X_batch)\n",
" loss = criterion(y_pred.squeeze(), y_batch) # Squeeze to match dimensions\n",
" loss.backward()\n",
" optimizer.step()\n",
" total_loss += loss.item()\n",
" return total_loss / len(loader)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Evaluation function\n",
"def evaluate_bayesian_nn(model, loader):\n",
" model.eval()\n",
" all_preds = []\n",
" with torch.no_grad():\n",
" for X_batch, _ in loader:\n",
" y_pred = model(X_batch)\n",
" all_preds.extend(y_pred.round().squeeze().cpu().numpy())\n",
" return np.array(all_preds)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/1 [00:00<?, ?it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1 - Training Loss: 0.6980\n",
"Epoch 2 - Training Loss: 0.6971\n",
"Epoch 3 - Training Loss: 0.6961\n",
"Epoch 4 - Training Loss: 0.6945\n",
"Epoch 5 - Training Loss: 0.6936\n",
"Epoch 6 - Training Loss: 0.6944\n",
"Epoch 7 - Training Loss: 0.6947\n",
"Epoch 8 - Training Loss: 0.6931\n",
"Epoch 9 - Training Loss: 0.6946\n",
"Epoch 10 - Training Loss: 0.6945\n",
"Epoch 11 - Training Loss: 0.6933\n",
"Epoch 12 - Training Loss: 0.6935\n",
"Epoch 13 - Training Loss: 0.6935\n",
"Epoch 14 - Training Loss: 0.6935\n",
"Epoch 15 - Training Loss: 0.6935\n",
"Epoch 16 - Training Loss: 0.6924\n",
"Epoch 17 - Training Loss: 0.6925\n",
"Epoch 18 - Training Loss: 0.6908\n",
"Epoch 19 - Training Loss: 0.6887\n",
"Epoch 20 - Training Loss: 0.6867\n",
"Epoch 21 - Training Loss: 0.6826\n",
"Epoch 22 - Training Loss: 0.6737\n",
"Epoch 23 - Training Loss: 0.6593\n",
"Epoch 24 - Training Loss: 0.6412\n",
"Epoch 25 - Training Loss: 0.6263\n",
"Epoch 26 - Training Loss: 0.6007\n",
"Epoch 27 - Training Loss: 0.5721\n",
"Epoch 28 - Training Loss: 0.5482\n",
"Epoch 29 - Training Loss: 0.5165\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1/1 [00:22<00:00, 22.49s/it]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 30 - Training Loss: 0.4857\n",
"Validation Accuracy for n=9: 0.7744\n",
"Test Accuracy for n=9: 0.7813\n",
"Accuracies across different n values: [(9, 0.7812962962962963)]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"# Function to run BNN for each value of n\n",
"def run_bayesian_nn(n):\n",
" # Load data\n",
" X = np.load(f'Datasets/kryptonite-{n}-X.npy')\n",
" y = np.load(f'Datasets/kryptonite-{n}-y.npy')\n",
" \n",
" # Split data into training, validation, and test sets\n",
" X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.6, random_state=42)\n",
" X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)\n",
" \n",
" # Convert data to PyTorch tensors and create DataLoaders\n",
" X_train, y_train = torch.tensor(X_train, dtype=torch.float32), torch.tensor(y_train, dtype=torch.float32)\n",
" X_val, y_val = torch.tensor(X_val, dtype=torch.float32), torch.tensor(y_val, dtype=torch.float32)\n",
" X_test, y_test = torch.tensor(X_test, dtype=torch.float32), torch.tensor(y_test, dtype=torch.float32)\n",
" \n",
" train_loader = DataLoader(TensorDataset(X_train, y_train), batch_size=32, shuffle=True)\n",
" val_loader = DataLoader(TensorDataset(X_val, y_val), batch_size=32)\n",
" test_loader = DataLoader(TensorDataset(X_test, y_test), batch_size=32)\n",
" \n",
" # Initialize the model, loss function, and optimizer\n",
" input_dim = X_train.shape[1]\n",
" model = BayesianNN(input_dim)\n",
" criterion = nn.BCELoss() # Binary Cross Entropy Loss for binary classification\n",
" optimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n",
" \n",
" # Train the model\n",
" for epoch in range(30): # Adjust epochs as needed\n",
" train_loss = train_bayesian_nn(model, train_loader, criterion, optimizer)\n",
" print(f\"Epoch {epoch+1} - Training Loss: {train_loss:.4f}\")\n",
" \n",
" # Validate the model\n",
" y_val_pred = evaluate_bayesian_nn(model, val_loader)\n",
" val_accuracy = accuracy_score(y_val, y_val_pred)\n",
" print(f\"Validation Accuracy for n={n}: {val_accuracy:.4f}\")\n",
" \n",
" # Test the model\n",
" y_test_pred = evaluate_bayesian_nn(model, test_loader)\n",
" test_accuracy = accuracy_score(y_test, y_test_pred)\n",
" print(f\"Test Accuracy for n={n}: {test_accuracy:.4f}\")\n",
" \n",
" return test_accuracy\n",
"\n",
"# Run for each n value and collect accuracies\n",
"results = []\n",
"possible_n_vals = [9]\n",
"for n in tqdm(possible_n_vals):\n",
" accuracy = run_bayesian_nn(n)\n",
" results.append((n, accuracy))\n",
"\n",
"print(\"Accuracies across different n values:\", results)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 2 additions & 2 deletions Kryptonite-n-GPT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -122,7 +122,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
6 changes: 5 additions & 1 deletion NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ The remainign 10 marks can be earned by performing any **2** of the following ad
### Experimental Results
Students will present the results of their experiments with tables or figures and describe how these plots validate or invalidate their hypothesis. Importantly: text in figures should be no smaller than font size in the main text.
### Discussion
Students will use this section to discuss what they were able to do, highlight any experiments they wish they could have done, and provide an environmental impact assessment of the experiments that they ran
Students will use this section to discuss what they were able to do, highlight any experiments they wish they could have done, and provide an environmental impact assessment of the experiments that they ran


## The Goal ##
![alt text](image.png)
Loading

0 comments on commit 35502bb

Please sign in to comment.