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
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ repos:
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
# Black formatter
- repo: https://github.com/psf/black
rev: 24.4.0
hooks:
- id: black
args: ["--line-length=120"]

# Codespell - Fix common misspellings in text files.
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
args: [--write-changes]

# Pyupgrade - automatically upgrade syntax for newer versions of the language.
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py310-plus]
284 changes: 284 additions & 0 deletions Ex1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"from torch import nn\n",
"from torch.utils.data import DataLoader\n",
"from torchvision import datasets\n",
"from torchvision.transforms import ToTensor"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"training_data = datasets.FashionMNIST(\n",
" root=\"data\",\n",
" train=True,\n",
" download=True,\n",
" transform=ToTensor(),\n",
")\n",
"\n",
"test_data = datasets.FashionMNIST(\n",
" root=\"data\",\n",
" train=False,\n",
" download=True,\n",
" transform=ToTensor(),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])\n",
"Shape of y: torch.Size([64]) torch.int64\n"
]
}
],
"source": [
"batch_size = 64\n",
"\n",
"train_dataloader = DataLoader(training_data, batch_size=batch_size)\n",
"test_dataloader = DataLoader(test_data, batch_size=batch_size)\n",
"\n",
"for X, y in test_dataloader:\n",
" print(f\"Shape of X [N, C, H, W]: {X.shape}\")\n",
" print(f\"Shape of y: {y.shape} {y.dtype}\")\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using cuda device\n",
"NeuralNetwork(\n",
" (flatten): Flatten(start_dim=1, end_dim=-1)\n",
" (linear_relu_stack): Sequential(\n",
" (0): Linear(in_features=784, out_features=512, bias=True)\n",
" (1): ReLU()\n",
" (2): Linear(in_features=512, out_features=512, bias=True)\n",
" (3): ReLU()\n",
" (4): Linear(in_features=512, out_features=10, bias=True)\n",
" )\n",
")\n"
]
}
],
"source": [
"device = \"cuda\" if torch.cuda.is_available() else \"mps\" if torch.backends.mps.is_available() else \"cpu\"\n",
"print(f\"Using {device} device\")\n",
"\n",
"\n",
"class NeuralNetwork(nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.flatten = nn.Flatten()\n",
" self.linear_relu_stack = nn.Sequential(\n",
" nn.Linear(28 * 28, 512), nn.ReLU(), nn.Linear(512, 512), nn.ReLU(), nn.Linear(512, 10)\n",
" )\n",
"\n",
" def forward(self, x):\n",
" x = self.flatten(x)\n",
" logits = self.linear_relu_stack(x)\n",
" return logits\n",
"\n",
"\n",
"model = NeuralNetwork().to(device)\n",
"print(model)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"loss_fn = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def train(dataloader, model, loss_fn, optimizer):\n",
" size = len(dataloader.dataset)\n",
" model.train()\n",
" for batch, (X, y) in enumerate(dataloader):\n",
" X, y = X.to(device), y.to(device)\n",
"\n",
" pred = model(X)\n",
" loss = loss_fn(pred, y)\n",
"\n",
" loss.backward()\n",
" optimizer.step()\n",
" optimizer.zero_grad()\n",
"\n",
" if batch % 100 == 0:\n",
" loss, current = loss.item(), (batch + 1) * len(X)\n",
" print(f\"loss: {loss:>7f} [{current:>5d}/{size:>5d}]\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def test(dataloader, model, loss_fn):\n",
" size = len(dataloader.dataset)\n",
" num_batches = len(dataloader)\n",
" model.eval()\n",
" test_loss, correct = 0, 0\n",
" with torch.no_grad():\n",
" for X, y in dataloader:\n",
" X, y = X.to(device), y.to(device)\n",
" pred = model(X)\n",
" test_loss += loss_fn(pred, y).item()\n",
" correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n",
" test_loss /= num_batches\n",
" correct /= size\n",
" print(f\"Test Error: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1\n",
"-------------------------------\n",
"loss: 2.301345 [ 64/60000]\n",
"loss: 2.289013 [ 6464/60000]\n",
"loss: 2.267571 [12864/60000]\n",
"loss: 2.253652 [19264/60000]\n",
"loss: 2.245495 [25664/60000]\n",
"loss: 2.214388 [32064/60000]\n",
"loss: 2.218577 [38464/60000]\n",
"loss: 2.192012 [44864/60000]\n",
"loss: 2.185515 [51264/60000]\n",
"loss: 2.154312 [57664/60000]\n",
"Test Error: \n",
" Accuracy: 46.3%, Avg loss: 2.144304 \n",
"\n",
"Epoch 2\n",
"-------------------------------\n",
"loss: 2.153029 [ 64/60000]\n",
"loss: 2.143812 [ 6464/60000]\n",
"loss: 2.087860 [12864/60000]\n",
"loss: 2.103519 [19264/60000]\n",
"loss: 2.048817 [25664/60000]\n",
"loss: 1.996760 [32064/60000]\n",
"loss: 2.015409 [38464/60000]\n",
"loss: 1.948691 [44864/60000]\n",
"loss: 1.953080 [51264/60000]\n",
"loss: 1.880756 [57664/60000]\n",
"Test Error: \n",
" Accuracy: 60.8%, Avg loss: 1.873655 \n",
"\n",
"Epoch 3\n",
"-------------------------------\n",
"loss: 1.901290 [ 64/60000]\n",
"loss: 1.874183 [ 6464/60000]\n",
"loss: 1.759593 [12864/60000]\n",
"loss: 1.804479 [19264/60000]\n",
"loss: 1.688624 [25664/60000]\n",
"loss: 1.641422 [32064/60000]\n",
"loss: 1.656471 [38464/60000]\n",
"loss: 1.567693 [44864/60000]\n",
"loss: 1.594154 [51264/60000]\n",
"loss: 1.483651 [57664/60000]\n",
"Test Error: \n",
" Accuracy: 63.9%, Avg loss: 1.499217 \n",
"\n",
"Epoch 4\n",
"-------------------------------\n",
"loss: 1.559186 [ 64/60000]\n",
"loss: 1.532048 [ 6464/60000]\n",
"loss: 1.379261 [12864/60000]\n",
"loss: 1.455680 [19264/60000]\n",
"loss: 1.335025 [25664/60000]\n",
"loss: 1.325481 [32064/60000]\n",
"loss: 1.334234 [38464/60000]\n",
"loss: 1.269634 [44864/60000]\n",
"loss: 1.307040 [51264/60000]\n",
"loss: 1.203263 [57664/60000]\n",
"Test Error: \n",
" Accuracy: 64.5%, Avg loss: 1.229295 \n",
"\n",
"Epoch 5\n",
"-------------------------------\n",
"loss: 1.298207 [ 64/60000]\n",
"loss: 1.292620 [ 6464/60000]\n",
"loss: 1.121837 [12864/60000]\n",
"loss: 1.233628 [19264/60000]\n",
"loss: 1.112199 [25664/60000]\n",
"loss: 1.126678 [32064/60000]\n",
"loss: 1.144357 [38464/60000]\n",
"loss: 1.091252 [44864/60000]\n",
"loss: 1.132425 [51264/60000]\n",
"loss: 1.049065 [57664/60000]\n",
"Test Error: \n",
" Accuracy: 65.3%, Avg loss: 1.068460 \n",
"\n",
"Done!\n"
]
}
],
"source": [
"epochs = 5\n",
"for t in range(epochs):\n",
" print(f\"Epoch {t+1}\\n-------------------------------\")\n",
" train(train_dataloader, model, loss_fn, optimizer)\n",
" test(test_dataloader, model, loss_fn)\n",
"print(\"Done!\")"
]
}
],
"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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading