diff --git a/pytorch/PyTorch-MNIST-Minimal.ipynb b/pytorch/PyTorch-MNIST-Minimal.ipynb index c83ae8d..a210bed 100644 --- a/pytorch/PyTorch-MNIST-Minimal.ipynb +++ b/pytorch/PyTorch-MNIST-Minimal.ipynb @@ -3,6 +3,7 @@ { "cell_type": "code", "execution_count": null, + "id": "8d8e689d", "metadata": { "ExecuteTime": { "end_time": "2021-03-04T20:07:46.654477Z", @@ -11,12 +12,13 @@ }, "outputs": [], "source": [ - "!pip install torch torchvision prometheus_client --progress-bar off" + "!pip install torchvision prometheus_client --progress-bar off" ] }, { "cell_type": "code", "execution_count": null, + "id": "7f832a27", "metadata": { "ExecuteTime": { "end_time": "2021-03-04T20:08:02.078254Z", @@ -34,6 +36,8 @@ "from torch import nn, optim\n", "from torch.nn import functional as F\n", "from prometheus_client import CollectorRegistry, Gauge, push_to_gateway\n", + "import warnings\n", + "warnings.filterwarnings('ignore')\n", "\n", "transform = transforms.Compose([transforms.ToTensor(),\n", " transforms.Normalize((0.5,), (0.5,)),\n", @@ -103,7 +107,7 @@ " add_metrics_data(current_time, epoch, value_formatter.format(time_post_loop - time_pre_loop),value_formatter.format(step_time_avg * 1000))\n", "\n", "\n", - "pushgateway_url = \"http://prom-push-as-pushgateway.apps.zero.massopen.cloud\"\n", + "#pushgateway_url = \"http://prom-push-as-pushgateway.apps.zero.massopen.cloud\"\n", "registry = CollectorRegistry()\n", "epoch_gauge = Gauge(name='epoch_duration_seconds', documentation='epoch_value is the metric itself, the stuff in the {}s are tags',labelnames=[\"model\",\"framework\",\"date\",\"epoch\"],registry=registry)\n", "step_gauge = Gauge(name='step_during_milliseconds', documentation='step_during_milliseconds is the metric itself, the stuff in the {}s are tags',labelnames=[\"model\",\"framework\",\"date\",\"epoch\"],registry=registry)\n", @@ -111,7 +115,7 @@ "def add_metrics_data(current_time, epoch_num, epoch_value, step_value): \n", " epoch_gauge.labels(\"mnist-minimal\",\"Pytorch\",current_time,epoch_num).set(epoch_value)\n", " step_gauge.labels(\"mnist-minimal\",\"Pytorch\",current_time,epoch_num).set(step_value) \n", - " push_to_gateway(pushgateway_url, job='jupyterhub_load', registry=registry)\n", + " #push_to_gateway(pushgateway_url, job='jupyterhub_load', registry=registry)\n", "\n", "\n", "fit(epochs=5, model=model, loss_func=F.cross_entropy,\n", @@ -139,7 +143,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.5" + "version": "3.6.8" }, "toc": { "base_numbering": 1, @@ -157,4 +161,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} diff --git a/pytorch/fgsm_tutorial.ipynb b/pytorch/fgsm_tutorial.ipynb new file mode 100644 index 0000000..39d5025 --- /dev/null +++ b/pytorch/fgsm_tutorial.ipynb @@ -0,0 +1,324 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "# Default torch version contains a bug triggered by this nb\n", + "!pip install --upgrade torch torchvision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "from __future__ import print_function\n", + "import torch\n", + "import torch.nn as nn\n", + "import torch.nn.functional as F\n", + "import torch.optim as optim\n", + "from torchvision import datasets, transforms\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "\n", + "# NOTE: This is a hack to get around \"User-agent\" limitations when downloading MNIST datasets\n", + "# see, https://github.com/pytorch/vision/issues/3497 for more information\n", + "from six.moves import urllib\n", + "opener = urllib.request.build_opener()\n", + "opener.addheaders = [('User-agent', 'Mozilla/5.0')]\n", + "urllib.request.install_opener(opener)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "epsilons = [0, .05, .1, .15, .2, .25, .3]\n", + "pretrained_model = \"lenet_mnist_model.pth\"\n", + "use_cuda=True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# LeNet Model definition\n", + "class Net(nn.Module):\n", + " def __init__(self):\n", + " super(Net, self).__init__()\n", + " self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n", + " self.conv2 = nn.Conv2d(10, 20, kernel_size=5)\n", + " self.conv2_drop = nn.Dropout2d()\n", + " self.fc1 = nn.Linear(320, 50)\n", + " self.fc2 = nn.Linear(50, 10)\n", + "\n", + " def forward(self, x):\n", + " x = F.relu(F.max_pool2d(self.conv1(x), 2))\n", + " x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))\n", + " x = x.view(-1, 320)\n", + " x = F.relu(self.fc1(x))\n", + " x = F.dropout(x, training=self.training)\n", + " x = self.fc2(x)\n", + " return F.log_softmax(x, dim=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# MNIST Test dataset and dataloader declaration\n", + "# Use contextlib to redirect output to stdout instead of stderr\n", + "# cfr. https://github.com/pytorch/vision/issues/7040\n", + "import contextlib\n", + "import sys\n", + "\n", + "with contextlib.redirect_stderr(sys.stdout):\n", + " test_loader = torch.utils.data.DataLoader(\n", + " datasets.MNIST('../data', train=False, download=True, transform=transforms.Compose([\n", + " transforms.ToTensor(),\n", + " ])), \n", + " batch_size=1, shuffle=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Define what device we are using\n", + "print(\"CUDA Available: \",torch.cuda.is_available())\n", + "device = torch.device(\"cuda\" if (use_cuda and torch.cuda.is_available()) else \"cpu\")\n", + "\n", + "# Initialize the network\n", + "model = Net().to(device)\n", + "\n", + "# Load the pretrained model\n", + "model.load_state_dict(torch.load(pretrained_model, map_location='cpu'))\n", + "\n", + "# Set the model in evaluation mode. In this case this is for the Dropout layers\n", + "model.eval()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# FGSM attack code\n", + "def fgsm_attack(image, epsilon, data_grad):\n", + " # Collect the element-wise sign of the data gradient\n", + " sign_data_grad = data_grad.sign()\n", + " # Create the perturbed image by adjusting each pixel of the input image\n", + " perturbed_image = image + epsilon*sign_data_grad\n", + " # Adding clipping to maintain [0,1] range\n", + " perturbed_image = torch.clamp(perturbed_image, 0, 1)\n", + " # Return the perturbed image\n", + " return perturbed_image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "def test( model, device, test_loader, epsilon ):\n", + "\n", + " # Accuracy counter\n", + " correct = 0\n", + " adv_examples = []\n", + "\n", + " # Loop over all examples in test set\n", + " for data, target in test_loader:\n", + "\n", + " # Send the data and label to the device\n", + " data, target = data.to(device), target.to(device)\n", + "\n", + " # Set requires_grad attribute of tensor. Important for Attack\n", + " data.requires_grad = True\n", + "\n", + " # Forward pass the data through the model\n", + " output = model(data)\n", + " init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability\n", + "\n", + " # If the initial prediction is wrong, dont bother attacking, just move on\n", + " if init_pred.item() != target.item():\n", + " continue\n", + "\n", + " # Calculate the loss\n", + " loss = F.nll_loss(output, target)\n", + "\n", + " # Zero all existing gradients\n", + " model.zero_grad()\n", + "\n", + " # Calculate gradients of model in backward pass\n", + " loss.backward()\n", + "\n", + " # Collect datagrad\n", + " data_grad = data.grad.data\n", + "\n", + " # Call FGSM Attack\n", + " perturbed_data = fgsm_attack(data, epsilon, data_grad)\n", + "\n", + " # Re-classify the perturbed image\n", + " output = model(perturbed_data)\n", + "\n", + " # Check for success\n", + " final_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability\n", + " if final_pred.item() == target.item():\n", + " correct += 1\n", + " # Special case for saving 0 epsilon examples\n", + " if (epsilon == 0) and (len(adv_examples) < 5):\n", + " adv_ex = perturbed_data.squeeze().detach().cpu().numpy()\n", + " adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) )\n", + " else:\n", + " # Save some adv examples for visualization later\n", + " if len(adv_examples) < 5:\n", + " adv_ex = perturbed_data.squeeze().detach().cpu().numpy()\n", + " adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) )\n", + "\n", + " # Calculate final accuracy for this epsilon\n", + " final_acc = correct/float(len(test_loader))\n", + " print(\"Epsilon: {}\\tTest Accuracy = {} / {} = {}\".format(epsilon, correct, len(test_loader), final_acc))\n", + "\n", + " # Return the accuracy and an adversarial example\n", + " return final_acc, adv_examples" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "accuracies = []\n", + "examples = []\n", + "\n", + "# Run test for each epsilon\n", + "for eps in epsilons:\n", + " acc, ex = test(model, device, test_loader, eps)\n", + " accuracies.append(acc)\n", + " examples.append(ex)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "plt.figure(figsize=(5,5))\n", + "plt.plot(epsilons, accuracies, \"*-\")\n", + "plt.yticks(np.arange(0, 1.1, step=0.1))\n", + "plt.xticks(np.arange(0, .35, step=0.05))\n", + "plt.title(\"Accuracy vs Epsilon\")\n", + "plt.xlabel(\"Epsilon\")\n", + "plt.ylabel(\"Accuracy\")\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# Plot several examples of adversarial samples at each epsilon\n", + "cnt = 0\n", + "plt.figure(figsize=(8,10))\n", + "for i in range(len(epsilons)):\n", + " for j in range(len(examples[i])):\n", + " cnt += 1\n", + " plt.subplot(len(epsilons),len(examples[0]),cnt)\n", + " plt.xticks([], [])\n", + " plt.yticks([], [])\n", + " if j == 0:\n", + " plt.ylabel(\"Eps: {}\".format(epsilons[i]), fontsize=14)\n", + " orig,adv,ex = examples[i][j]\n", + " plt.title(\"{} -> {}\".format(orig, adv))\n", + " plt.imshow(ex, cmap=\"gray\")\n", + "plt.tight_layout()\n", + "plt.show()" + ] + } + ], + "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.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/pytorch/lenet_mnist_model.pth b/pytorch/lenet_mnist_model.pth new file mode 100644 index 0000000..571e03a Binary files /dev/null and b/pytorch/lenet_mnist_model.pth differ diff --git a/tensorflow/GPU-no-warnings.ipynb b/tensorflow/GPU-no-warnings.ipynb new file mode 100644 index 0000000..667e540 --- /dev/null +++ b/tensorflow/GPU-no-warnings.ipynb @@ -0,0 +1,134 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "ad5eae44", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install tensorflow_datasets protobuf==3.19.6 prometheus_client --progress-bar off" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ff2c1f8", + "metadata": { + "ExecuteTime": { + "end_time": "2021-03-04T15:32:58.177302Z", + "start_time": "2021-03-04T15:31:59.263480Z" + }, + "scrolled": true + }, + "outputs": [], + "source": [ + "import sys\n", + "import contextlib\n", + "import os\n", + "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n", + "\n", + "with contextlib.redirect_stderr(sys.stdout):\n", + " import tensorflow as tf\n", + " import tensorflow_datasets as tfds\n", + "\n", + " tfds.disable_progress_bar()\n", + "\n", + " (mnist_train, mnist_test), ds_info = tfds.load(\n", + " 'mnist',\n", + " split=['train', 'test'],\n", + " shuffle_files=True,\n", + " as_supervised=True,\n", + " with_info=True,\n", + " )\n", + "\n", + "\n", + " def normalize_img(image, label):\n", + " \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n", + " return tf.cast(image, tf.float32) / 255.0, label\n", + "\n", + "\n", + " mnist_train = mnist_train.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + " mnist_train = mnist_train.cache() \\\n", + " .shuffle(ds_info.splits['train'].num_examples) \\\n", + " .batch(128) \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + " mnist_test = mnist_test.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + " mnist_test = mnist_test.batch(128) \\\n", + " .cache() \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + "\n", + " model = tf.keras.models.Sequential([\n", + " tf.keras.layers.Conv2D(\n", + " filters=32, kernel_size=(2, 2), input_shape=(28, 28, 1)),\n", + " tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=128, activation=tf.nn.relu),\n", + " tf.keras.layers.Dropout(rate=0.2),\n", + " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", + " ])\n", + "\n", + "\n", + " model.compile(\n", + " optimizer=tf.keras.optimizers.Adam(0.001),\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n", + " )\n", + "\n", + " model.summary()\n", + "\n", + " model.fit(mnist_train, epochs=5, validation_data=mnist_test)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "348fd0d3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "finalized": { + "timestamp": 1614876075124, + "trusted": true + }, + "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.9.16" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tensorflow/GPU-with-warnings.ipynb b/tensorflow/GPU-with-warnings.ipynb new file mode 100644 index 0000000..a6ff8ae --- /dev/null +++ b/tensorflow/GPU-with-warnings.ipynb @@ -0,0 +1,245 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "1c02700a", + "metadata": { + "ExecuteTime": { + "end_time": "2021-03-04T20:07:47.647809Z", + "start_time": "2021-03-04T20:07:47.622673Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: pip in /home/lgiorgi/.local/lib/python3.6/site-packages (21.2.3)\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: tensorflow in /home/lgiorgi/.local/lib/python3.6/site-packages (2.5.0)\n", + "Requirement already satisfied: tensorflow_datasets in /home/lgiorgi/.local/lib/python3.6/site-packages (4.4.0)\n", + "Requirement already satisfied: prometheus_client in /usr/local/lib/python3.6/site-packages (0.10.1)\n", + "Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow) (1.1.0)\n", + "Requirement already satisfied: wrapt~=1.12.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.12.1)\n", + "Requirement already satisfied: keras-preprocessing~=1.1.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.1.2)\n", + "Requirement already satisfied: opt-einsum~=3.3.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (3.3.0)\n", + "Requirement already satisfied: h5py~=3.1.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (3.1.0)\n", + "Requirement already satisfied: typing-extensions~=3.7.4 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (3.7.4.3)\n", + "Requirement already satisfied: gast==0.4.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (0.4.0)\n", + "Requirement already satisfied: keras-nightly~=2.5.0.dev in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (2.5.0.dev2021032900)\n", + "Requirement already satisfied: absl-py~=0.10 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (0.12.0)\n", + "Requirement already satisfied: tensorboard~=2.5 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (2.5.0)\n", + "Requirement already satisfied: grpcio~=1.34.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.34.1)\n", + "Requirement already satisfied: numpy~=1.19.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.19.5)\n", + "Requirement already satisfied: protobuf>=3.9.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (3.17.0)\n", + "Requirement already satisfied: google-pasta~=0.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (0.2.0)\n", + "Requirement already satisfied: tensorflow-estimator<2.6.0,>=2.5.0rc0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (2.5.0)\n", + "Requirement already satisfied: astunparse~=1.6.3 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.6.3)\n", + "Requirement already satisfied: flatbuffers~=1.12.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.12)\n", + "Requirement already satisfied: wheel~=0.35 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (0.36.2)\n", + "Requirement already satisfied: six~=1.15.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow) (1.15.0)\n", + "Requirement already satisfied: attrs>=18.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow_datasets) (21.2.0)\n", + "Requirement already satisfied: dill in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.3.4)\n", + "Requirement already satisfied: tensorflow-metadata in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (1.2.0)\n", + "Requirement already satisfied: requests>=2.19.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (2.23.0)\n", + "Requirement already satisfied: future in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.18.2)\n", + "Requirement already satisfied: promise in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (2.3)\n", + "Requirement already satisfied: dataclasses in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.8)\n", + "Requirement already satisfied: importlib-resources in /usr/local/lib/python3.6/site-packages (from tensorflow_datasets) (5.2.0)\n", + "Requirement already satisfied: tqdm in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (4.62.0)\n", + "Requirement already satisfied: cached-property in /home/lgiorgi/.local/lib/python3.6/site-packages (from h5py~=3.1.0->tensorflow) (1.5.2)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (2020.4.5.1)\n", + "Requirement already satisfied: idna<3,>=2.5 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (2.9)\n", + "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (3.0.4)\n", + "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (1.25.8)\n", + "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (1.8.0)\n", + "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (0.6.1)\n", + "Requirement already satisfied: setuptools>=41.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (56.2.0)\n", + "Requirement already satisfied: werkzeug>=0.11.15 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (2.0.0)\n", + "Requirement already satisfied: markdown>=2.6.8 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (3.3.4)\n", + "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (0.4.4)\n", + "Requirement already satisfied: google-auth<2,>=1.6.3 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.5->tensorflow) (1.30.0)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (0.2.8)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (4.7.2)\n", + "Requirement already satisfied: cachetools<5.0,>=2.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (4.2.2)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.5->tensorflow) (1.3.0)\n", + "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from markdown>=2.6.8->tensorboard~=2.5->tensorflow) (4.0.1)\n", + "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /home/lgiorgi/.local/lib/python3.6/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard~=2.5->tensorflow) (0.4.8)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.5->tensorflow) (3.1.0)\n", + "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->markdown>=2.6.8->tensorboard~=2.5->tensorflow) (3.4.1)\n", + "Requirement already satisfied: googleapis-common-protos<2,>=1.52.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-metadata->tensorflow_datasets) (1.53.0)\n" + ] + } + ], + "source": [ + "!python -m pip install --upgrade pip\n", + "!pip install tensorflow tensorflow_datasets prometheus_client --progress-bar off" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6ff2c1f8", + "metadata": { + "ExecuteTime": { + "end_time": "2021-03-04T15:32:58.177302Z", + "start_time": "2021-03-04T15:31:59.263480Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model: \"sequential\"\n", + "_________________________________________________________________\n", + "Layer (type) Output Shape Param # \n", + "=================================================================\n", + "conv2d (Conv2D) (None, 27, 27, 32) 160 \n", + "_________________________________________________________________\n", + "max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 \n", + "_________________________________________________________________\n", + "flatten (Flatten) (None, 5408) 0 \n", + "_________________________________________________________________\n", + "dense (Dense) (None, 128) 692352 \n", + "_________________________________________________________________\n", + "dropout (Dropout) (None, 128) 0 \n", + "_________________________________________________________________\n", + "dense_1 (Dense) (None, 10) 1290 \n", + "=================================================================\n", + "Total params: 693,802\n", + "Trainable params: 693,802\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n", + "Epoch 1/5\n", + "469/469 [==============================] - 9s 17ms/step - loss: 1.5838 - sparse_categorical_accuracy: 0.8913 - val_loss: 1.5190 - val_sparse_categorical_accuracy: 0.9460\n", + "Epoch 2/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.5159 - sparse_categorical_accuracy: 0.9493 - val_loss: 1.5012 - val_sparse_categorical_accuracy: 0.9620\n", + "Epoch 3/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.5026 - sparse_categorical_accuracy: 0.9615 - val_loss: 1.4925 - val_sparse_categorical_accuracy: 0.9709\n", + "Epoch 4/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.4946 - sparse_categorical_accuracy: 0.9692 - val_loss: 1.4914 - val_sparse_categorical_accuracy: 0.9720\n", + "Epoch 5/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.4895 - sparse_categorical_accuracy: 0.9741 - val_loss: 1.4857 - val_sparse_categorical_accuracy: 0.9776\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import tensorflow as tf\n", + "import tensorflow_datasets as tfds\n", + "import warnings\n", + "\n", + "tfds.disable_progress_bar()\n", + "\n", + "(mnist_train, mnist_test), ds_info = tfds.load(\n", + " 'mnist',\n", + " split=['train', 'test'],\n", + " shuffle_files=True,\n", + " as_supervised=True,\n", + " with_info=True,\n", + ")\n", + "\n", + "\n", + "def normalize_img(image, label):\n", + " \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n", + " return tf.cast(image, tf.float32) / 255.0, label\n", + "\n", + "\n", + "mnist_train = mnist_train.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + "mnist_train = mnist_train.cache() \\\n", + " .shuffle(ds_info.splits['train'].num_examples) \\\n", + " .batch(128) \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + "mnist_test = mnist_test.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + "mnist_test = mnist_test.batch(128) \\\n", + " .cache() \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + "\n", + "model = tf.keras.models.Sequential([\n", + " tf.keras.layers.Conv2D(\n", + " filters=32, kernel_size=(2, 2), input_shape=(28, 28, 1)),\n", + " tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=128, activation=tf.nn.relu),\n", + " tf.keras.layers.Dropout(rate=0.2),\n", + " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", + "])\n", + "\n", + "\n", + "model.compile(\n", + " optimizer=tf.keras.optimizers.Adam(0.001),\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n", + ")\n", + "\n", + "model.summary()\n", + "\n", + "\n", + " \n", + "model.fit(mnist_train, epochs=5, validation_data=mnist_test)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "348fd0d3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "finalized": { + "timestamp": 1614876075124, + "trusted": true + }, + "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.6.8" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tensorflow/TensorFlow-MNIST-Minimal.ipynb b/tensorflow/TensorFlow-MNIST-Minimal.ipynb deleted file mode 100644 index d3da3f0..0000000 --- a/tensorflow/TensorFlow-MNIST-Minimal.ipynb +++ /dev/null @@ -1,165 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2021-03-04T20:07:47.647809Z", - "start_time": "2021-03-04T20:07:47.622673Z" - } - }, - "outputs": [], - "source": [ - "!pip install tensorflow tensorflow_datasets prometheus_client --progress-bar off" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "ExecuteTime": { - "end_time": "2021-03-04T15:32:58.177302Z", - "start_time": "2021-03-04T15:31:59.263480Z" - } - }, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "import tensorflow_datasets as tfds\n", - "\n", - "tfds.disable_progress_bar()\n", - "\n", - "(mnist_train, mnist_test), ds_info = tfds.load(\n", - " 'mnist',\n", - " split=['train', 'test'],\n", - " shuffle_files=True,\n", - " as_supervised=True,\n", - " with_info=True,\n", - ")\n", - "\n", - "\n", - "def normalize_img(image, label):\n", - " \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n", - " return tf.cast(image, tf.float32) / 255.0, label\n", - "\n", - "\n", - "mnist_train = mnist_train.map(\n", - " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", - "mnist_train = mnist_train.cache() \\\n", - " .shuffle(ds_info.splits['train'].num_examples) \\\n", - " .batch(128) \\\n", - " .prefetch(tf.data.experimental.AUTOTUNE)\n", - "\n", - "mnist_test = mnist_test.map(\n", - " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", - "mnist_test = mnist_test.batch(128) \\\n", - " .cache() \\\n", - " .prefetch(tf.data.experimental.AUTOTUNE)\n", - "\n", - "\n", - "model = tf.keras.models.Sequential([\n", - " tf.keras.layers.Conv2D(\n", - " filters=32, kernel_size=(2, 2), input_shape=(28, 28, 1)),\n", - " tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),\n", - " tf.keras.layers.Flatten(),\n", - " tf.keras.layers.Dense(units=128, activation=tf.nn.relu),\n", - " tf.keras.layers.Dropout(rate=0.2),\n", - " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", - "])\n", - "\n", - "\n", - "model.compile(\n", - " optimizer=tf.keras.optimizers.Adam(0.001),\n", - " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", - " metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n", - ")\n", - "\n", - "model.summary()\n", - "\n", - "import time\n", - "from prometheus_client import CollectorRegistry, Gauge, push_to_gateway\n", - "class SendMetrics(tf.keras.callbacks.Callback):\n", - " def __init__(self):\n", - " self.epoch_start_time = 0\n", - " self.batchstart = 0\n", - " self.batchtimes = []\n", - " self.current_time=time.time()\n", - " \n", - " self.pushgateway_url = \"http://prom-push-as-pushgateway.apps.zero.massopen.cloud\"\n", - " self.registry = CollectorRegistry()\n", - " self.epoch_gauge = Gauge(name='epoch_duration_seconds', documentation='epoch_value is the metric itself, the stuff in the {}s are tags',labelnames=[\"model\",\"framework\",\"date\",\"epoch\"],registry=self.registry)\n", - " self.step_gauge = Gauge(name='step_during_milliseconds', documentation='step_during_milliseconds is the metric itself, the stuff in the {}s are tags',labelnames=[\"model\",\"framework\",\"date\",\"epoch\"],registry=self.registry)\n", - "\n", - " def on_epoch_begin(self, epoch, logs=None):\n", - " self.epoch_start_time=tf.timestamp()\n", - " \n", - " def on_epoch_end(self, epoch, logs=None):\n", - " keys = list(logs.keys())\n", - " total=0\n", - " count=0\n", - " for x in self.batchtimes:\n", - " total=total+x\n", - " count=count+1\n", - " \n", - " value_formatter = \"{0:.2f}\"\n", - " epoch_value = value_formatter.format(tf.timestamp() - self.epoch_start_time)\n", - " step_value = value_formatter.format(total/count*1000)\n", - " print(\"epoch={}, epoch time={}s, step time={}ms\".format(epoch, epoch_value, step_value))\n", - " self.add_metrics_data(epoch,epoch_value,step_value)\n", - " \n", - " def on_train_batch_begin(self, batch, logs=None):\n", - " self.batchstart=tf.timestamp()\n", - " \n", - "\n", - " def on_train_batch_end(self, batch, logs=None):\n", - " self.batchtimes.append(tf.timestamp()- self.batchstart) \n", - " \n", - " def add_metrics_data(self, epoch_num, epoch_value, step_value): \n", - " self.epoch_gauge.labels(\"mnist-minimal\",\"Tensorflow\",self.current_time,epoch_num).set(epoch_value)\n", - " self.step_gauge.labels(\"mnist-minimal\",\"Tensorflow\",self.current_time,epoch_num).set(step_value) \n", - " push_to_gateway(self.pushgateway_url, job='jupyterhub_load', registry=self.registry)\n", - " \n", - "model.fit(mnist_train, epochs=5, validation_data=mnist_test, callbacks=[SendMetrics()])\n" - ] - } - ], - "metadata": { - "finalized": { - "timestamp": 1614876075124, - "trusted": true - }, - "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.5" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": true, - "toc_window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file diff --git a/tensorflow/tensorflow-cpu_no-warnings.ipynb b/tensorflow/tensorflow-cpu_no-warnings.ipynb new file mode 100644 index 0000000..d01b52d --- /dev/null +++ b/tensorflow/tensorflow-cpu_no-warnings.ipynb @@ -0,0 +1,251 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 5, + "id": "1c02700a", + "metadata": { + "ExecuteTime": { + "end_time": "2021-03-04T20:07:47.647809Z", + "start_time": "2021-03-04T20:07:47.622673Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: pip in /home/lgiorgi/.local/lib/python3.6/site-packages (21.2.4)\n", + "\u001b[33mWARNING: Skipping tensorflow as it is not installed.\u001b[0m\n", + "\u001b[33mWARNING: Skipping tensorflow-gpu as it is not installed.\u001b[0m\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: tensorflow-cpu in /home/lgiorgi/.local/lib/python3.6/site-packages (2.6.0)\n", + "Requirement already satisfied: tensorflow_datasets in /home/lgiorgi/.local/lib/python3.6/site-packages (4.4.0)\n", + "Requirement already satisfied: prometheus_client in /usr/local/lib/python3.6/site-packages (0.10.1)\n", + "Requirement already satisfied: absl-py~=0.10 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (0.12.0)\n", + "Requirement already satisfied: opt-einsum~=3.3.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (3.3.0)\n", + "Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow-cpu) (1.1.0)\n", + "Requirement already satisfied: flatbuffers~=1.12.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.12)\n", + "Requirement already satisfied: google-pasta~=0.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (0.2.0)\n", + "Requirement already satisfied: six~=1.15.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.15.0)\n", + "Requirement already satisfied: keras~=2.6 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (2.6.0)\n", + "Requirement already satisfied: wrapt~=1.12.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.12.1)\n", + "Requirement already satisfied: keras-preprocessing~=1.1.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.1.2)\n", + "Requirement already satisfied: typing-extensions~=3.7.4 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (3.7.4.3)\n", + "Requirement already satisfied: protobuf>=3.9.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (3.17.0)\n", + "Requirement already satisfied: tensorboard~=2.6 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (2.6.0)\n", + "Requirement already satisfied: gast==0.4.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (0.4.0)\n", + "Requirement already satisfied: wheel~=0.35 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (0.36.2)\n", + "Requirement already satisfied: grpcio<2.0,>=1.37.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.39.0)\n", + "Requirement already satisfied: astunparse~=1.6.3 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.6.3)\n", + "Requirement already satisfied: clang~=5.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (5.0)\n", + "Requirement already satisfied: h5py~=3.1.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (3.1.0)\n", + "Requirement already satisfied: tensorflow-estimator~=2.6 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (2.6.0)\n", + "Requirement already satisfied: numpy~=1.19.2 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-cpu) (1.19.5)\n", + "Requirement already satisfied: future in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.18.2)\n", + "Requirement already satisfied: promise in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (2.3)\n", + "Requirement already satisfied: tensorflow-metadata in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (1.2.0)\n", + "Requirement already satisfied: requests>=2.19.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (2.23.0)\n", + "Requirement already satisfied: attrs>=18.1.0 in /usr/local/lib/python3.6/site-packages (from tensorflow_datasets) (21.2.0)\n", + "Requirement already satisfied: dataclasses in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.8)\n", + "Requirement already satisfied: dill in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (0.3.4)\n", + "Requirement already satisfied: importlib-resources in /usr/local/lib/python3.6/site-packages (from tensorflow_datasets) (5.2.0)\n", + "Requirement already satisfied: tqdm in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow_datasets) (4.62.0)\n", + "Requirement already satisfied: cached-property in /home/lgiorgi/.local/lib/python3.6/site-packages (from h5py~=3.1.0->tensorflow-cpu) (1.5.2)\n", + "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (1.25.8)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (2020.4.5.1)\n", + "Requirement already satisfied: idna<3,>=2.5 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (2.9)\n", + "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/lib/python3.6/site-packages (from requests>=2.19.0->tensorflow_datasets) (3.0.4)\n", + "Requirement already satisfied: setuptools>=41.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (56.2.0)\n", + "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (0.6.1)\n", + "Requirement already satisfied: markdown>=2.6.8 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (3.3.4)\n", + "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (0.4.4)\n", + "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (1.8.0)\n", + "Requirement already satisfied: google-auth<2,>=1.6.3 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (1.30.0)\n", + "Requirement already satisfied: werkzeug>=0.11.15 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorboard~=2.6->tensorflow-cpu) (2.0.0)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.6->tensorflow-cpu) (0.2.8)\n", + "Requirement already satisfied: cachetools<5.0,>=2.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.6->tensorflow-cpu) (4.2.2)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth<2,>=1.6.3->tensorboard~=2.6->tensorflow-cpu) (4.7.2)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.6->tensorflow-cpu) (1.3.0)\n", + "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.6/site-packages (from markdown>=2.6.8->tensorboard~=2.6->tensorflow-cpu) (4.0.1)\n", + "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /home/lgiorgi/.local/lib/python3.6/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard~=2.6->tensorflow-cpu) (0.4.8)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard~=2.6->tensorflow-cpu) (3.1.0)\n", + "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/site-packages (from importlib-metadata->markdown>=2.6.8->tensorboard~=2.6->tensorflow-cpu) (3.4.1)\n", + "Requirement already satisfied: googleapis-common-protos<2,>=1.52.0 in /home/lgiorgi/.local/lib/python3.6/site-packages (from tensorflow-metadata->tensorflow_datasets) (1.53.0)\n" + ] + } + ], + "source": [ + "!python -m pip install --upgrade pip\n", + "!pip uninstall tensorflow tensorflow-gpu -y\n", + "!pip install tensorflow-cpu tensorflow_datasets prometheus_client --progress-bar off" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6ff2c1f8", + "metadata": { + "ExecuteTime": { + "end_time": "2021-03-04T15:32:58.177302Z", + "start_time": "2021-03-04T15:31:59.263480Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model: \"sequential\"\n", + "_________________________________________________________________\n", + "Layer (type) Output Shape Param # \n", + "=================================================================\n", + "conv2d (Conv2D) (None, 27, 27, 32) 160 \n", + "_________________________________________________________________\n", + "max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 \n", + "_________________________________________________________________\n", + "flatten (Flatten) (None, 5408) 0 \n", + "_________________________________________________________________\n", + "dense (Dense) (None, 128) 692352 \n", + "_________________________________________________________________\n", + "dropout (Dropout) (None, 128) 0 \n", + "_________________________________________________________________\n", + "dense_1 (Dense) (None, 10) 1290 \n", + "=================================================================\n", + "Total params: 693,802\n", + "Trainable params: 693,802\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n", + "Epoch 1/5\n", + "469/469 [==============================] - 10s 18ms/step - loss: 1.5879 - sparse_categorical_accuracy: 0.8887 - val_loss: 1.5238 - val_sparse_categorical_accuracy: 0.9421\n", + "Epoch 2/5\n", + "469/469 [==============================] - 9s 18ms/step - loss: 1.5191 - sparse_categorical_accuracy: 0.9465 - val_loss: 1.5060 - val_sparse_categorical_accuracy: 0.9579\n", + "Epoch 3/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.5050 - sparse_categorical_accuracy: 0.9587 - val_loss: 1.4946 - val_sparse_categorical_accuracy: 0.9691\n", + "Epoch 4/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.4960 - sparse_categorical_accuracy: 0.9680 - val_loss: 1.4905 - val_sparse_categorical_accuracy: 0.9720\n", + "Epoch 5/5\n", + "469/469 [==============================] - 8s 17ms/step - loss: 1.4905 - sparse_categorical_accuracy: 0.9730 - val_loss: 1.4881 - val_sparse_categorical_accuracy: 0.9752\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import tensorflow as tf\n", + "import tensorflow_datasets as tfds\n", + "import warnings\n", + "#warnings.filterwarnings('ignore')\n", + "#tf.get_logger().setLevel(\"FATAL\")\n", + "\n", + "tfds.disable_progress_bar()\n", + "\n", + "(mnist_train, mnist_test), ds_info = tfds.load(\n", + " 'mnist',\n", + " split=['train', 'test'],\n", + " shuffle_files=True,\n", + " as_supervised=True,\n", + " with_info=True,\n", + ")\n", + "\n", + "\n", + "def normalize_img(image, label):\n", + " \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n", + " return tf.cast(image, tf.float32) / 255.0, label\n", + "\n", + "\n", + "mnist_train = mnist_train.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + "mnist_train = mnist_train.cache() \\\n", + " .shuffle(ds_info.splits['train'].num_examples) \\\n", + " .batch(128) \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + "mnist_test = mnist_test.map(\n", + " normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)\n", + "mnist_test = mnist_test.batch(128) \\\n", + " .cache() \\\n", + " .prefetch(tf.data.experimental.AUTOTUNE)\n", + "\n", + "\n", + "model = tf.keras.models.Sequential([\n", + " tf.keras.layers.Conv2D(\n", + " filters=32, kernel_size=(2, 2), input_shape=(28, 28, 1)),\n", + " tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=2),\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=128, activation=tf.nn.relu),\n", + " tf.keras.layers.Dropout(rate=0.2),\n", + " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", + "])\n", + "\n", + "\n", + "model.compile(\n", + " optimizer=tf.keras.optimizers.Adam(0.001),\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n", + ")\n", + "\n", + "model.summary()\n", + "\n", + "\n", + " \n", + "model.fit(mnist_train, epochs=5, validation_data=mnist_test)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "348fd0d3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "finalized": { + "timestamp": 1614876075124, + "trusted": true + }, + "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.6.8" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}