Skip to content

Commit 01c6cf8

Browse files
authored
Add files via upload
1 parent 9f31af0 commit 01c6cf8

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# How to Use Multi GPUs in Training in TensorFlow "
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"This is a simple codeset to utilize mutiple GPUs (if available) for a example MNIST problem in tensorflow"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"Bascially, we would only have to add two additional lines of code in \"Network\" part to utilize Multiple GPUs"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"strategy = tf.distribute.MirroredStrategy()\n",
31+
"with strategy.scope():"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"# Import Package\n",
41+
"import os\n",
42+
"import numpy as np\n",
43+
"import tensorflow as tf\n",
44+
"from tensorflow.keras import layers, models, losses, optimizers, datasets, utils\n",
45+
"\n",
46+
"# Data Prepare\n",
47+
"(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()\n",
48+
"train_x, test_x = np.expand_dims(train_x/255., -1), np.expand_dims(test_x/255., -1)\n",
49+
"print(\"Train Data's Shape : \", train_x.shape, train_y.shape)\n",
50+
"print(\"Test Data's Shape : \", test_x.shape, test_y.shape)\n",
51+
"\n",
52+
"# Build Network\n",
53+
"strategy = tf.distribute.MirroredStrategy() # New Lines \n",
54+
"with strategy.scope(): # New Lines\n",
55+
" cnn = models.Sequential()\n",
56+
" cnn.add(layers.Conv2D(16, 3, activation='relu', input_shape=(28, 28, 1,)))\n",
57+
" cnn.add(layers.MaxPool2D())\n",
58+
" cnn.add(layers.Conv2D(32, 3, activation='relu'))\n",
59+
" cnn.add(layers.MaxPool2D())\n",
60+
" cnn.add(layers.Flatten())\n",
61+
" cnn.add(layers.Dense(10, activation='softmax'))\n",
62+
"\n",
63+
" cnn.compile(optimizer=optimizers.Adam(), loss=losses.sparse_categorical_crossentropy, metrics=['accuracy']) \n",
64+
"print(\"Network Built!\")\n",
65+
"\n",
66+
"# Training Network with Multi GPUs\n",
67+
"epochs=10\n",
68+
"batch_size_each_gpu = 4096\n",
69+
"batch_size = batch_size_each_gpu*len(gpus) \n",
70+
"\n",
71+
"## ================= ##\n",
72+
"# Single GPU code \n",
73+
"# epochs=10\n",
74+
"# batch_size = 4096\n",
75+
"## ================= ##\n",
76+
"\n",
77+
"history = cnn.fit(train_x, train_y, epochs=10, batch_size=batch_size, validation_data=(test_x, test_y))"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"Adding a single GPU codeset below for comparison"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [],
92+
"source": [
93+
"# Import Package\n",
94+
"import os\n",
95+
"import numpy as np\n",
96+
"import tensorflow as tf\n",
97+
"from tensorflow.keras import layers, models, losses, optimizers, datasets, utils\n",
98+
"\n",
99+
"# Data Prepare\n",
100+
"(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()\n",
101+
"train_x, test_x = np.expand_dims(train_x/255., -1), np.expand_dims(test_x/255., -1)\n",
102+
"print(\"Train Data's Shape : \", train_x.shape, train_y.shape)\n",
103+
"print(\"Test Data's Shape : \", test_x.shape, test_y.shape)\n",
104+
"\n",
105+
"# Build Network\n",
106+
"cnn = models.Sequential()\n",
107+
"cnn.add(layers.Conv2D(16, 3, activation='relu', input_shape=(28, 28, 1,)))\n",
108+
"cnn.add(layers.MaxPool2D())\n",
109+
"cnn.add(layers.Conv2D(32, 3, activation='relu'))\n",
110+
"cnn.add(layers.MaxPool2D())\n",
111+
"cnn.add(layers.Flatten())\n",
112+
"cnn.add(layers.Dense(10, activation='softmax'))\n",
113+
"\n",
114+
"cnn.compile(optimizer=optimizers.Adam(), loss=losses.sparse_categorical_crossentropy, metrics=['accuracy']) \n",
115+
"print(\"Network Built!\")\n",
116+
"\n",
117+
"# Training Network\n",
118+
"epochs=10\n",
119+
"batch_size = 4096\n",
120+
"history = cnn.fit(train_x, train_y, epochs=10, batch_size=batch_size, validation_data=(test_x, test_y))"
121+
]
122+
}
123+
],
124+
"metadata": {
125+
"kernelspec": {
126+
"display_name": "Python 3",
127+
"language": "python",
128+
"name": "python3"
129+
},
130+
"language_info": {
131+
"codemirror_mode": {
132+
"name": "ipython",
133+
"version": 3
134+
},
135+
"file_extension": ".py",
136+
"mimetype": "text/x-python",
137+
"name": "python",
138+
"nbconvert_exporter": "python",
139+
"pygments_lexer": "ipython3",
140+
"version": "3.7.6"
141+
}
142+
},
143+
"nbformat": 4,
144+
"nbformat_minor": 4
145+
}

0 commit comments

Comments
 (0)