Skip to content

Commit

Permalink
ENH: moved all the active tutorial content from py/deeplearning
Browse files Browse the repository at this point in the history
  • Loading branch information
Karandeep Grover committed Jan 25, 2021
1 parent 3ef69c5 commit a36fb3c
Show file tree
Hide file tree
Showing 60 changed files with 53,235 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style='color:blue'>Exercise: GPU performance for fashion mnist dataset</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook is derived from a tensorflow tutorial here: https://www.tensorflow.org/tutorials/keras/classification\n",
"So please refer to it before starting work on this exercise"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You need to write code wherever you see `your code goes here` comment. You are going to do image classification for fashion mnist dataset and then you will benchmark the performance of GPU vs CPU for 1 hidden layer and then for 5 hidden layers. You will eventually fill out this table with your performance benchmark numbers\n",
"\n",
"\n",
"| Hidden Layer | CPU | GPU |\n",
"|:------|:------|:------|\n",
"| 1 | ? | ? |\n",
"| 5 | ? | ? |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TensorFlow and tf.keras\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"\n",
"# Helper libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"print(tf.__version__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fashion_mnist = keras.datasets.fashion_mnist\n",
"\n",
"(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n",
" 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_images.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"plt.imshow(train_images[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_labels[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class_names[train_labels[0]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"plt.figure(figsize=(3,3))\n",
"for i in range(5):\n",
" plt.imshow(train_images[i])\n",
" plt.xlabel(class_names[train_labels[i]])\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_images_scaled = train_images / 255.0\n",
"test_images_scaled = test_images / 255.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_model(hidden_layers=1):\n",
" layers = []\n",
" # Your code goes here-----------START\n",
" # Create Flatten input layers\n",
" # Create hidden layers that are equal to hidden_layers argument in this function\n",
" # Create output \n",
" # Your code goes here-----------END\n",
" model = keras.Sequential(layers)\n",
" \n",
" model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])\n",
" \n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = get_model(1)\n",
"model.fit(train_images_scaled, train_labels, epochs=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.predict(test_images_scaled)[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_labels[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tf.config.experimental.list_physical_devices() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4 style=\"color:purple\">5 Epochs performance comparison for 1 hidden layer</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n1 -r1\n",
"with tf.device('/CPU:0'):\n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"%%timeit -n1 -r1\n",
"with tf.device('/GPU:0'):\n",
" # your code goes here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4 style=\"color:purple\">5 Epocs performance comparison with 5 hidden layers</h4>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n1 -r1\n",
"with tf.device('/CPU:0'):\n",
" # your code here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%timeit -n1 -r1\n",
"with tf.device('/GPU:0'):\n",
" # your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Click me to check solution for this exercise](https://github.com/codebasics/py/blob/master/DeepLearningML/10_gpu_benchmarking/Exercise/exercise_solution.ipynb)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit a36fb3c

Please sign in to comment.