Last active
November 14, 2018 04:17
-
-
Save weakish/53dee45528bb4858a18aa12650a60840 to your computer and use it in GitHub Desktop.
Keras_LSTM_TPU.ipynb (change mini batch size from 128 to 128/8=16)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Keras_LSTM_TPU.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "TPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/weakish/53dee45528bb4858a18aa12650a60840/keras_lstm_tpu.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "CB43mV-TD1vb", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"# Tutorial - [How to train Keras model x20 times faster with TPU for free](https://www.dlology.com/blog/how-to-train-keras-model-x20-times-faster-with-tpu-for-free/)" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "ya06BE0ZU526", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import tensorflow as tf\n", | |
"from tensorflow.keras.datasets import imdb\n", | |
"from tensorflow.keras.preprocessing import sequence\n", | |
"from tensorflow.python.keras.layers import Input, LSTM, Bidirectional, Dense, Embedding" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "_uSZchXTVOHr", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
}, | |
"outputId": "4886a016-8518-4b58-ca36-3e3278f91322" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"\n", | |
"# Number of words to consider as features\n", | |
"max_features = 10000\n", | |
"# Cut texts after this number of words (among top max_features most common words)\n", | |
"maxlen = 500\n", | |
"\n", | |
"# Load data\n", | |
"(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)\n", | |
"\n", | |
"# Reverse sequences\n", | |
"x_train = [x[::-1] for x in x_train]\n", | |
"x_test = [x[::-1] for x in x_test]\n", | |
"\n", | |
"# Pad sequences\n", | |
"x_train = sequence.pad_sequences(x_train, maxlen=maxlen)\n", | |
"x_test = sequence.pad_sequences(x_test, maxlen=maxlen)" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb.npz\n", | |
"17465344/17464789 [==============================] - 0s 0us/step\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "p35nSfjbVVBE", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def make_model(batch_size=None):\n", | |
" source = Input(shape=(maxlen,), batch_size=batch_size, dtype=tf.int32, name='Input')\n", | |
" embedding = Embedding(input_dim=max_features, output_dim=128, name='Embedding')(source)\n", | |
" # lstm = Bidirectional(LSTM(32, name = 'LSTM'), name='Bidirectional')(embedding)\n", | |
" lstm = LSTM(32, name = 'LSTM')(embedding)\n", | |
" predicted_var = Dense(1, activation='sigmoid', name='Output')(lstm)\n", | |
" model = tf.keras.Model(inputs=[source], outputs=[predicted_var])\n", | |
" model.compile(\n", | |
" optimizer=tf.train.RMSPropOptimizer(learning_rate=0.01),\n", | |
" loss='binary_crossentropy',\n", | |
" metrics=['acc'])\n", | |
" return model\n", | |
" " | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "bivVZS0jZhxg", | |
"colab_type": "code", | |
"outputId": "fbdb5484-f0e0-46e3-db2d-6af503339b52", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 278 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tf.keras.backend.clear_session()\n", | |
"training_model = make_model(batch_size = 128/8)\n", | |
"training_model.summary()" | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"Input (InputLayer) (16, 500) 0 \n", | |
"_________________________________________________________________\n", | |
"Embedding (Embedding) (16, 500, 128) 1280000 \n", | |
"_________________________________________________________________\n", | |
"LSTM (LSTM) (16, 32) 20608 \n", | |
"_________________________________________________________________\n", | |
"Output (Dense) (16, 1) 33 \n", | |
"=================================================================\n", | |
"Total params: 1,300,641\n", | |
"Trainable params: 1,300,641\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "xeGmIQX2aJvw", | |
"colab_type": "code", | |
"outputId": "8d4fafb7-1d74-4ead-8f30-79a7ac6fa7b4", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 590 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import os\n", | |
"# This address identifies the TPU we'll use when configuring TensorFlow.\n", | |
"TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']\n", | |
"tf.logging.set_verbosity(tf.logging.INFO)\n", | |
"\n", | |
"tpu_model = tf.contrib.tpu.keras_to_tpu_model(\n", | |
" training_model,\n", | |
" strategy=tf.contrib.tpu.TPUDistributionStrategy(\n", | |
" tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))\n", | |
"\n", | |
"tpu_model.summary()" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"INFO:tensorflow:Querying Tensorflow master (b'grpc://10.68.69.42:8470') for TPU system metadata.\n", | |
"INFO:tensorflow:Found TPU system:\n", | |
"INFO:tensorflow:*** Num TPU Cores: 8\n", | |
"INFO:tensorflow:*** Num TPU Workers: 1\n", | |
"INFO:tensorflow:*** Num TPU Cores Per Worker: 8\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:CPU:0, CPU, -1, 17260984861428910334)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 1190064797676527259)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 4948940367157152356)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 12981579361334909640)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 3383750835275027858)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:2, TPU, 17179869184, 16734569315852543769)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:3, TPU, 17179869184, 1842442240140948747)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:4, TPU, 17179869184, 1141177644839217058)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:5, TPU, 17179869184, 6943083046894110387)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:6, TPU, 17179869184, 6968958008103944341)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:7, TPU, 17179869184, 885304166695756159)\n", | |
"INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU_SYSTEM:0, TPU_SYSTEM, 17179869184, 11658603214034785057)\n", | |
"WARNING:tensorflow:tpu_model (from tensorflow.contrib.tpu.python.tpu.keras_support) is experimental and may change or be removed at any time, and without warning.\n", | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"Input (InputLayer) (16, 500) 0 \n", | |
"_________________________________________________________________\n", | |
"Embedding (Embedding) (16, 500, 128) 1280000 \n", | |
"_________________________________________________________________\n", | |
"LSTM (LSTM) (16, 32) 20608 \n", | |
"_________________________________________________________________\n", | |
"Output (Dense) (16, 1) 33 \n", | |
"=================================================================\n", | |
"Total params: 1,300,641\n", | |
"Trainable params: 1,300,641\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "XlSm1vd5bteH", | |
"colab_type": "code", | |
"outputId": "a397434e-704e-4e8d-83a6-6d416a3af11a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 1148 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import time\n", | |
"start_time = time.time()\n", | |
"\n", | |
"history = tpu_model.fit(x_train, y_train,\n", | |
" epochs=20,\n", | |
" batch_size=128,\n", | |
" validation_split=0.2)\n", | |
"tpu_model.save_weights('./tpu_model.h5', overwrite=True)\n", | |
"\n", | |
"print(\"--- %s seconds ---\" % (time.time() - start_time))\n" | |
], | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Train on 25000 samples, validate on 5000 samples\n", | |
"Epoch 1/20\n", | |
"INFO:tensorflow:New input shapes; (re-)compiling: mode=train (# of cores 8), [TensorSpec(shape=(16,), dtype=tf.int32, name='core_id0'), TensorSpec(shape=(16, 500), dtype=tf.int32, name='Input_10'), TensorSpec(shape=(16, 1), dtype=tf.float32, name='Output_target_30')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 3.27878999710083 secs\n", | |
"INFO:tensorflow:Setting weights on TPU model.\n", | |
"24832/25000 [============================>.] - ETA: 0s - loss: 0.6936 - acc: 0.5263INFO:tensorflow:New input shapes; (re-)compiling: mode=train (# of cores 8), [TensorSpec(shape=(5,), dtype=tf.int32, name='core_id0'), TensorSpec(shape=(5, 500), dtype=tf.int32, name='Input_10'), TensorSpec(shape=(5, 1), dtype=tf.float32, name='Output_target_30')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 5.3668670654296875 secs\n", | |
"24960/25000 [============================>.] - ETA: 0s - loss: 0.6933 - acc: 0.5269INFO:tensorflow:New input shapes; (re-)compiling: mode=eval (# of cores 8), [TensorSpec(shape=(16,), dtype=tf.int32, name='core_id_10'), TensorSpec(shape=(16, 500), dtype=tf.int32, name='Input_10'), TensorSpec(shape=(16, 1), dtype=tf.float32, name='Output_target_30')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 6.521803855895996 secs\n", | |
"INFO:tensorflow:New input shapes; (re-)compiling: mode=eval (# of cores 8), [TensorSpec(shape=(1,), dtype=tf.int32, name='core_id_10'), TensorSpec(shape=(1, 500), dtype=tf.int32, name='Input_10'), TensorSpec(shape=(1, 1), dtype=tf.float32, name='Output_target_30')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 8.179735898971558 secs\n", | |
"25000/25000 [==============================] - 60s 2ms/step - loss: 0.6933 - acc: 0.5269 - val_loss: 0.8048 - val_acc: 0.5230\n", | |
"Epoch 2/20\n", | |
"25000/25000 [==============================] - 16s 653us/step - loss: 0.5264 - acc: 0.7417 - val_loss: 0.4219 - val_acc: 0.8230\n", | |
"Epoch 3/20\n", | |
"25000/25000 [==============================] - 17s 661us/step - loss: 0.2839 - acc: 0.8880 - val_loss: 0.1879 - val_acc: 0.9290\n", | |
"Epoch 4/20\n", | |
"25000/25000 [==============================] - 16s 659us/step - loss: 0.2005 - acc: 0.9238 - val_loss: 0.1412 - val_acc: 0.9554\n", | |
"Epoch 5/20\n", | |
"25000/25000 [==============================] - 16s 659us/step - loss: 0.1428 - acc: 0.9479 - val_loss: 0.1033 - val_acc: 0.9628\n", | |
"Epoch 6/20\n", | |
"25000/25000 [==============================] - 16s 654us/step - loss: 0.1044 - acc: 0.9637 - val_loss: 0.0543 - val_acc: 0.9832\n", | |
"Epoch 7/20\n", | |
"25000/25000 [==============================] - 16s 655us/step - loss: 0.0729 - acc: 0.9756 - val_loss: 0.0461 - val_acc: 0.9896\n", | |
"Epoch 8/20\n", | |
"25000/25000 [==============================] - 16s 653us/step - loss: 0.0508 - acc: 0.9837 - val_loss: 0.0258 - val_acc: 0.9926\n", | |
"Epoch 9/20\n", | |
"25000/25000 [==============================] - 16s 649us/step - loss: 0.0430 - acc: 0.9862 - val_loss: 0.0214 - val_acc: 0.9944\n", | |
"Epoch 10/20\n", | |
"25000/25000 [==============================] - 16s 653us/step - loss: 0.0309 - acc: 0.9905 - val_loss: 0.0122 - val_acc: 0.9972\n", | |
"Epoch 11/20\n", | |
"25000/25000 [==============================] - 16s 653us/step - loss: 0.0248 - acc: 0.9930 - val_loss: 0.0160 - val_acc: 0.9952\n", | |
"Epoch 12/20\n", | |
"25000/25000 [==============================] - 16s 651us/step - loss: 0.0206 - acc: 0.9937 - val_loss: 0.0172 - val_acc: 0.9950\n", | |
"Epoch 13/20\n", | |
"25000/25000 [==============================] - 17s 670us/step - loss: 0.0194 - acc: 0.9941 - val_loss: 0.0186 - val_acc: 0.9952\n", | |
"Epoch 14/20\n", | |
"25000/25000 [==============================] - 16s 658us/step - loss: 0.0183 - acc: 0.9944 - val_loss: 0.0073 - val_acc: 0.9980\n", | |
"Epoch 15/20\n", | |
"25000/25000 [==============================] - 16s 654us/step - loss: 0.0142 - acc: 0.9954 - val_loss: 0.0093 - val_acc: 0.9976\n", | |
"Epoch 16/20\n", | |
"25000/25000 [==============================] - 16s 649us/step - loss: 0.0129 - acc: 0.9959 - val_loss: 0.0086 - val_acc: 0.9972\n", | |
"Epoch 17/20\n", | |
"25000/25000 [==============================] - 16s 649us/step - loss: 0.0132 - acc: 0.9962 - val_loss: 0.0053 - val_acc: 0.9990\n", | |
"Epoch 18/20\n", | |
"25000/25000 [==============================] - 16s 655us/step - loss: 0.0145 - acc: 0.9953 - val_loss: 0.0049 - val_acc: 0.9988\n", | |
"Epoch 19/20\n", | |
"25000/25000 [==============================] - 16s 648us/step - loss: 0.0117 - acc: 0.9966 - val_loss: 0.0036 - val_acc: 0.9990\n", | |
"Epoch 20/20\n", | |
"25000/25000 [==============================] - 16s 648us/step - loss: 0.0096 - acc: 0.9972 - val_loss: 0.0083 - val_acc: 0.9986\n", | |
"INFO:tensorflow:Copying TPU weights to the CPU\n", | |
"--- 376.7751371860504 seconds ---\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "tLjeqllMZzv5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"# history = tpu_model.fit(x_train, y_train,\n", | |
"# epochs=20,\n", | |
"# batch_size=128 * 8,\n", | |
"# validation_split=0.2)\n", | |
"# tpu_model.save_weights('./tpu_model.h5', overwrite=True)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "gpcLs6PYatf5", | |
"colab_type": "code", | |
"outputId": "4c8ecdca-5a73-4550-e4c1-337cc48bba7d", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 278 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"inferencing_model = make_model(batch_size=None)\n", | |
"inferencing_model.load_weights('./tpu_model.h5')\n", | |
"inferencing_model.summary()" | |
], | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"Input (InputLayer) (None, 500) 0 \n", | |
"_________________________________________________________________\n", | |
"Embedding (Embedding) (None, 500, 128) 1280000 \n", | |
"_________________________________________________________________\n", | |
"LSTM (LSTM) (None, 32) 20608 \n", | |
"_________________________________________________________________\n", | |
"Output (Dense) (None, 1) 33 \n", | |
"=================================================================\n", | |
"Total params: 1,300,641\n", | |
"Trainable params: 1,300,641\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "KZyqMpMecAGp", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "2cqqkN84dJQk", | |
"colab_type": "code", | |
"outputId": "743f5d12-2e6c-41dc-9c0e-a81b8bd6430e", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 52 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"inferencing_model.evaluate(x_test, y_test)" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"25000/25000 [==============================] - 73s 3ms/step\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[0.7590128913450241, 0.85628]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "XMR_TncEeFKx", | |
"colab_type": "code", | |
"outputId": "9ead66f2-209f-4f46-957d-5a1adcf6df97", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 159 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tpu_model.evaluate(x_test, y_test, batch_size=128)" | |
], | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"24704/25000 [============================>.] - ETA: 0sINFO:tensorflow:New input shapes; (re-)compiling: mode=eval (# of cores 8), [TensorSpec(shape=(5,), dtype=tf.int32, name='core_id_10'), TensorSpec(shape=(5, 500), dtype=tf.int32, name='Input_10'), TensorSpec(shape=(5, 1), dtype=tf.float32, name='Output_target_30')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 14.0126211643219 secs\n", | |
"25000/25000 [==============================] - 25s 984us/step\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[0.771998558883667, 0.8562400000572205]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "PYCdyFggnKAU", | |
"colab_type": "code", | |
"outputId": "d68e3b51-e570-4d1c-d9d4-538ab383e7fe", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 54 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tpu_model.evaluate(x_test, y_test, batch_size=128 * 8)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"25000/25000 [==============================] - 2s 94us/step\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[0.608636932144165, 0.8271999996948243]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 25 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "CanGKZdUeOKf", | |
"colab_type": "code", | |
"outputId": "c39825ad-9357-4ec2-f8af-9328b0fc6083", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 199 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"inferencing_model.predict(x_test[:10])> 0.5" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[False],\n", | |
" [ True],\n", | |
" [ True],\n", | |
" [ True],\n", | |
" [ True],\n", | |
" [ True],\n", | |
" [ True],\n", | |
" [False],\n", | |
" [ True],\n", | |
" [ True]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 26 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "gVeRjm5AeW-w", | |
"colab_type": "code", | |
"outputId": "1db03650-5709-449c-f0dc-d863b47c61f5", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"y_test[:10]" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([0, 1, 1, 0, 1, 1, 1, 0, 0, 1])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 27 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "tIw95Yc0eeeY", | |
"colab_type": "code", | |
"outputId": "80a74a4a-664d-4937-b242-6a94303e9cca", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 235 | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"tpu_model.predict_on_batch(x_train[:128 * 8])>0.5" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"INFO:tensorflow:New input shapes; (re-)compiling: mode=infer, [TensorSpec(shape=(128, 500), dtype=tf.int32, name='Input0')]\n", | |
"INFO:tensorflow:Overriding default placeholder.\n", | |
"INFO:tensorflow:Remapping placeholder for Input\n", | |
"INFO:tensorflow:Started compiling\n", | |
"INFO:tensorflow:Finished compiling. Time elapsed: 7.4768149852752686 secs\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[ True],\n", | |
" [False],\n", | |
" [False],\n", | |
" ...,\n", | |
" [False],\n", | |
" [ True],\n", | |
" [False]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 28 | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "EiQqm2R_DvIV", | |
"colab_type": "text" | |
}, | |
"cell_type": "markdown", | |
"source": [ | |
"## Download the trained model weights to your local file system" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "UzMykUsdDjiD", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import files\n", | |
"\n", | |
"files.download('./tpu_model.h5')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "apwRGvwWDnau", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment