Skip to content

Commit a36fb3c

Browse files
author
Karandeep Grover
committed
ENH: moved all the active tutorial content from py/deeplearning
1 parent 3ef69c5 commit a36fb3c

File tree

60 files changed

+53235
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+53235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<h3 style='color:blue'>Exercise: GPU performance for fashion mnist dataset</h3>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"This notebook is derived from a tensorflow tutorial here: https://www.tensorflow.org/tutorials/keras/classification\n",
15+
"So please refer to it before starting work on this exercise"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"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",
23+
"\n",
24+
"\n",
25+
"| Hidden Layer | CPU | GPU |\n",
26+
"|:------|:------|:------|\n",
27+
"| 1 | ? | ? |\n",
28+
"| 5 | ? | ? |"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"# TensorFlow and tf.keras\n",
38+
"import tensorflow as tf\n",
39+
"from tensorflow import keras\n",
40+
"\n",
41+
"# Helper libraries\n",
42+
"import numpy as np\n",
43+
"import matplotlib.pyplot as plt\n",
44+
"\n",
45+
"print(tf.__version__)"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"fashion_mnist = keras.datasets.fashion_mnist\n",
55+
"\n",
56+
"(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n",
66+
" 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"train_images.shape"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {
82+
"scrolled": true
83+
},
84+
"outputs": [],
85+
"source": [
86+
"plt.imshow(train_images[0])"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"train_labels[0]"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"class_names[train_labels[0]]"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {
111+
"scrolled": false
112+
},
113+
"outputs": [],
114+
"source": [
115+
"plt.figure(figsize=(3,3))\n",
116+
"for i in range(5):\n",
117+
" plt.imshow(train_images[i])\n",
118+
" plt.xlabel(class_names[train_labels[i]])\n",
119+
" plt.show()"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"train_images_scaled = train_images / 255.0\n",
129+
"test_images_scaled = test_images / 255.0"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"def get_model(hidden_layers=1):\n",
139+
" layers = []\n",
140+
" # Your code goes here-----------START\n",
141+
" # Create Flatten input layers\n",
142+
" # Create hidden layers that are equal to hidden_layers argument in this function\n",
143+
" # Create output \n",
144+
" # Your code goes here-----------END\n",
145+
" model = keras.Sequential(layers)\n",
146+
" \n",
147+
" model.compile(optimizer='adam',\n",
148+
" loss='sparse_categorical_crossentropy',\n",
149+
" metrics=['accuracy'])\n",
150+
" \n",
151+
" return model"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"model = get_model(1)\n",
161+
"model.fit(train_images_scaled, train_labels, epochs=5)"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"model.predict(test_images_scaled)[2]"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"test_labels[2]"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"tf.config.experimental.list_physical_devices() "
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"<h4 style=\"color:purple\">5 Epochs performance comparison for 1 hidden layer</h4>"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"%%timeit -n1 -r1\n",
205+
"with tf.device('/CPU:0'):\n",
206+
" # your code goes here"
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {
213+
"scrolled": false
214+
},
215+
"outputs": [],
216+
"source": [
217+
"%%timeit -n1 -r1\n",
218+
"with tf.device('/GPU:0'):\n",
219+
" # your code goes here"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"<h4 style=\"color:purple\">5 Epocs performance comparison with 5 hidden layers</h4>"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": null,
232+
"metadata": {},
233+
"outputs": [],
234+
"source": [
235+
"%%timeit -n1 -r1\n",
236+
"with tf.device('/CPU:0'):\n",
237+
" # your code here"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": null,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"%%timeit -n1 -r1\n",
247+
"with tf.device('/GPU:0'):\n",
248+
" # your code here"
249+
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"metadata": {},
254+
"source": [
255+
"[Click me to check solution for this exercise](https://github.com/codebasics/py/blob/master/DeepLearningML/10_gpu_benchmarking/Exercise/exercise_solution.ipynb)"
256+
]
257+
}
258+
],
259+
"metadata": {
260+
"kernelspec": {
261+
"display_name": "Python 3",
262+
"language": "python",
263+
"name": "python3"
264+
},
265+
"language_info": {
266+
"codemirror_mode": {
267+
"name": "ipython",
268+
"version": 3
269+
},
270+
"file_extension": ".py",
271+
"mimetype": "text/x-python",
272+
"name": "python",
273+
"nbconvert_exporter": "python",
274+
"pygments_lexer": "ipython3",
275+
"version": "3.8.5"
276+
}
277+
},
278+
"nbformat": 4,
279+
"nbformat_minor": 4
280+
}

0 commit comments

Comments
 (0)