Skip to content

Instantly share code, notes, and snippets.

@SandroLuck
Created August 15, 2021 17:31
Show Gist options
  • Save SandroLuck/3bb36434a73c559188fe012001181938 to your computer and use it in GitHub Desktop.
Save SandroLuck/3bb36434a73c559188fe012001181938 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "b0d3efd0",
"metadata": {},
"outputs": [],
"source": [
"! pip install --quiet \"pytorch-lightning>=1.3\" \"torchvision\" \"torchmetrics>=0.3\" \"torch>=1.6, <1.9\"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "29eafd96",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"import torch\n",
"from pytorch_lightning import LightningModule, Trainer\n",
"from pytorch_lightning.metrics.functional import accuracy\n",
"from torch import nn\n",
"from torch.nn import functional as F\n",
"from torch.utils.data import DataLoader, random_split\n",
"from torchvision import transforms\n",
"from torchvision.datasets import MNIST\n",
"\n",
"PATH_DATASETS = os.environ.get('PATH_DATASETS', '.')\n",
"AVAIL_GPUS = min(1, torch.cuda.device_count())\n",
"BATCH_SIZE = 256 if AVAIL_GPUS else 16"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bcee96b7",
"metadata": {},
"outputs": [],
"source": [
"class MNISTModel(LightningModule):\n",
"\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.l1 = torch.nn.Linear(28 * 28, 10)\n",
"\n",
" def forward(self, x):\n",
" return torch.relu(self.l1(x.view(x.size(0), -1)))\n",
"\n",
" def training_step(self, batch, batch_nb):\n",
" x, y = batch\n",
" loss = F.cross_entropy(self(x), y)\n",
" return loss\n",
"\n",
" def configure_optimizers(self):\n",
" return torch.optim.Adam(self.parameters(), lr=0.02)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "bba96a2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz\n",
"Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./MNIST/raw/train-images-idx3-ubyte.gz\n",
"Failed to download (trying next):\n",
"HTTP Error 503: Service Unavailable\n",
"\n",
"Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz\n",
"Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz to ./MNIST/raw/train-images-idx3-ubyte.gz\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "47f7aac993aa478a91b51cf890277784",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/9912422 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting ./MNIST/raw/train-images-idx3-ubyte.gz to ./MNIST/raw\n",
"\n",
"Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz\n",
"Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./MNIST/raw/train-labels-idx1-ubyte.gz\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b31ea977d8a845fd9757ae63051bded0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/28881 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting ./MNIST/raw/train-labels-idx1-ubyte.gz to ./MNIST/raw\n",
"\n",
"Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz\n",
"Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./MNIST/raw/t10k-images-idx3-ubyte.gz\n",
"Failed to download (trying next):\n",
"HTTP Error 503: Service Unavailable\n",
"\n",
"Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz\n",
"Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz to ./MNIST/raw/t10k-images-idx3-ubyte.gz\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "deaa96f92916417aaed746569181255e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/1648877 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting ./MNIST/raw/t10k-images-idx3-ubyte.gz to ./MNIST/raw\n",
"\n",
"Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz\n",
"Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./MNIST/raw/t10k-labels-idx1-ubyte.gz\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3ea405871cc842438bbab747aea6ad73",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/4542 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py:502: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:143.)\n",
" return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)\n",
"GPU available: False, used: False\n",
"TPU available: False, using: 0 TPU cores\n",
"IPU available: False, using: 0 IPUs\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting ./MNIST/raw/t10k-labels-idx1-ubyte.gz to ./MNIST/raw\n",
"\n",
"Processing...\n",
"Done!\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
" | Name | Type | Params\n",
"--------------------------------\n",
"0 | l1 | Linear | 7.9 K \n",
"--------------------------------\n",
"7.9 K Trainable params\n",
"0 Non-trainable params\n",
"7.9 K Total params\n",
"0.031 Total estimated model params size (MB)\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c0e6b7643f4a49d89811af7dcd859178",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Training: -1it [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mnist_model = MNISTModel()\n",
"\n",
"# Init DataLoader from MNIST Dataset\n",
"train_ds = MNIST(PATH_DATASETS, train=True, download=True, transform=transforms.ToTensor())\n",
"train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE)\n",
"\n",
"# Initialize a trainer\n",
"trainer = Trainer(\n",
" gpus=AVAIL_GPUS,\n",
" max_epochs=3,\n",
" progress_bar_refresh_rate=20,\n",
")\n",
"\n",
"# Train the model ⚡\n",
"trainer.fit(mnist_model, train_loader)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92e163e3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"environment": {
"name": "tf2-gpu.2-3.m76",
"type": "gcloud",
"uri": "gcr.io/deeplearning-platform-release/tf2-gpu.2-3:m76"
},
"kernelspec": {
"display_name": "Python [conda env:root] *",
"language": "python",
"name": "conda-root-py"
},
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment