{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "ed9e0a13", "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 v2" ] }, { "cell_type": "code", "execution_count": 3, "id": "c838fd53", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100.0%\n", "100.0%\n", "100.0%\n", "100.0%\n" ] } ], "source": [ "training_data = datasets.FashionMNIST(\n", " root=\"data\",\n", " train=True,\n", " download=True,\n", " transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n", ")\n", "\n", "test_data = datasets.FashionMNIST(\n", " root=\"data\",\n", " train=False,\n", " download=True,\n", " transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "id": "744a8ecb", "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": 8, "id": "25639067", "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 = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else \"cpu\"\n", "print(f\"Using {device} device\")\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),\n", " nn.ReLU(),\n", " nn.Linear(512, 512),\n", " nn.ReLU(),\n", " nn.Linear(512, 10)\n", " )\n", " def forward(self, x):\n", " x = self.flatten(x)\n", " logits = self.linear_relu_stack(x)\n", " return logits\n", " \n", "model = NeuralNetwork().to(device)\n", "print(model)" ] }, { "cell_type": "code", "execution_count": null, "id": "4d995f06", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.11.15)", "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.11.15" } }, "nbformat": 4, "nbformat_minor": 5 }