|
44 | 44 | },
|
45 | 45 | {
|
46 | 46 | "cell_type": "code",
|
47 |
| - "execution_count": 3, |
| 47 | + "execution_count": 5, |
48 | 48 | "metadata": {
|
49 | 49 | "scrolled": true
|
50 | 50 | },
|
|
53 | 53 | "data": {
|
54 | 54 | "text/plain": [
|
55 | 55 | "[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),\n",
|
56 |
| - " PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU'),\n", |
57 |
| - " PhysicalDevice(name='/physical_device:XLA_GPU:0', device_type='XLA_GPU')]" |
| 56 | + " PhysicalDevice(name='/physical_device:XLA_CPU:0', device_type='XLA_CPU')]" |
58 | 57 | ]
|
59 | 58 | },
|
60 |
| - "execution_count": 3, |
| 59 | + "execution_count": 5, |
61 | 60 | "metadata": {},
|
62 | 61 | "output_type": "execute_result"
|
63 | 62 | }
|
|
68 | 67 | },
|
69 | 68 | {
|
70 | 69 | "cell_type": "code",
|
71 |
| - "execution_count": 7, |
| 70 | + "execution_count": 6, |
72 | 71 | "metadata": {},
|
73 | 72 | "outputs": [
|
74 | 73 | {
|
75 | 74 | "data": {
|
76 | 75 | "text/plain": [
|
77 |
| - "'2.2.0'" |
| 76 | + "'2.3.0'" |
78 | 77 | ]
|
79 | 78 | },
|
80 |
| - "execution_count": 7, |
| 79 | + "execution_count": 6, |
81 | 80 | "metadata": {},
|
82 | 81 | "output_type": "execute_result"
|
83 | 82 | }
|
|
88 | 87 | },
|
89 | 88 | {
|
90 | 89 | "cell_type": "code",
|
91 |
| - "execution_count": 5, |
| 90 | + "execution_count": 7, |
92 | 91 | "metadata": {},
|
93 | 92 | "outputs": [
|
94 | 93 | {
|
|
97 | 96 | "True"
|
98 | 97 | ]
|
99 | 98 | },
|
100 |
| - "execution_count": 5, |
| 99 | + "execution_count": 7, |
101 | 100 | "metadata": {},
|
102 | 101 | "output_type": "execute_result"
|
103 | 102 | }
|
|
129 | 128 | },
|
130 | 129 | {
|
131 | 130 | "cell_type": "code",
|
132 |
| - "execution_count": 5, |
| 131 | + "execution_count": 8, |
133 | 132 | "metadata": {},
|
134 |
| - "outputs": [ |
135 |
| - { |
136 |
| - "name": "stdout", |
137 |
| - "output_type": "stream", |
138 |
| - "text": [ |
139 |
| - "Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n", |
140 |
| - "170500096/170498071 [==============================] - 42s 0us/step\n" |
141 |
| - ] |
142 |
| - } |
143 |
| - ], |
| 133 | + "outputs": [], |
144 | 134 | "source": [
|
145 | 135 | "(X_train, y_train), (X_test,y_test) = tf.keras.datasets.cifar10.load_data()"
|
146 | 136 | ]
|
147 | 137 | },
|
148 | 138 | {
|
149 | 139 | "cell_type": "code",
|
150 |
| - "execution_count": 6, |
| 140 | + "execution_count": 9, |
151 | 141 | "metadata": {},
|
152 | 142 | "outputs": [
|
153 | 143 | {
|
|
156 | 146 | "(50000, 32, 32, 3)"
|
157 | 147 | ]
|
158 | 148 | },
|
159 |
| - "execution_count": 6, |
| 149 | + "execution_count": 9, |
160 | 150 | "metadata": {},
|
161 | 151 | "output_type": "execute_result"
|
162 | 152 | }
|
|
167 | 157 | },
|
168 | 158 | {
|
169 | 159 | "cell_type": "code",
|
170 |
| - "execution_count": 7, |
| 160 | + "execution_count": 10, |
171 | 161 | "metadata": {
|
172 | 162 | "scrolled": true
|
173 | 163 | },
|
|
178 | 168 | "(50000, 1)"
|
179 | 169 | ]
|
180 | 170 | },
|
181 |
| - "execution_count": 7, |
| 171 | + "execution_count": 10, |
182 | 172 | "metadata": {},
|
183 | 173 | "output_type": "execute_result"
|
184 | 174 | }
|
|
401 | 391 | },
|
402 | 392 | {
|
403 | 393 | "cell_type": "code",
|
404 |
| - "execution_count": 18, |
| 394 | + "execution_count": 11, |
405 | 395 | "metadata": {
|
406 | 396 | "scrolled": true
|
407 | 397 | },
|
|
413 | 403 | },
|
414 | 404 | {
|
415 | 405 | "cell_type": "code",
|
416 |
| - "execution_count": 19, |
| 406 | + "execution_count": 12, |
417 | 407 | "metadata": {},
|
418 | 408 | "outputs": [],
|
419 | 409 | "source": [
|
|
453 | 443 | "cell_type": "code",
|
454 | 444 | "execution_count": 21,
|
455 | 445 | "metadata": {
|
456 |
| - "scrolled": true |
| 446 | + "scrolled": false |
457 | 447 | },
|
458 | 448 | "outputs": [
|
459 | 449 | {
|
|
476 | 466 | ]
|
477 | 467 | },
|
478 | 468 | {
|
479 |
| - "cell_type": "markdown", |
| 469 | + "cell_type": "code", |
| 470 | + "execution_count": 15, |
480 | 471 | "metadata": {},
|
| 472 | + "outputs": [], |
481 | 473 | "source": [
|
482 |
| - "<h4 style=\"color:purple\">Model building and training</h4>" |
| 474 | + "train_tf_dataset = tf.data.Dataset.from_tensor_slices((X_train_scaled, y_train_categorical))\n", |
| 475 | + "test_tf_dataset = tf.data.Dataset.from_tensor_slices((X_test_scaled, y_test_categorical))" |
483 | 476 | ]
|
484 | 477 | },
|
485 | 478 | {
|
486 | 479 | "cell_type": "code",
|
487 |
| - "execution_count": 22, |
488 |
| - "metadata": { |
489 |
| - "scrolled": true |
490 |
| - }, |
| 480 | + "execution_count": 19, |
| 481 | + "metadata": {}, |
491 | 482 | "outputs": [
|
492 | 483 | {
|
493 | 484 | "name": "stdout",
|
494 | 485 | "output_type": "stream",
|
495 | 486 | "text": [
|
496 |
| - "1563/1563 [==============================] - 22s 14ms/step - loss: 1.8602 - accuracy: 0.3321\n" |
| 487 | + "WARNING:tensorflow:There are non-GPU devices in `tf.distribute.Strategy`, not using nccl allreduce.\n", |
| 488 | + "INFO:tensorflow:Using MirroredStrategy with devices ('/job:localhost/replica:0/task:0/device:CPU:0',)\n" |
497 | 489 | ]
|
498 |
| - }, |
| 490 | + } |
| 491 | + ], |
| 492 | + "source": [ |
| 493 | + "strategy = tf.distribute.MirroredStrategy()" |
| 494 | + ] |
| 495 | + }, |
| 496 | + { |
| 497 | + "cell_type": "code", |
| 498 | + "execution_count": 20, |
| 499 | + "metadata": {}, |
| 500 | + "outputs": [ |
499 | 501 | {
|
500 | 502 | "data": {
|
501 | 503 | "text/plain": [
|
502 |
| - "<tensorflow.python.keras.callbacks.History at 0x7f697c1c82b0>" |
| 504 | + "1" |
503 | 505 | ]
|
504 | 506 | },
|
505 |
| - "execution_count": 22, |
| 507 | + "execution_count": 20, |
506 | 508 | "metadata": {},
|
507 | 509 | "output_type": "execute_result"
|
508 | 510 | }
|
509 | 511 | ],
|
510 | 512 | "source": [
|
511 |
| - "model = keras.Sequential([\n", |
512 |
| - " keras.layers.Flatten(input_shape=(32,32,3)),\n", |
513 |
| - " keras.layers.Dense(3000, activation='relu'),\n", |
514 |
| - " keras.layers.Dense(1000, activation='relu'),\n", |
515 |
| - " keras.layers.Dense(10, activation='sigmoid') \n", |
516 |
| - " ])\n", |
| 513 | + "strategy.num_replicas_in_sync" |
| 514 | + ] |
| 515 | + }, |
| 516 | + { |
| 517 | + "cell_type": "code", |
| 518 | + "execution_count": 21, |
| 519 | + "metadata": {}, |
| 520 | + "outputs": [], |
| 521 | + "source": [ |
| 522 | + "BATCH_SIZE_PER_REPLICA = 64\n", |
| 523 | + "BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync\n", |
| 524 | + "SHUFFLE_BUFFER_SIZE = 100\n", |
| 525 | + "\n", |
| 526 | + "train_dataset = train_tf_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)\n", |
| 527 | + "test_dataset = test_tf_dataset.batch(BATCH_SIZE)" |
| 528 | + ] |
| 529 | + }, |
| 530 | + { |
| 531 | + "cell_type": "markdown", |
| 532 | + "metadata": {}, |
| 533 | + "source": [ |
| 534 | + "<h4 style=\"color:purple\">Model building and training</h4>" |
| 535 | + ] |
| 536 | + }, |
| 537 | + { |
| 538 | + "cell_type": "code", |
| 539 | + "execution_count": 23, |
| 540 | + "metadata": { |
| 541 | + "scrolled": true |
| 542 | + }, |
| 543 | + "outputs": [ |
| 544 | + { |
| 545 | + "name": "stdout", |
| 546 | + "output_type": "stream", |
| 547 | + "text": [ |
| 548 | + "WARNING:tensorflow:From C:\\Users\\dhava\\AppData\\Roaming\\Python\\Python38\\site-packages\\tensorflow\\python\\data\\ops\\multi_device_iterator_ops.py:601: get_next_as_optional (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.\n", |
| 549 | + "Instructions for updating:\n", |
| 550 | + "Use `tf.data.Iterator.get_next_as_optional()` instead.\n", |
| 551 | + "782/782 [==============================] - 20s 26ms/step - loss: 1.9170 - accuracy: 0.3119\n" |
| 552 | + ] |
| 553 | + } |
| 554 | + ], |
| 555 | + "source": [ |
| 556 | + "with strategy.scope():\n", |
| 557 | + " model = keras.Sequential([\n", |
| 558 | + " keras.layers.Flatten(input_shape=(32,32,3)),\n", |
| 559 | + " keras.layers.Dense(3000, activation='relu'),\n", |
| 560 | + " keras.layers.Dense(1000, activation='relu'),\n", |
| 561 | + " keras.layers.Dense(10, activation='sigmoid') \n", |
| 562 | + " ])\n", |
517 | 563 | "\n",
|
518 |
| - "model.compile(optimizer='SGD',\n", |
519 |
| - " loss='categorical_crossentropy',\n", |
520 |
| - " metrics=['accuracy'])\n", |
| 564 | + " model.compile(optimizer='SGD',\n", |
| 565 | + " loss='categorical_crossentropy',\n", |
| 566 | + " metrics=['accuracy'])\n", |
521 | 567 | "\n",
|
522 |
| - "model.fit(X_train_scaled, y_train_categorical, epochs=1)" |
| 568 | + " model.fit(train_dataset, epochs=1)" |
523 | 569 | ]
|
524 | 570 | },
|
525 | 571 | {
|
|
0 commit comments