Skip to content

Commit

Permalink
Final Version
Browse files Browse the repository at this point in the history
  • Loading branch information
virgit1 authored Sep 11, 2023
1 parent 6eea9d8 commit 3518f52
Show file tree
Hide file tree
Showing 41 changed files with 22,623 additions and 0 deletions.
2,381 changes: 2,381 additions & 0 deletions Gráficos.ipynb

Large diffs are not rendered by default.

247 changes: 247 additions & 0 deletions IPUTFG.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"id": "f714b90e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31mERROR: Could not find a version that satisfies the requirement poptorch (from versions: none)\u001b[0m\u001b[31m\r\n",
"\u001b[0m\u001b[31mERROR: No matching distribution found for poptorch\u001b[0m\u001b[31m\r\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install poptorch"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "cfd476c8",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'poptorch'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;241m,\u001b[39m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnn\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnn\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpopart\u001b[39;00m\u001b[38;5;241m,\u001b[39m \u001b[38;5;21;01mpoptorch\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msnntorch\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msnn\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msnntorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctional\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mSF\u001b[39;00m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'poptorch'"
]
}
],
"source": [
"import torch, torch.nn as nn\n",
"import popart, poptorch\n",
"import snntorch as snn\n",
"import snntorch.functional as SF"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cdbfc95",
"metadata": {},
"outputs": [],
"source": [
"from torch.utils.data import DataLoader\n",
"from torchvision import datasets, transforms\n",
"\n",
"batch_size = 128\n",
"data_path='/data/mnist'\n",
"\n",
"# Define a transform\n",
"transform = transforms.Compose([\n",
" transforms.Resize((28, 28)),\n",
" transforms.Grayscale(),\n",
" transforms.ToTensor(),\n",
" transforms.Normalize((0,), (1,))])\n",
"\n",
"mnist_train = datasets.MNIST(data_path, train=True, download=True, transform=transform)\n",
"mnist_test = datasets.MNIST(data_path, train=False, download=True, transform=transform)\n",
"\n",
"# Train using full precision 32-flt\n",
"opts = poptorch.Options()\n",
"opts.Precision.halfFloatCasting(poptorch.HalfFloatCastingBehavior.HalfUpcastToFloat)\n",
"\n",
"# Create DataLoaders\n",
"train_loader = poptorch.DataLoader(options=opts, dataset=mnist_train, batch_size=batch_size, shuffle=True, num_workers=20)\n",
"test_loader = poptorch.DataLoader(options=opts, dataset=mnist_test, batch_size=batch_size, shuffle=True, num_workers=20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca06da51",
"metadata": {},
"outputs": [],
"source": [
"num_steps = 25\n",
"beta = 0.9"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ca4a94e",
"metadata": {},
"outputs": [],
"source": [
"class Model(torch.nn.Module):\n",
"def __init__(self):\n",
" super().__init__()\n",
"\n",
" num_inputs = 784\n",
" num_hidden = 1000\n",
" num_outputs = 10\n",
"\n",
" self.fc1 = nn.Linear(num_inputs, num_hidden)\n",
" self.lif1 = snn.Leaky(beta=beta)\n",
" self.fc2 = nn.Linear(num_hidden, num_output)\n",
" self.lif2 = snn.Leaky(beta=beta)\n",
"\n",
" # Cross-Entropy Spike Count Loss\n",
" self.loss_fn = SF.ce_count_loss()\n",
"\n",
"def forward(self, x, labels=None):\n",
" mem1 = self.lif1.init_leaky()\n",
" mem2 = self.lif2.init_leaky()\n",
"\n",
" spk2_rec = []\n",
" mem2_rec = []\n",
"\n",
" for step in range(num_steps):\n",
" cur1 = self.fc1(x.view(batch_size,-1))\n",
" spk1, mem1 = self.lif1(cur1, mem1)\n",
" cur2 = self.fc2(spk1)\n",
" spk2, mem2 = self.lif2(cur2, mem2)\n",
"\n",
" spk2_rec.append(spk2)\n",
" mem2_rec.append(mem2)\n",
"\n",
" spk2_rec = torch.stack(spk2_rec)\n",
" mem2_rec = torch.stack(mem2_rec)\n",
"\n",
" if self.training:\n",
" return spk2_rec, poptorch.identity_loss(self.loss_fn(mem2_rec, labels), \"none\")\n",
" return spk2_rec"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3a02ba6",
"metadata": {},
"outputs": [],
"source": [
"self.fc1 = nn.Linear(num_inputs, num_hidden)\n",
"self.lif1 = snn.Leaky(beta=beta)\n",
"self.fc2 = nn.Linear(num_hidden, num_output)\n",
"self.lif2 = snn.Leaky(beta=beta)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ba8b3a05",
"metadata": {},
"outputs": [],
"source": [
"from snntorch import surrogate\n",
"\n",
"self.lif1 = snn.Leaky(beta=beta, spike_grad = surrogate.fast_sigmoid())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d8c3d12",
"metadata": {},
"outputs": [],
"source": [
"self.loss_fn = SF.ce_count_loss()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71c22d0b",
"metadata": {},
"outputs": [],
"source": [
"mem1 = self.lif1.init_leaky()\n",
"mem2 = self.lif2.init_leaky()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "94b42c2e",
"metadata": {},
"outputs": [],
"source": [
"for step in range(num_steps):\n",
" cur1 = self.fc1(x.view(batch_size,-1))\n",
" spk1, mem1 = self.lif1(cur1, mem1)\n",
" cur2 = self.fc2(spk1)\n",
" spk2, mem2 = self.lif2(cur2, mem2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24a3d65a",
"metadata": {},
"outputs": [],
"source": [
"net = Model()\n",
"optimizer = poptorch.optim.Adam(net.parameters(), lr=0.001, betas=(0.9, 0.999))\n",
"\n",
"poptorch_model = poptorch.trainingModel(net, options=opts, optimizer=optimizer)\n",
"\n",
"epochs = 10\n",
"for epoch in tqdm(range(epochs), desc=\"epochs\"):\n",
" correct = 0.0\n",
"\n",
" for i, (data, labels) in enumerate(train_loader):\n",
" output, loss = poptorch_model(data, labels)\n",
"\n",
" if i % 250 == 0:\n",
" _, pred = output.sum(dim=0).max(1)\n",
" correct = (labels == pred).sum().item()/len(labels)\n",
"\n",
" # Accuracy on a single batch\n",
" print(\"Accuracy: \", correct)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 3518f52

Please sign in to comment.