├── README.md ├── chapter01 └── README.md ├── chapter02 ├── .ipynb_checkpoints │ ├── OReilly_C2_ImageGenerator-checkpoint.ipynb │ ├── OReilly_C2_generator-checkpoint.ipynb │ ├── OReilly_C2_tf_dataset-checkpoint.ipynb │ └── oreilly_tf_dataset-Copy1-checkpoint.ipynb ├── OReilly_C2_ImageGenerator.ipynb ├── OReilly_C2_generator.ipynb ├── OReilly_C2_tf_dataset.ipynb ├── README.md ├── dataset │ ├── CaseCountData.xlsx │ ├── README.md │ ├── full_data.csv │ ├── owid-covid-data-part001.csv │ ├── owid-covid-data-part0010.csv │ ├── owid-covid-data-part00100.csv │ ├── owid-covid-data-part0011.csv │ ├── owid-covid-data-part0012.csv │ ├── owid-covid-data-part0013.csv │ ├── owid-covid-data-part0014.csv │ ├── owid-covid-data-part0015.csv │ ├── owid-covid-data-part0016.csv │ ├── owid-covid-data-part0017.csv │ ├── owid-covid-data-part0018.csv │ ├── owid-covid-data-part0019.csv │ ├── owid-covid-data-part002.csv │ ├── owid-covid-data-part0020.csv │ ├── owid-covid-data-part0021.csv │ ├── owid-covid-data-part0022.csv │ ├── owid-covid-data-part0023.csv │ ├── owid-covid-data-part0024.csv │ ├── owid-covid-data-part0025.csv │ ├── owid-covid-data-part0026.csv │ ├── owid-covid-data-part0027.csv │ ├── owid-covid-data-part0028.csv │ ├── owid-covid-data-part0029.csv │ ├── owid-covid-data-part003.csv │ ├── owid-covid-data-part0030.csv │ ├── owid-covid-data-part0031.csv │ ├── owid-covid-data-part0032.csv │ ├── owid-covid-data-part0033.csv │ ├── owid-covid-data-part0034.csv │ ├── owid-covid-data-part0035.csv │ ├── owid-covid-data-part0036.csv │ ├── owid-covid-data-part0037.csv │ ├── owid-covid-data-part0038.csv │ ├── owid-covid-data-part0039.csv │ ├── owid-covid-data-part004.csv │ ├── owid-covid-data-part0040.csv │ ├── owid-covid-data-part0041.csv │ ├── owid-covid-data-part0042.csv │ ├── owid-covid-data-part0043.csv │ ├── owid-covid-data-part0044.csv │ ├── owid-covid-data-part0045.csv │ ├── owid-covid-data-part0046.csv │ ├── owid-covid-data-part0047.csv │ ├── owid-covid-data-part0048.csv │ ├── owid-covid-data-part0049.csv │ ├── owid-covid-data-part005.csv │ ├── owid-covid-data-part0050.csv │ ├── owid-covid-data-part0051.csv │ ├── owid-covid-data-part0052.csv │ ├── owid-covid-data-part0053.csv │ ├── owid-covid-data-part0054.csv │ ├── owid-covid-data-part0055.csv │ ├── owid-covid-data-part0056.csv │ ├── owid-covid-data-part0057.csv │ ├── owid-covid-data-part0058.csv │ ├── owid-covid-data-part0059.csv │ ├── owid-covid-data-part006.csv │ ├── owid-covid-data-part0060.csv │ ├── owid-covid-data-part0061.csv │ ├── owid-covid-data-part0062.csv │ ├── owid-covid-data-part0063.csv │ ├── owid-covid-data-part0064.csv │ ├── owid-covid-data-part0065.csv │ ├── owid-covid-data-part0066.csv │ ├── owid-covid-data-part0067.csv │ ├── owid-covid-data-part0068.csv │ ├── owid-covid-data-part0069.csv │ ├── owid-covid-data-part007.csv │ ├── owid-covid-data-part0070.csv │ ├── owid-covid-data-part0071.csv │ ├── owid-covid-data-part0072.csv │ ├── owid-covid-data-part0073.csv │ ├── owid-covid-data-part0074.csv │ ├── owid-covid-data-part0075.csv │ ├── owid-covid-data-part0076.csv │ ├── owid-covid-data-part0077.csv │ ├── owid-covid-data-part0078.csv │ ├── owid-covid-data-part0079.csv │ ├── owid-covid-data-part008.csv │ ├── owid-covid-data-part0080.csv │ ├── owid-covid-data-part0081.csv │ ├── owid-covid-data-part0082.csv │ ├── owid-covid-data-part0083.csv │ ├── owid-covid-data-part0084.csv │ ├── owid-covid-data-part0085.csv │ ├── owid-covid-data-part0086.csv │ ├── owid-covid-data-part0087.csv │ ├── owid-covid-data-part0088.csv │ ├── owid-covid-data-part0089.csv │ ├── owid-covid-data-part009.csv │ ├── owid-covid-data-part0090.csv │ ├── owid-covid-data-part0091.csv │ ├── owid-covid-data-part0092.csv │ ├── owid-covid-data-part0093.csv │ ├── owid-covid-data-part0094.csv │ ├── owid-covid-data-part0095.csv │ ├── owid-covid-data-part0096.csv │ ├── owid-covid-data-part0097.csv │ ├── owid-covid-data-part0098.csv │ ├── owid-covid-data-part0099.csv │ ├── owid-covid-data.csv │ └── total_cases.csv ├── oreilly_generator_intro.ipynb ├── owid-covid-data.csv └── working_data │ ├── README.md │ └── pima-indians-diabetes.data.csv ├── chapter03 ├── OReilly_C3_titanic_example.ipynb └── README.md ├── chapter04 ├── OReilly_C4_hub_intro_embedding.ipynb ├── Oreilly_chapter_4_transfer_learning.ipynb └── README.md ├── chapter05 ├── Oreilly_C5_flow_from_dataframe.ipynb ├── Oreilly_C5_flow_numpy.ipynb ├── Oreilly_C5_text_data_from_directory.ipynb └── README.md ├── chapter06 ├── README.md └── oreilly_c6_model_creation_style.ipynb ├── chapter07 ├── README.md ├── oreilly_c7_callbacks.ipynb ├── oreilly_c7_cifar10.ipynb ├── oreilly_c7_cifar10_Checkpoint.ipynb └── oreilly_c7_cifar10_Tensorboard.ipynb ├── chapter08 ├── OReilly_C8_Distributed_CIFAR.ipynb └── README.md ├── chapter09 ├── OReilly_C9_Distributed_CIFAR_V1.ipynb ├── OReilly_C9_TFS.ipynb └── README.md └── chapter10 ├── OReilly_C10_CIFAR10_Hyperparameter_Tuning.ipynb ├── Oreilly_C10_Titanic.ipynb └── README.md /README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung 4 | -------------------------------------------------------------------------------- /chapter01/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | There is no source code for this chapter. All content for chapter 1 are in the book. 4 | -------------------------------------------------------------------------------- /chapter02/.ipynb_checkpoints/OReilly_C2_ImageGenerator-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /chapter02/.ipynb_checkpoints/OReilly_C2_generator-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /chapter02/.ipynb_checkpoints/OReilly_C2_tf_dataset-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "base_pattern = 'dataset'\n", 19 | "file_pattern = 'owid-covid-data-part*'\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "! ls {base_pattern}" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 23, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "files = tf.io.gfile.glob(base_pattern + '/' + file_pattern)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "files" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 41, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "csv_dataset = tf.data.experimental.make_csv_dataset(files, \n", 56 | " header = True,\n", 57 | " batch_size = 5,\n", 58 | " label_name = 'new_deaths',\n", 59 | " num_epochs = 1,\n", 60 | " ignore_errors = True)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 45, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "'Target': [ 0. 0. 16. 0. 0.]\n", 73 | "'Features:'\n", 74 | " 'iso_code' : [b'SWZ' b'ESP' b'ECU' b'ISL' b'FRO']\n", 75 | " 'continent' : [b'Africa' b'Europe' b'South America' b'Europe' b'Europe']\n", 76 | " 'location' : [b'Swaziland' b'Spain' b'Ecuador' b'Iceland' b'Faeroe Islands']\n", 77 | " 'date' : [b'2020-04-04' b'2020-02-07' b'2020-07-13' b'2020-04-01' b'2020-06-11']\n", 78 | " 'total_cases' : [9.000e+00 1.000e+00 6.787e+04 1.135e+03 1.870e+02]\n", 79 | " 'new_cases' : [ 0. 0. 661. 49. 0.]\n", 80 | " 'total_deaths' : [0.000e+00 0.000e+00 5.047e+03 2.000e+00 0.000e+00]\n", 81 | " 'total_cases_per_million': [7.758000e+00 2.100000e-02 3.846838e+03 3.326007e+03 3.826870e+03]\n", 82 | " 'new_cases_per_million': [ 0. 0. 37.465 143.59 0. ]\n", 83 | " 'total_deaths_per_million': [ 0. 0. 286.061 5.861 0. ]\n", 84 | " 'new_deaths_per_million': [0. 0. 0.907 0. 0. ]\n", 85 | " 'new_tests' : [b'' b'' b'1331.0' b'1414.0' b'']\n", 86 | " 'total_tests' : [b'' b'' b'140602.0' b'20889.0' b'']\n", 87 | " 'total_tests_per_thousand': [b'' b'' b'7.969' b'61.213' b'']\n", 88 | " 'new_tests_per_thousand': [b'' b'' b'0.075' b'4.144' b'']\n", 89 | " 'new_tests_smoothed': [b'' b'' b'1986.0' b'1188.0' b'']\n", 90 | " 'new_tests_smoothed_per_thousand': [b'' b'' b'0.113' b'3.481' b'']\n", 91 | " 'tests_units' : [b'' b'' b'units unclear' b'tests performed' b'']\n", 92 | " 'stringency_index' : [89.81 11.11 82.41 53.7 0. ]\n", 93 | " 'population' : [ 1160164. 46754784. 17643060. 341250. 48865.]\n", 94 | " 'population_density': [79.492 93.105 66.939 3.404 35.308]\n", 95 | " 'median_age' : [21.5 45.5 28.1 37.3 0. ]\n", 96 | " 'aged_65_older' : [ 3.163 19.436 7.104 14.431 0. ]\n", 97 | " 'aged_70_older' : [ 1.845 13.799 4.458 9.207 0. ]\n", 98 | " 'gdp_per_capita' : [ 7738.975 34272.36 10581.936 46482.957 0. ]\n", 99 | " 'extreme_poverty' : [b'' b'1.0' b'3.6' b'0.2' b'']\n", 100 | " 'cardiovasc_death_rate': [333.436 99.403 140.448 117.992 0. ]\n", 101 | " 'diabetes_prevalence': [3.94 7.17 5.55 5.31 0. ]\n", 102 | " 'female_smokers' : [b'1.7' b'27.4' b'2.0' b'14.3' b'']\n", 103 | " 'male_smokers' : [b'16.5' b'31.4' b'12.3' b'15.2' b'']\n", 104 | " 'handwashing_facilities': [24.097 0. 80.635 0. 0. ]\n", 105 | " 'hospital_beds_per_thousand': [2.1 2.97 1.5 2.91 0. ]\n", 106 | " 'life_expectancy' : [60.19 83.56 77.01 82.99 80.67]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for features, target in csv_dataset.take(1):\n", 112 | " print(\"'Target': {}\".format(target))\n", 113 | " print(\"'Features:'\")\n", 114 | " for k, v in features.items():\n", 115 | " print(\" {!r:20s}: {}\".format(k, v))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 57, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "features, label = next(iter(csv_dataset))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 58, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/plain": [ 135 | "" 136 | ] 137 | }, 138 | "execution_count": 58, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "label" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 61, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "OrderedDict([('iso_code',\n", 156 | " ),\n", 157 | " ('continent',\n", 158 | " ),\n", 159 | " ('location',\n", 160 | " ),\n", 163 | " ('date',\n", 164 | " ),\n", 167 | " ('total_cases',\n", 168 | " ),\n", 169 | " ('new_cases',\n", 170 | " ),\n", 171 | " ('total_deaths',\n", 172 | " ),\n", 173 | " ('total_cases_per_million',\n", 174 | " ),\n", 177 | " ('new_cases_per_million',\n", 178 | " ),\n", 179 | " ('total_deaths_per_million',\n", 180 | " ),\n", 181 | " ('new_deaths_per_million',\n", 182 | " ),\n", 183 | " ('new_tests',\n", 184 | " ),\n", 185 | " ('total_tests',\n", 186 | " ),\n", 187 | " ('total_tests_per_thousand',\n", 188 | " ),\n", 189 | " ('new_tests_per_thousand',\n", 190 | " ),\n", 191 | " ('new_tests_smoothed',\n", 192 | " ),\n", 193 | " ('new_tests_smoothed_per_thousand',\n", 194 | " ),\n", 195 | " ('tests_units',\n", 196 | " ),\n", 197 | " ('stringency_index',\n", 198 | " ),\n", 199 | " ('population',\n", 200 | " ),\n", 203 | " ('population_density',\n", 204 | " ),\n", 205 | " ('median_age',\n", 206 | " ),\n", 207 | " ('aged_65_older',\n", 208 | " ),\n", 209 | " ('aged_70_older',\n", 210 | " ),\n", 211 | " ('gdp_per_capita',\n", 212 | " ),\n", 215 | " ('extreme_poverty',\n", 216 | " ),\n", 217 | " ('cardiovasc_death_rate',\n", 218 | " ),\n", 219 | " ('diabetes_prevalence',\n", 220 | " ),\n", 221 | " ('female_smokers',\n", 222 | " ),\n", 223 | " ('male_smokers',\n", 224 | " ),\n", 225 | " ('handwashing_facilities',\n", 226 | " ),\n", 227 | " ('hospital_beds_per_thousand',\n", 228 | " ),\n", 229 | " ('life_expectancy',\n", 230 | " )])" 231 | ] 232 | }, 233 | "execution_count": 61, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "features" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 54, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "features, label = next(iter(csv_dataset))" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 55, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "" 260 | ] 261 | }, 262 | "execution_count": 55, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "label" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 56, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "tensorflow.python.data.ops.dataset_ops.PrefetchDataset" 280 | ] 281 | }, 282 | "execution_count": 56, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "type(csv_dataset)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.8.5" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 4 320 | } 321 | -------------------------------------------------------------------------------- /chapter02/.ipynb_checkpoints/oreilly_tf_dataset-Copy1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "base_pattern = 'dataset'\n", 19 | "file_pattern = 'owid-covid-data-part*'\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "! ls {base_pattern}" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 23, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "files = tf.io.gfile.glob(base_pattern + '/' + file_pattern)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "files" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 41, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "csv_dataset = tf.data.experimental.make_csv_dataset(files, \n", 56 | " header = True,\n", 57 | " batch_size = 5,\n", 58 | " label_name = 'new_deaths',\n", 59 | " num_epochs = 1,\n", 60 | " ignore_errors = True)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 45, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "'Target': [ 0. 0. 16. 0. 0.]\n", 73 | "'Features:'\n", 74 | " 'iso_code' : [b'SWZ' b'ESP' b'ECU' b'ISL' b'FRO']\n", 75 | " 'continent' : [b'Africa' b'Europe' b'South America' b'Europe' b'Europe']\n", 76 | " 'location' : [b'Swaziland' b'Spain' b'Ecuador' b'Iceland' b'Faeroe Islands']\n", 77 | " 'date' : [b'2020-04-04' b'2020-02-07' b'2020-07-13' b'2020-04-01' b'2020-06-11']\n", 78 | " 'total_cases' : [9.000e+00 1.000e+00 6.787e+04 1.135e+03 1.870e+02]\n", 79 | " 'new_cases' : [ 0. 0. 661. 49. 0.]\n", 80 | " 'total_deaths' : [0.000e+00 0.000e+00 5.047e+03 2.000e+00 0.000e+00]\n", 81 | " 'total_cases_per_million': [7.758000e+00 2.100000e-02 3.846838e+03 3.326007e+03 3.826870e+03]\n", 82 | " 'new_cases_per_million': [ 0. 0. 37.465 143.59 0. ]\n", 83 | " 'total_deaths_per_million': [ 0. 0. 286.061 5.861 0. ]\n", 84 | " 'new_deaths_per_million': [0. 0. 0.907 0. 0. ]\n", 85 | " 'new_tests' : [b'' b'' b'1331.0' b'1414.0' b'']\n", 86 | " 'total_tests' : [b'' b'' b'140602.0' b'20889.0' b'']\n", 87 | " 'total_tests_per_thousand': [b'' b'' b'7.969' b'61.213' b'']\n", 88 | " 'new_tests_per_thousand': [b'' b'' b'0.075' b'4.144' b'']\n", 89 | " 'new_tests_smoothed': [b'' b'' b'1986.0' b'1188.0' b'']\n", 90 | " 'new_tests_smoothed_per_thousand': [b'' b'' b'0.113' b'3.481' b'']\n", 91 | " 'tests_units' : [b'' b'' b'units unclear' b'tests performed' b'']\n", 92 | " 'stringency_index' : [89.81 11.11 82.41 53.7 0. ]\n", 93 | " 'population' : [ 1160164. 46754784. 17643060. 341250. 48865.]\n", 94 | " 'population_density': [79.492 93.105 66.939 3.404 35.308]\n", 95 | " 'median_age' : [21.5 45.5 28.1 37.3 0. ]\n", 96 | " 'aged_65_older' : [ 3.163 19.436 7.104 14.431 0. ]\n", 97 | " 'aged_70_older' : [ 1.845 13.799 4.458 9.207 0. ]\n", 98 | " 'gdp_per_capita' : [ 7738.975 34272.36 10581.936 46482.957 0. ]\n", 99 | " 'extreme_poverty' : [b'' b'1.0' b'3.6' b'0.2' b'']\n", 100 | " 'cardiovasc_death_rate': [333.436 99.403 140.448 117.992 0. ]\n", 101 | " 'diabetes_prevalence': [3.94 7.17 5.55 5.31 0. ]\n", 102 | " 'female_smokers' : [b'1.7' b'27.4' b'2.0' b'14.3' b'']\n", 103 | " 'male_smokers' : [b'16.5' b'31.4' b'12.3' b'15.2' b'']\n", 104 | " 'handwashing_facilities': [24.097 0. 80.635 0. 0. ]\n", 105 | " 'hospital_beds_per_thousand': [2.1 2.97 1.5 2.91 0. ]\n", 106 | " 'life_expectancy' : [60.19 83.56 77.01 82.99 80.67]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for features, target in csv_dataset.take(1):\n", 112 | " print(\"'Target': {}\".format(target))\n", 113 | " print(\"'Features:'\")\n", 114 | " for k, v in features.items():\n", 115 | " print(\" {!r:20s}: {}\".format(k, v))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 57, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "features, label = next(iter(csv_dataset))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 58, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/plain": [ 135 | "" 136 | ] 137 | }, 138 | "execution_count": 58, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "label" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 61, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "OrderedDict([('iso_code',\n", 156 | " ),\n", 157 | " ('continent',\n", 158 | " ),\n", 159 | " ('location',\n", 160 | " ),\n", 163 | " ('date',\n", 164 | " ),\n", 167 | " ('total_cases',\n", 168 | " ),\n", 169 | " ('new_cases',\n", 170 | " ),\n", 171 | " ('total_deaths',\n", 172 | " ),\n", 173 | " ('total_cases_per_million',\n", 174 | " ),\n", 177 | " ('new_cases_per_million',\n", 178 | " ),\n", 179 | " ('total_deaths_per_million',\n", 180 | " ),\n", 181 | " ('new_deaths_per_million',\n", 182 | " ),\n", 183 | " ('new_tests',\n", 184 | " ),\n", 185 | " ('total_tests',\n", 186 | " ),\n", 187 | " ('total_tests_per_thousand',\n", 188 | " ),\n", 189 | " ('new_tests_per_thousand',\n", 190 | " ),\n", 191 | " ('new_tests_smoothed',\n", 192 | " ),\n", 193 | " ('new_tests_smoothed_per_thousand',\n", 194 | " ),\n", 195 | " ('tests_units',\n", 196 | " ),\n", 197 | " ('stringency_index',\n", 198 | " ),\n", 199 | " ('population',\n", 200 | " ),\n", 203 | " ('population_density',\n", 204 | " ),\n", 205 | " ('median_age',\n", 206 | " ),\n", 207 | " ('aged_65_older',\n", 208 | " ),\n", 209 | " ('aged_70_older',\n", 210 | " ),\n", 211 | " ('gdp_per_capita',\n", 212 | " ),\n", 215 | " ('extreme_poverty',\n", 216 | " ),\n", 217 | " ('cardiovasc_death_rate',\n", 218 | " ),\n", 219 | " ('diabetes_prevalence',\n", 220 | " ),\n", 221 | " ('female_smokers',\n", 222 | " ),\n", 223 | " ('male_smokers',\n", 224 | " ),\n", 225 | " ('handwashing_facilities',\n", 226 | " ),\n", 227 | " ('hospital_beds_per_thousand',\n", 228 | " ),\n", 229 | " ('life_expectancy',\n", 230 | " )])" 231 | ] 232 | }, 233 | "execution_count": 61, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "features" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 54, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "features, label = next(iter(csv_dataset))" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 55, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "" 260 | ] 261 | }, 262 | "execution_count": 55, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "label" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 56, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "tensorflow.python.data.ops.dataset_ops.PrefetchDataset" 280 | ] 281 | }, 282 | "execution_count": 56, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "type(csv_dataset)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.8.5" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 4 320 | } 321 | -------------------------------------------------------------------------------- /chapter02/OReilly_C2_tf_dataset.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "base_pattern = 'dataset'\n", 19 | "file_pattern = 'owid-covid-data-part*'\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "! ls {base_pattern}" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 23, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "files = tf.io.gfile.glob(base_pattern + '/' + file_pattern)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "files" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 41, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "csv_dataset = tf.data.experimental.make_csv_dataset(files, \n", 56 | " header = True,\n", 57 | " batch_size = 5,\n", 58 | " label_name = 'new_deaths',\n", 59 | " num_epochs = 1,\n", 60 | " ignore_errors = True)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 45, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "'Target': [ 0. 0. 16. 0. 0.]\n", 73 | "'Features:'\n", 74 | " 'iso_code' : [b'SWZ' b'ESP' b'ECU' b'ISL' b'FRO']\n", 75 | " 'continent' : [b'Africa' b'Europe' b'South America' b'Europe' b'Europe']\n", 76 | " 'location' : [b'Swaziland' b'Spain' b'Ecuador' b'Iceland' b'Faeroe Islands']\n", 77 | " 'date' : [b'2020-04-04' b'2020-02-07' b'2020-07-13' b'2020-04-01' b'2020-06-11']\n", 78 | " 'total_cases' : [9.000e+00 1.000e+00 6.787e+04 1.135e+03 1.870e+02]\n", 79 | " 'new_cases' : [ 0. 0. 661. 49. 0.]\n", 80 | " 'total_deaths' : [0.000e+00 0.000e+00 5.047e+03 2.000e+00 0.000e+00]\n", 81 | " 'total_cases_per_million': [7.758000e+00 2.100000e-02 3.846838e+03 3.326007e+03 3.826870e+03]\n", 82 | " 'new_cases_per_million': [ 0. 0. 37.465 143.59 0. ]\n", 83 | " 'total_deaths_per_million': [ 0. 0. 286.061 5.861 0. ]\n", 84 | " 'new_deaths_per_million': [0. 0. 0.907 0. 0. ]\n", 85 | " 'new_tests' : [b'' b'' b'1331.0' b'1414.0' b'']\n", 86 | " 'total_tests' : [b'' b'' b'140602.0' b'20889.0' b'']\n", 87 | " 'total_tests_per_thousand': [b'' b'' b'7.969' b'61.213' b'']\n", 88 | " 'new_tests_per_thousand': [b'' b'' b'0.075' b'4.144' b'']\n", 89 | " 'new_tests_smoothed': [b'' b'' b'1986.0' b'1188.0' b'']\n", 90 | " 'new_tests_smoothed_per_thousand': [b'' b'' b'0.113' b'3.481' b'']\n", 91 | " 'tests_units' : [b'' b'' b'units unclear' b'tests performed' b'']\n", 92 | " 'stringency_index' : [89.81 11.11 82.41 53.7 0. ]\n", 93 | " 'population' : [ 1160164. 46754784. 17643060. 341250. 48865.]\n", 94 | " 'population_density': [79.492 93.105 66.939 3.404 35.308]\n", 95 | " 'median_age' : [21.5 45.5 28.1 37.3 0. ]\n", 96 | " 'aged_65_older' : [ 3.163 19.436 7.104 14.431 0. ]\n", 97 | " 'aged_70_older' : [ 1.845 13.799 4.458 9.207 0. ]\n", 98 | " 'gdp_per_capita' : [ 7738.975 34272.36 10581.936 46482.957 0. ]\n", 99 | " 'extreme_poverty' : [b'' b'1.0' b'3.6' b'0.2' b'']\n", 100 | " 'cardiovasc_death_rate': [333.436 99.403 140.448 117.992 0. ]\n", 101 | " 'diabetes_prevalence': [3.94 7.17 5.55 5.31 0. ]\n", 102 | " 'female_smokers' : [b'1.7' b'27.4' b'2.0' b'14.3' b'']\n", 103 | " 'male_smokers' : [b'16.5' b'31.4' b'12.3' b'15.2' b'']\n", 104 | " 'handwashing_facilities': [24.097 0. 80.635 0. 0. ]\n", 105 | " 'hospital_beds_per_thousand': [2.1 2.97 1.5 2.91 0. ]\n", 106 | " 'life_expectancy' : [60.19 83.56 77.01 82.99 80.67]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for features, target in csv_dataset.take(1):\n", 112 | " print(\"'Target': {}\".format(target))\n", 113 | " print(\"'Features:'\")\n", 114 | " for k, v in features.items():\n", 115 | " print(\" {!r:20s}: {}\".format(k, v))" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 57, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "features, label = next(iter(csv_dataset))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 58, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/plain": [ 135 | "" 136 | ] 137 | }, 138 | "execution_count": 58, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "label" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 61, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "OrderedDict([('iso_code',\n", 156 | " ),\n", 157 | " ('continent',\n", 158 | " ),\n", 159 | " ('location',\n", 160 | " ),\n", 163 | " ('date',\n", 164 | " ),\n", 167 | " ('total_cases',\n", 168 | " ),\n", 169 | " ('new_cases',\n", 170 | " ),\n", 171 | " ('total_deaths',\n", 172 | " ),\n", 173 | " ('total_cases_per_million',\n", 174 | " ),\n", 177 | " ('new_cases_per_million',\n", 178 | " ),\n", 179 | " ('total_deaths_per_million',\n", 180 | " ),\n", 181 | " ('new_deaths_per_million',\n", 182 | " ),\n", 183 | " ('new_tests',\n", 184 | " ),\n", 185 | " ('total_tests',\n", 186 | " ),\n", 187 | " ('total_tests_per_thousand',\n", 188 | " ),\n", 189 | " ('new_tests_per_thousand',\n", 190 | " ),\n", 191 | " ('new_tests_smoothed',\n", 192 | " ),\n", 193 | " ('new_tests_smoothed_per_thousand',\n", 194 | " ),\n", 195 | " ('tests_units',\n", 196 | " ),\n", 197 | " ('stringency_index',\n", 198 | " ),\n", 199 | " ('population',\n", 200 | " ),\n", 203 | " ('population_density',\n", 204 | " ),\n", 205 | " ('median_age',\n", 206 | " ),\n", 207 | " ('aged_65_older',\n", 208 | " ),\n", 209 | " ('aged_70_older',\n", 210 | " ),\n", 211 | " ('gdp_per_capita',\n", 212 | " ),\n", 215 | " ('extreme_poverty',\n", 216 | " ),\n", 217 | " ('cardiovasc_death_rate',\n", 218 | " ),\n", 219 | " ('diabetes_prevalence',\n", 220 | " ),\n", 221 | " ('female_smokers',\n", 222 | " ),\n", 223 | " ('male_smokers',\n", 224 | " ),\n", 225 | " ('handwashing_facilities',\n", 226 | " ),\n", 227 | " ('hospital_beds_per_thousand',\n", 228 | " ),\n", 229 | " ('life_expectancy',\n", 230 | " )])" 231 | ] 232 | }, 233 | "execution_count": 61, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "features" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 54, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "features, label = next(iter(csv_dataset))" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 55, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "" 260 | ] 261 | }, 262 | "execution_count": 55, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "label" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 56, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "tensorflow.python.data.ops.dataset_ops.PrefetchDataset" 280 | ] 281 | }, 282 | "execution_count": 56, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "type(csv_dataset)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.8.5" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 4 320 | } 321 | -------------------------------------------------------------------------------- /chapter02/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter02/dataset/CaseCountData.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinchan75034/tensorflow-pocket-ref/b30f9b76d988209fdaf763b4fd9d99369b27c6b5/chapter02/dataset/CaseCountData.xlsx -------------------------------------------------------------------------------- /chapter02/dataset/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter02/dataset/owid-covid-data-part00100.csv: -------------------------------------------------------------------------------- 1 | iso_code,continent,location,date,total_cases,new_cases,total_deaths,new_deaths,total_cases_per_million,new_cases_per_million,total_deaths_per_million,new_deaths_per_million,new_tests,total_tests,total_tests_per_thousand,new_tests_per_thousand,new_tests_smoothed,new_tests_smoothed_per_thousand,tests_units,stringency_index,population,population_density,median_age,aged_65_older,aged_70_older,gdp_per_capita,extreme_poverty,cardiovasc_death_rate,diabetes_prevalence,female_smokers,male_smokers,handwashing_facilities,hospital_beds_per_thousand,life_expectancy 2 | OWID_WRL,,World,2020-06-04,6476357.0,126718.0,385779.0,5655.0,830.856,16.257,49.492,0.725,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 3 | OWID_WRL,,World,2020-06-05,6604110.0,127753.0,390956.0,5177.0,847.246,16.39,50.156,0.664,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 4 | OWID_WRL,,World,2020-06-06,6737646.0,133536.0,395734.0,4778.0,864.377,17.131,50.769,0.613,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 5 | OWID_WRL,,World,2020-06-07,6863479.0,125833.0,399553.0,3819.0,880.52,16.143,51.259,0.49,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 6 | OWID_WRL,,World,2020-06-08,6979773.0,116294.0,403056.0,3503.0,895.44,14.919,51.708,0.449,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 7 | OWID_WRL,,World,2020-06-09,7085494.0,105721.0,406279.0,3223.0,909.003,13.563,52.122,0.413,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 8 | OWID_WRL,,World,2020-06-10,7211151.0,125657.0,411210.0,4931.0,925.123,16.121,52.754,0.633,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 9 | OWID_WRL,,World,2020-06-11,7344591.0,133440.0,416451.0,5241.0,942.243,17.119,53.427,0.672,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 10 | OWID_WRL,,World,2020-06-12,7482121.0,137530.0,421213.0,4762.0,959.886,17.644,54.038,0.611,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 11 | OWID_WRL,,World,2020-06-13,7626625.0,144504.0,425947.0,4734.0,978.425,18.539,54.645,0.607,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 12 | OWID_WRL,,World,2020-06-14,7760685.0,134060.0,430148.0,4201.0,995.624,17.199,55.184,0.539,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 13 | OWID_WRL,,World,2020-06-15,7882719.0,122034.0,433276.0,3128.0,1011.279,15.656,55.585,0.401,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 14 | OWID_WRL,,World,2020-06-16,8001568.0,118849.0,436651.0,3375.0,1026.527,15.247,56.018,0.433,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 15 | OWID_WRL,,World,2020-06-17,8143056.0,141488.0,443508.0,6857.0,1044.678,18.152,56.898,0.88,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 16 | OWID_WRL,,World,2020-06-18,8319594.0,176538.0,448753.0,5245.0,1067.326,22.648,57.571,0.673,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 17 | OWID_WRL,,World,2020-06-19,8458374.0,138780.0,455076.0,6323.0,1085.131,17.804,58.382,0.811,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 18 | OWID_WRL,,World,2020-06-20,8635697.0,177323.0,460091.0,5015.0,1107.879,22.749,59.025,0.643,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 19 | OWID_WRL,,World,2020-06-21,8797688.0,161991.0,464303.0,4212.0,1128.661,20.782,59.566,0.54,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 20 | OWID_WRL,,World,2020-06-22,8927658.0,129970.0,468278.0,3975.0,1145.335,16.674,60.076,0.51,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 21 | OWID_WRL,,World,2020-06-23,9063888.0,136230.0,471711.0,3433.0,1162.812,17.477,60.516,0.44,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 22 | OWID_WRL,,World,2020-06-24,9229757.0,165869.0,477289.0,5578.0,1184.092,21.279,61.232,0.716,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 23 | OWID_WRL,,World,2020-06-25,9401175.0,171418.0,482485.0,5196.0,1206.083,21.991,61.898,0.667,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 24 | OWID_WRL,,World,2020-06-26,9582733.0,181558.0,489206.0,6721.0,1229.375,23.292,62.761,0.862,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 25 | OWID_WRL,,World,2020-06-27,9772106.0,189373.0,493912.0,4706.0,1253.67,24.295,63.364,0.604,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 26 | OWID_WRL,,World,2020-06-28,9953597.0,181491.0,498548.0,4636.0,1276.954,23.284,63.959,0.595,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 27 | OWID_WRL,,World,2020-06-29,10113636.0,160039.0,501600.0,3052.0,1297.485,20.532,64.351,0.392,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 28 | OWID_WRL,,World,2020-06-30,10273550.0,159914.0,505312.0,3712.0,1318.001,20.515,64.827,0.476,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 29 | OWID_WRL,,World,2020-07-01,10466131.0,192581.0,511054.0,5742.0,1342.707,24.706,65.563,0.737,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 30 | OWID_WRL,,World,2020-07-02,10666536.0,200405.0,515984.0,4930.0,1368.417,25.71,66.196,0.632,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 31 | OWID_WRL,,World,2020-07-03,10844974.0,178438.0,521131.0,5147.0,1391.309,22.892,66.856,0.66,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 32 | OWID_WRL,,World,2020-07-04,11051513.0,206539.0,526248.0,5117.0,1417.806,26.497,67.513,0.656,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 33 | OWID_WRL,,World,2020-07-05,11241216.0,189703.0,530592.0,4344.0,1442.143,24.337,68.07,0.557,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 34 | OWID_WRL,,World,2020-07-06,11420214.0,178998.0,533977.0,3385.0,1465.107,22.964,68.504,0.434,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 35 | OWID_WRL,,World,2020-07-07,11595683.0,175469.0,537824.0,3847.0,1487.618,22.511,68.998,0.494,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 36 | OWID_WRL,,World,2020-07-08,11802571.0,206888.0,543919.0,6095.0,1514.16,26.542,69.78,0.782,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 37 | OWID_WRL,,World,2020-07-09,12018097.0,215526.0,549291.0,5372.0,1541.81,27.65,70.469,0.689,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 38 | OWID_WRL,,World,2020-07-10,12243312.0,225215.0,554731.0,5440.0,1570.703,28.893,71.167,0.698,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 39 | OWID_WRL,,World,2020-07-11,12473581.0,230269.0,560006.0,5275.0,1600.244,29.541,71.844,0.677,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 40 | OWID_WRL,,World,2020-07-12,12691869.0,218288.0,564935.0,4929.0,1628.248,28.004,72.476,0.632,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 41 | OWID_WRL,,World,2020-07-13,12887565.0,195696.0,568752.0,3817.0,1653.354,25.106,72.966,0.49,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 42 | OWID_WRL,,World,2020-07-14,13077307.0,189742.0,572674.0,3922.0,1677.697,24.342,73.469,0.503,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 43 | OWID_WRL,,World,2020-07-15,13300974.0,223667.0,578334.0,5660.0,1706.391,28.694,74.195,0.726,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 44 | OWID_WRL,,World,2020-07-16,13532689.0,231715.0,583911.0,5577.0,1736.118,29.727,74.91,0.715,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 45 | OWID_WRL,,World,2020-07-17,13787160.0,254471.0,589714.0,5803.0,1768.764,32.646,75.655,0.744,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 46 | OWID_WRL,,World,2020-07-18,14040790.0,253630.0,597158.0,7444.0,1801.302,32.538,76.61,0.955,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 47 | OWID_WRL,,World,2020-07-19,14269374.0,228584.0,602049.0,4891.0,1830.628,29.325,77.237,0.627,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 48 | OWID_WRL,,World,2020-07-20,14481532.0,212158.0,605994.0,3945.0,1857.846,27.218,77.743,0.506,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 49 | OWID_WRL,,World,2020-07-21,14683202.0,201670.0,610104.0,4110.0,1883.718,25.872,78.271,0.527,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 50 | OWID_WRL,,World,2020-07-22,14922665.0,239463.0,616324.0,6220.0,1914.439,30.721,79.069,0.798,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 51 | OWID_WRL,,World,2020-07-23,15204874.0,282209.0,623290.0,6966.0,1950.644,36.205,79.962,0.894,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 52 | OWID_WRL,,World,2020-07-24,15480762.0,275888.0,633126.0,9836.0,1986.037,35.394,81.224,1.262,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 53 | OWID_WRL,,World,2020-07-25,15762581.0,281819.0,639275.0,6149.0,2022.192,36.155,82.013,0.789,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 54 | OWID_WRL,,World,2020-07-26,16018105.0,255524.0,644832.0,5557.0,2054.974,32.781,82.726,0.713,,,,,,,,,7794798729.0,58.045,30.9,8.696,5.355,15469.207,10.0,233.07,8.51,6.434,34.635,60.13,2.705,72.58 55 | ,,International,2019-12-31,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 56 | ,,International,2020-01-01,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 57 | ,,International,2020-01-02,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 58 | ,,International,2020-01-03,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 59 | ,,International,2020-01-04,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 60 | ,,International,2020-01-05,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 61 | ,,International,2020-01-06,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 62 | ,,International,2020-01-07,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 63 | ,,International,2020-01-08,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 64 | ,,International,2020-01-09,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 65 | ,,International,2020-01-10,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 66 | ,,International,2020-01-11,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 67 | ,,International,2020-01-12,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 68 | ,,International,2020-01-13,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 69 | ,,International,2020-01-14,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 70 | ,,International,2020-01-15,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 71 | ,,International,2020-01-16,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 72 | ,,International,2020-01-17,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 73 | ,,International,2020-01-18,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 74 | ,,International,2020-01-19,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 75 | ,,International,2020-01-20,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 76 | ,,International,2020-01-21,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 77 | ,,International,2020-01-22,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 78 | ,,International,2020-01-23,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 79 | ,,International,2020-01-24,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 80 | ,,International,2020-01-25,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 81 | ,,International,2020-01-26,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 82 | ,,International,2020-01-27,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 83 | ,,International,2020-01-28,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 84 | ,,International,2020-01-29,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 85 | ,,International,2020-01-30,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 86 | ,,International,2020-01-31,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 87 | ,,International,2020-02-01,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 88 | ,,International,2020-02-02,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 89 | ,,International,2020-02-03,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 90 | ,,International,2020-02-04,0.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 91 | ,,International,2020-02-05,10.0,10.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 92 | ,,International,2020-02-06,20.0,10.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 93 | ,,International,2020-02-07,61.0,41.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 94 | ,,International,2020-02-08,64.0,3.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 95 | ,,International,2020-02-09,64.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 96 | ,,International,2020-02-10,70.0,6.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 97 | ,,International,2020-02-11,135.0,65.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 98 | ,,International,2020-02-12,174.0,39.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 99 | ,,International,2020-02-13,174.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 100 | ,,International,2020-02-14,221.0,47.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 101 | ,,International,2020-02-15,221.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 102 | ,,International,2020-02-16,355.0,134.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 103 | ,,International,2020-02-17,355.0,0.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 104 | ,,International,2020-02-18,454.0,99.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 105 | ,,International,2020-02-19,542.0,88.0,0.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 106 | ,,International,2020-02-20,621.0,79.0,2.0,2.0,,,,,,,,,,,,,,,,,,,,,,,,,, 107 | ,,International,2020-02-21,634.0,13.0,2.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 108 | ,,International,2020-02-22,634.0,0.0,2.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 109 | ,,International,2020-02-23,634.0,0.0,2.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 110 | ,,International,2020-02-24,691.0,57.0,3.0,1.0,,,,,,,,,,,,,,,,,,,,,,,,,, 111 | ,,International,2020-02-25,691.0,0.0,3.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 112 | ,,International,2020-02-26,691.0,0.0,4.0,1.0,,,,,,,,,,,,,,,,,,,,,,,,,, 113 | ,,International,2020-02-27,705.0,14.0,4.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 114 | ,,International,2020-02-28,705.0,0.0,4.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 115 | ,,International,2020-02-29,705.0,0.0,6.0,2.0,,,,,,,,,,,,,,,,,,,,,,,,,, 116 | ,,International,2020-03-01,705.0,0.0,6.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 117 | ,,International,2020-03-02,705.0,0.0,6.0,0.0,,,,,,,,,,,,,,,,,,,,,,,,,, 118 | ,,International,2020-03-10,696.0,-9.0,7.0,1.0,,,,,,,,,,,,,,,,,,,,,,,,,, 119 | -------------------------------------------------------------------------------- /chapter02/working_data/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter02/working_data/pima-indians-diabetes.data.csv: -------------------------------------------------------------------------------- 1 | 6,148,72,35,0,33.6,0.627,50,1 2 | 1,85,66,29,0,26.6,0.351,31,0 3 | 8,183,64,0,0,23.3,0.672,32,1 4 | 1,89,66,23,94,28.1,0.167,21,0 5 | 0,137,40,35,168,43.1,2.288,33,1 6 | 5,116,74,0,0,25.6,0.201,30,0 7 | 3,78,50,32,88,31.0,0.248,26,1 8 | 10,115,0,0,0,35.3,0.134,29,0 9 | 2,197,70,45,543,30.5,0.158,53,1 10 | 8,125,96,0,0,0.0,0.232,54,1 11 | 4,110,92,0,0,37.6,0.191,30,0 12 | 10,168,74,0,0,38.0,0.537,34,1 13 | 10,139,80,0,0,27.1,1.441,57,0 14 | 1,189,60,23,846,30.1,0.398,59,1 15 | 5,166,72,19,175,25.8,0.587,51,1 16 | 7,100,0,0,0,30.0,0.484,32,1 17 | 0,118,84,47,230,45.8,0.551,31,1 18 | 7,107,74,0,0,29.6,0.254,31,1 19 | 1,103,30,38,83,43.3,0.183,33,0 20 | 1,115,70,30,96,34.6,0.529,32,1 21 | 3,126,88,41,235,39.3,0.704,27,0 22 | 8,99,84,0,0,35.4,0.388,50,0 23 | 7,196,90,0,0,39.8,0.451,41,1 24 | 9,119,80,35,0,29.0,0.263,29,1 25 | 11,143,94,33,146,36.6,0.254,51,1 26 | 10,125,70,26,115,31.1,0.205,41,1 27 | 7,147,76,0,0,39.4,0.257,43,1 28 | 1,97,66,15,140,23.2,0.487,22,0 29 | 13,145,82,19,110,22.2,0.245,57,0 30 | 5,117,92,0,0,34.1,0.337,38,0 31 | 5,109,75,26,0,36.0,0.546,60,0 32 | 3,158,76,36,245,31.6,0.851,28,1 33 | 3,88,58,11,54,24.8,0.267,22,0 34 | 6,92,92,0,0,19.9,0.188,28,0 35 | 10,122,78,31,0,27.6,0.512,45,0 36 | 4,103,60,33,192,24.0,0.966,33,0 37 | 11,138,76,0,0,33.2,0.420,35,0 38 | 9,102,76,37,0,32.9,0.665,46,1 39 | 2,90,68,42,0,38.2,0.503,27,1 40 | 4,111,72,47,207,37.1,1.390,56,1 41 | 3,180,64,25,70,34.0,0.271,26,0 42 | 7,133,84,0,0,40.2,0.696,37,0 43 | 7,106,92,18,0,22.7,0.235,48,0 44 | 9,171,110,24,240,45.4,0.721,54,1 45 | 7,159,64,0,0,27.4,0.294,40,0 46 | 0,180,66,39,0,42.0,1.893,25,1 47 | 1,146,56,0,0,29.7,0.564,29,0 48 | 2,71,70,27,0,28.0,0.586,22,0 49 | 7,103,66,32,0,39.1,0.344,31,1 50 | 7,105,0,0,0,0.0,0.305,24,0 51 | 1,103,80,11,82,19.4,0.491,22,0 52 | 1,101,50,15,36,24.2,0.526,26,0 53 | 5,88,66,21,23,24.4,0.342,30,0 54 | 8,176,90,34,300,33.7,0.467,58,1 55 | 7,150,66,42,342,34.7,0.718,42,0 56 | 1,73,50,10,0,23.0,0.248,21,0 57 | 7,187,68,39,304,37.7,0.254,41,1 58 | 0,100,88,60,110,46.8,0.962,31,0 59 | 0,146,82,0,0,40.5,1.781,44,0 60 | 0,105,64,41,142,41.5,0.173,22,0 61 | 2,84,0,0,0,0.0,0.304,21,0 62 | 8,133,72,0,0,32.9,0.270,39,1 63 | 5,44,62,0,0,25.0,0.587,36,0 64 | 2,141,58,34,128,25.4,0.699,24,0 65 | 7,114,66,0,0,32.8,0.258,42,1 66 | 5,99,74,27,0,29.0,0.203,32,0 67 | 0,109,88,30,0,32.5,0.855,38,1 68 | 2,109,92,0,0,42.7,0.845,54,0 69 | 1,95,66,13,38,19.6,0.334,25,0 70 | 4,146,85,27,100,28.9,0.189,27,0 71 | 2,100,66,20,90,32.9,0.867,28,1 72 | 5,139,64,35,140,28.6,0.411,26,0 73 | 13,126,90,0,0,43.4,0.583,42,1 74 | 4,129,86,20,270,35.1,0.231,23,0 75 | 1,79,75,30,0,32.0,0.396,22,0 76 | 1,0,48,20,0,24.7,0.140,22,0 77 | 7,62,78,0,0,32.6,0.391,41,0 78 | 5,95,72,33,0,37.7,0.370,27,0 79 | 0,131,0,0,0,43.2,0.270,26,1 80 | 2,112,66,22,0,25.0,0.307,24,0 81 | 3,113,44,13,0,22.4,0.140,22,0 82 | 2,74,0,0,0,0.0,0.102,22,0 83 | 7,83,78,26,71,29.3,0.767,36,0 84 | 0,101,65,28,0,24.6,0.237,22,0 85 | 5,137,108,0,0,48.8,0.227,37,1 86 | 2,110,74,29,125,32.4,0.698,27,0 87 | 13,106,72,54,0,36.6,0.178,45,0 88 | 2,100,68,25,71,38.5,0.324,26,0 89 | 15,136,70,32,110,37.1,0.153,43,1 90 | 1,107,68,19,0,26.5,0.165,24,0 91 | 1,80,55,0,0,19.1,0.258,21,0 92 | 4,123,80,15,176,32.0,0.443,34,0 93 | 7,81,78,40,48,46.7,0.261,42,0 94 | 4,134,72,0,0,23.8,0.277,60,1 95 | 2,142,82,18,64,24.7,0.761,21,0 96 | 6,144,72,27,228,33.9,0.255,40,0 97 | 2,92,62,28,0,31.6,0.130,24,0 98 | 1,71,48,18,76,20.4,0.323,22,0 99 | 6,93,50,30,64,28.7,0.356,23,0 100 | 1,122,90,51,220,49.7,0.325,31,1 101 | 1,163,72,0,0,39.0,1.222,33,1 102 | 1,151,60,0,0,26.1,0.179,22,0 103 | 0,125,96,0,0,22.5,0.262,21,0 104 | 1,81,72,18,40,26.6,0.283,24,0 105 | 2,85,65,0,0,39.6,0.930,27,0 106 | 1,126,56,29,152,28.7,0.801,21,0 107 | 1,96,122,0,0,22.4,0.207,27,0 108 | 4,144,58,28,140,29.5,0.287,37,0 109 | 3,83,58,31,18,34.3,0.336,25,0 110 | 0,95,85,25,36,37.4,0.247,24,1 111 | 3,171,72,33,135,33.3,0.199,24,1 112 | 8,155,62,26,495,34.0,0.543,46,1 113 | 1,89,76,34,37,31.2,0.192,23,0 114 | 4,76,62,0,0,34.0,0.391,25,0 115 | 7,160,54,32,175,30.5,0.588,39,1 116 | 4,146,92,0,0,31.2,0.539,61,1 117 | 5,124,74,0,0,34.0,0.220,38,1 118 | 5,78,48,0,0,33.7,0.654,25,0 119 | 4,97,60,23,0,28.2,0.443,22,0 120 | 4,99,76,15,51,23.2,0.223,21,0 121 | 0,162,76,56,100,53.2,0.759,25,1 122 | 6,111,64,39,0,34.2,0.260,24,0 123 | 2,107,74,30,100,33.6,0.404,23,0 124 | 5,132,80,0,0,26.8,0.186,69,0 125 | 0,113,76,0,0,33.3,0.278,23,1 126 | 1,88,30,42,99,55.0,0.496,26,1 127 | 3,120,70,30,135,42.9,0.452,30,0 128 | 1,118,58,36,94,33.3,0.261,23,0 129 | 1,117,88,24,145,34.5,0.403,40,1 130 | 0,105,84,0,0,27.9,0.741,62,1 131 | 4,173,70,14,168,29.7,0.361,33,1 132 | 9,122,56,0,0,33.3,1.114,33,1 133 | 3,170,64,37,225,34.5,0.356,30,1 134 | 8,84,74,31,0,38.3,0.457,39,0 135 | 2,96,68,13,49,21.1,0.647,26,0 136 | 2,125,60,20,140,33.8,0.088,31,0 137 | 0,100,70,26,50,30.8,0.597,21,0 138 | 0,93,60,25,92,28.7,0.532,22,0 139 | 0,129,80,0,0,31.2,0.703,29,0 140 | 5,105,72,29,325,36.9,0.159,28,0 141 | 3,128,78,0,0,21.1,0.268,55,0 142 | 5,106,82,30,0,39.5,0.286,38,0 143 | 2,108,52,26,63,32.5,0.318,22,0 144 | 10,108,66,0,0,32.4,0.272,42,1 145 | 4,154,62,31,284,32.8,0.237,23,0 146 | 0,102,75,23,0,0.0,0.572,21,0 147 | 9,57,80,37,0,32.8,0.096,41,0 148 | 2,106,64,35,119,30.5,1.400,34,0 149 | 5,147,78,0,0,33.7,0.218,65,0 150 | 2,90,70,17,0,27.3,0.085,22,0 151 | 1,136,74,50,204,37.4,0.399,24,0 152 | 4,114,65,0,0,21.9,0.432,37,0 153 | 9,156,86,28,155,34.3,1.189,42,1 154 | 1,153,82,42,485,40.6,0.687,23,0 155 | 8,188,78,0,0,47.9,0.137,43,1 156 | 7,152,88,44,0,50.0,0.337,36,1 157 | 2,99,52,15,94,24.6,0.637,21,0 158 | 1,109,56,21,135,25.2,0.833,23,0 159 | 2,88,74,19,53,29.0,0.229,22,0 160 | 17,163,72,41,114,40.9,0.817,47,1 161 | 4,151,90,38,0,29.7,0.294,36,0 162 | 7,102,74,40,105,37.2,0.204,45,0 163 | 0,114,80,34,285,44.2,0.167,27,0 164 | 2,100,64,23,0,29.7,0.368,21,0 165 | 0,131,88,0,0,31.6,0.743,32,1 166 | 6,104,74,18,156,29.9,0.722,41,1 167 | 3,148,66,25,0,32.5,0.256,22,0 168 | 4,120,68,0,0,29.6,0.709,34,0 169 | 4,110,66,0,0,31.9,0.471,29,0 170 | 3,111,90,12,78,28.4,0.495,29,0 171 | 6,102,82,0,0,30.8,0.180,36,1 172 | 6,134,70,23,130,35.4,0.542,29,1 173 | 2,87,0,23,0,28.9,0.773,25,0 174 | 1,79,60,42,48,43.5,0.678,23,0 175 | 2,75,64,24,55,29.7,0.370,33,0 176 | 8,179,72,42,130,32.7,0.719,36,1 177 | 6,85,78,0,0,31.2,0.382,42,0 178 | 0,129,110,46,130,67.1,0.319,26,1 179 | 5,143,78,0,0,45.0,0.190,47,0 180 | 5,130,82,0,0,39.1,0.956,37,1 181 | 6,87,80,0,0,23.2,0.084,32,0 182 | 0,119,64,18,92,34.9,0.725,23,0 183 | 1,0,74,20,23,27.7,0.299,21,0 184 | 5,73,60,0,0,26.8,0.268,27,0 185 | 4,141,74,0,0,27.6,0.244,40,0 186 | 7,194,68,28,0,35.9,0.745,41,1 187 | 8,181,68,36,495,30.1,0.615,60,1 188 | 1,128,98,41,58,32.0,1.321,33,1 189 | 8,109,76,39,114,27.9,0.640,31,1 190 | 5,139,80,35,160,31.6,0.361,25,1 191 | 3,111,62,0,0,22.6,0.142,21,0 192 | 9,123,70,44,94,33.1,0.374,40,0 193 | 7,159,66,0,0,30.4,0.383,36,1 194 | 11,135,0,0,0,52.3,0.578,40,1 195 | 8,85,55,20,0,24.4,0.136,42,0 196 | 5,158,84,41,210,39.4,0.395,29,1 197 | 1,105,58,0,0,24.3,0.187,21,0 198 | 3,107,62,13,48,22.9,0.678,23,1 199 | 4,109,64,44,99,34.8,0.905,26,1 200 | 4,148,60,27,318,30.9,0.150,29,1 201 | 0,113,80,16,0,31.0,0.874,21,0 202 | 1,138,82,0,0,40.1,0.236,28,0 203 | 0,108,68,20,0,27.3,0.787,32,0 204 | 2,99,70,16,44,20.4,0.235,27,0 205 | 6,103,72,32,190,37.7,0.324,55,0 206 | 5,111,72,28,0,23.9,0.407,27,0 207 | 8,196,76,29,280,37.5,0.605,57,1 208 | 5,162,104,0,0,37.7,0.151,52,1 209 | 1,96,64,27,87,33.2,0.289,21,0 210 | 7,184,84,33,0,35.5,0.355,41,1 211 | 2,81,60,22,0,27.7,0.290,25,0 212 | 0,147,85,54,0,42.8,0.375,24,0 213 | 7,179,95,31,0,34.2,0.164,60,0 214 | 0,140,65,26,130,42.6,0.431,24,1 215 | 9,112,82,32,175,34.2,0.260,36,1 216 | 12,151,70,40,271,41.8,0.742,38,1 217 | 5,109,62,41,129,35.8,0.514,25,1 218 | 6,125,68,30,120,30.0,0.464,32,0 219 | 5,85,74,22,0,29.0,1.224,32,1 220 | 5,112,66,0,0,37.8,0.261,41,1 221 | 0,177,60,29,478,34.6,1.072,21,1 222 | 2,158,90,0,0,31.6,0.805,66,1 223 | 7,119,0,0,0,25.2,0.209,37,0 224 | 7,142,60,33,190,28.8,0.687,61,0 225 | 1,100,66,15,56,23.6,0.666,26,0 226 | 1,87,78,27,32,34.6,0.101,22,0 227 | 0,101,76,0,0,35.7,0.198,26,0 228 | 3,162,52,38,0,37.2,0.652,24,1 229 | 4,197,70,39,744,36.7,2.329,31,0 230 | 0,117,80,31,53,45.2,0.089,24,0 231 | 4,142,86,0,0,44.0,0.645,22,1 232 | 6,134,80,37,370,46.2,0.238,46,1 233 | 1,79,80,25,37,25.4,0.583,22,0 234 | 4,122,68,0,0,35.0,0.394,29,0 235 | 3,74,68,28,45,29.7,0.293,23,0 236 | 4,171,72,0,0,43.6,0.479,26,1 237 | 7,181,84,21,192,35.9,0.586,51,1 238 | 0,179,90,27,0,44.1,0.686,23,1 239 | 9,164,84,21,0,30.8,0.831,32,1 240 | 0,104,76,0,0,18.4,0.582,27,0 241 | 1,91,64,24,0,29.2,0.192,21,0 242 | 4,91,70,32,88,33.1,0.446,22,0 243 | 3,139,54,0,0,25.6,0.402,22,1 244 | 6,119,50,22,176,27.1,1.318,33,1 245 | 2,146,76,35,194,38.2,0.329,29,0 246 | 9,184,85,15,0,30.0,1.213,49,1 247 | 10,122,68,0,0,31.2,0.258,41,0 248 | 0,165,90,33,680,52.3,0.427,23,0 249 | 9,124,70,33,402,35.4,0.282,34,0 250 | 1,111,86,19,0,30.1,0.143,23,0 251 | 9,106,52,0,0,31.2,0.380,42,0 252 | 2,129,84,0,0,28.0,0.284,27,0 253 | 2,90,80,14,55,24.4,0.249,24,0 254 | 0,86,68,32,0,35.8,0.238,25,0 255 | 12,92,62,7,258,27.6,0.926,44,1 256 | 1,113,64,35,0,33.6,0.543,21,1 257 | 3,111,56,39,0,30.1,0.557,30,0 258 | 2,114,68,22,0,28.7,0.092,25,0 259 | 1,193,50,16,375,25.9,0.655,24,0 260 | 11,155,76,28,150,33.3,1.353,51,1 261 | 3,191,68,15,130,30.9,0.299,34,0 262 | 3,141,0,0,0,30.0,0.761,27,1 263 | 4,95,70,32,0,32.1,0.612,24,0 264 | 3,142,80,15,0,32.4,0.200,63,0 265 | 4,123,62,0,0,32.0,0.226,35,1 266 | 5,96,74,18,67,33.6,0.997,43,0 267 | 0,138,0,0,0,36.3,0.933,25,1 268 | 2,128,64,42,0,40.0,1.101,24,0 269 | 0,102,52,0,0,25.1,0.078,21,0 270 | 2,146,0,0,0,27.5,0.240,28,1 271 | 10,101,86,37,0,45.6,1.136,38,1 272 | 2,108,62,32,56,25.2,0.128,21,0 273 | 3,122,78,0,0,23.0,0.254,40,0 274 | 1,71,78,50,45,33.2,0.422,21,0 275 | 13,106,70,0,0,34.2,0.251,52,0 276 | 2,100,70,52,57,40.5,0.677,25,0 277 | 7,106,60,24,0,26.5,0.296,29,1 278 | 0,104,64,23,116,27.8,0.454,23,0 279 | 5,114,74,0,0,24.9,0.744,57,0 280 | 2,108,62,10,278,25.3,0.881,22,0 281 | 0,146,70,0,0,37.9,0.334,28,1 282 | 10,129,76,28,122,35.9,0.280,39,0 283 | 7,133,88,15,155,32.4,0.262,37,0 284 | 7,161,86,0,0,30.4,0.165,47,1 285 | 2,108,80,0,0,27.0,0.259,52,1 286 | 7,136,74,26,135,26.0,0.647,51,0 287 | 5,155,84,44,545,38.7,0.619,34,0 288 | 1,119,86,39,220,45.6,0.808,29,1 289 | 4,96,56,17,49,20.8,0.340,26,0 290 | 5,108,72,43,75,36.1,0.263,33,0 291 | 0,78,88,29,40,36.9,0.434,21,0 292 | 0,107,62,30,74,36.6,0.757,25,1 293 | 2,128,78,37,182,43.3,1.224,31,1 294 | 1,128,48,45,194,40.5,0.613,24,1 295 | 0,161,50,0,0,21.9,0.254,65,0 296 | 6,151,62,31,120,35.5,0.692,28,0 297 | 2,146,70,38,360,28.0,0.337,29,1 298 | 0,126,84,29,215,30.7,0.520,24,0 299 | 14,100,78,25,184,36.6,0.412,46,1 300 | 8,112,72,0,0,23.6,0.840,58,0 301 | 0,167,0,0,0,32.3,0.839,30,1 302 | 2,144,58,33,135,31.6,0.422,25,1 303 | 5,77,82,41,42,35.8,0.156,35,0 304 | 5,115,98,0,0,52.9,0.209,28,1 305 | 3,150,76,0,0,21.0,0.207,37,0 306 | 2,120,76,37,105,39.7,0.215,29,0 307 | 10,161,68,23,132,25.5,0.326,47,1 308 | 0,137,68,14,148,24.8,0.143,21,0 309 | 0,128,68,19,180,30.5,1.391,25,1 310 | 2,124,68,28,205,32.9,0.875,30,1 311 | 6,80,66,30,0,26.2,0.313,41,0 312 | 0,106,70,37,148,39.4,0.605,22,0 313 | 2,155,74,17,96,26.6,0.433,27,1 314 | 3,113,50,10,85,29.5,0.626,25,0 315 | 7,109,80,31,0,35.9,1.127,43,1 316 | 2,112,68,22,94,34.1,0.315,26,0 317 | 3,99,80,11,64,19.3,0.284,30,0 318 | 3,182,74,0,0,30.5,0.345,29,1 319 | 3,115,66,39,140,38.1,0.150,28,0 320 | 6,194,78,0,0,23.5,0.129,59,1 321 | 4,129,60,12,231,27.5,0.527,31,0 322 | 3,112,74,30,0,31.6,0.197,25,1 323 | 0,124,70,20,0,27.4,0.254,36,1 324 | 13,152,90,33,29,26.8,0.731,43,1 325 | 2,112,75,32,0,35.7,0.148,21,0 326 | 1,157,72,21,168,25.6,0.123,24,0 327 | 1,122,64,32,156,35.1,0.692,30,1 328 | 10,179,70,0,0,35.1,0.200,37,0 329 | 2,102,86,36,120,45.5,0.127,23,1 330 | 6,105,70,32,68,30.8,0.122,37,0 331 | 8,118,72,19,0,23.1,1.476,46,0 332 | 2,87,58,16,52,32.7,0.166,25,0 333 | 1,180,0,0,0,43.3,0.282,41,1 334 | 12,106,80,0,0,23.6,0.137,44,0 335 | 1,95,60,18,58,23.9,0.260,22,0 336 | 0,165,76,43,255,47.9,0.259,26,0 337 | 0,117,0,0,0,33.8,0.932,44,0 338 | 5,115,76,0,0,31.2,0.343,44,1 339 | 9,152,78,34,171,34.2,0.893,33,1 340 | 7,178,84,0,0,39.9,0.331,41,1 341 | 1,130,70,13,105,25.9,0.472,22,0 342 | 1,95,74,21,73,25.9,0.673,36,0 343 | 1,0,68,35,0,32.0,0.389,22,0 344 | 5,122,86,0,0,34.7,0.290,33,0 345 | 8,95,72,0,0,36.8,0.485,57,0 346 | 8,126,88,36,108,38.5,0.349,49,0 347 | 1,139,46,19,83,28.7,0.654,22,0 348 | 3,116,0,0,0,23.5,0.187,23,0 349 | 3,99,62,19,74,21.8,0.279,26,0 350 | 5,0,80,32,0,41.0,0.346,37,1 351 | 4,92,80,0,0,42.2,0.237,29,0 352 | 4,137,84,0,0,31.2,0.252,30,0 353 | 3,61,82,28,0,34.4,0.243,46,0 354 | 1,90,62,12,43,27.2,0.580,24,0 355 | 3,90,78,0,0,42.7,0.559,21,0 356 | 9,165,88,0,0,30.4,0.302,49,1 357 | 1,125,50,40,167,33.3,0.962,28,1 358 | 13,129,0,30,0,39.9,0.569,44,1 359 | 12,88,74,40,54,35.3,0.378,48,0 360 | 1,196,76,36,249,36.5,0.875,29,1 361 | 5,189,64,33,325,31.2,0.583,29,1 362 | 5,158,70,0,0,29.8,0.207,63,0 363 | 5,103,108,37,0,39.2,0.305,65,0 364 | 4,146,78,0,0,38.5,0.520,67,1 365 | 4,147,74,25,293,34.9,0.385,30,0 366 | 5,99,54,28,83,34.0,0.499,30,0 367 | 6,124,72,0,0,27.6,0.368,29,1 368 | 0,101,64,17,0,21.0,0.252,21,0 369 | 3,81,86,16,66,27.5,0.306,22,0 370 | 1,133,102,28,140,32.8,0.234,45,1 371 | 3,173,82,48,465,38.4,2.137,25,1 372 | 0,118,64,23,89,0.0,1.731,21,0 373 | 0,84,64,22,66,35.8,0.545,21,0 374 | 2,105,58,40,94,34.9,0.225,25,0 375 | 2,122,52,43,158,36.2,0.816,28,0 376 | 12,140,82,43,325,39.2,0.528,58,1 377 | 0,98,82,15,84,25.2,0.299,22,0 378 | 1,87,60,37,75,37.2,0.509,22,0 379 | 4,156,75,0,0,48.3,0.238,32,1 380 | 0,93,100,39,72,43.4,1.021,35,0 381 | 1,107,72,30,82,30.8,0.821,24,0 382 | 0,105,68,22,0,20.0,0.236,22,0 383 | 1,109,60,8,182,25.4,0.947,21,0 384 | 1,90,62,18,59,25.1,1.268,25,0 385 | 1,125,70,24,110,24.3,0.221,25,0 386 | 1,119,54,13,50,22.3,0.205,24,0 387 | 5,116,74,29,0,32.3,0.660,35,1 388 | 8,105,100,36,0,43.3,0.239,45,1 389 | 5,144,82,26,285,32.0,0.452,58,1 390 | 3,100,68,23,81,31.6,0.949,28,0 391 | 1,100,66,29,196,32.0,0.444,42,0 392 | 5,166,76,0,0,45.7,0.340,27,1 393 | 1,131,64,14,415,23.7,0.389,21,0 394 | 4,116,72,12,87,22.1,0.463,37,0 395 | 4,158,78,0,0,32.9,0.803,31,1 396 | 2,127,58,24,275,27.7,1.600,25,0 397 | 3,96,56,34,115,24.7,0.944,39,0 398 | 0,131,66,40,0,34.3,0.196,22,1 399 | 3,82,70,0,0,21.1,0.389,25,0 400 | 3,193,70,31,0,34.9,0.241,25,1 401 | 4,95,64,0,0,32.0,0.161,31,1 402 | 6,137,61,0,0,24.2,0.151,55,0 403 | 5,136,84,41,88,35.0,0.286,35,1 404 | 9,72,78,25,0,31.6,0.280,38,0 405 | 5,168,64,0,0,32.9,0.135,41,1 406 | 2,123,48,32,165,42.1,0.520,26,0 407 | 4,115,72,0,0,28.9,0.376,46,1 408 | 0,101,62,0,0,21.9,0.336,25,0 409 | 8,197,74,0,0,25.9,1.191,39,1 410 | 1,172,68,49,579,42.4,0.702,28,1 411 | 6,102,90,39,0,35.7,0.674,28,0 412 | 1,112,72,30,176,34.4,0.528,25,0 413 | 1,143,84,23,310,42.4,1.076,22,0 414 | 1,143,74,22,61,26.2,0.256,21,0 415 | 0,138,60,35,167,34.6,0.534,21,1 416 | 3,173,84,33,474,35.7,0.258,22,1 417 | 1,97,68,21,0,27.2,1.095,22,0 418 | 4,144,82,32,0,38.5,0.554,37,1 419 | 1,83,68,0,0,18.2,0.624,27,0 420 | 3,129,64,29,115,26.4,0.219,28,1 421 | 1,119,88,41,170,45.3,0.507,26,0 422 | 2,94,68,18,76,26.0,0.561,21,0 423 | 0,102,64,46,78,40.6,0.496,21,0 424 | 2,115,64,22,0,30.8,0.421,21,0 425 | 8,151,78,32,210,42.9,0.516,36,1 426 | 4,184,78,39,277,37.0,0.264,31,1 427 | 0,94,0,0,0,0.0,0.256,25,0 428 | 1,181,64,30,180,34.1,0.328,38,1 429 | 0,135,94,46,145,40.6,0.284,26,0 430 | 1,95,82,25,180,35.0,0.233,43,1 431 | 2,99,0,0,0,22.2,0.108,23,0 432 | 3,89,74,16,85,30.4,0.551,38,0 433 | 1,80,74,11,60,30.0,0.527,22,0 434 | 2,139,75,0,0,25.6,0.167,29,0 435 | 1,90,68,8,0,24.5,1.138,36,0 436 | 0,141,0,0,0,42.4,0.205,29,1 437 | 12,140,85,33,0,37.4,0.244,41,0 438 | 5,147,75,0,0,29.9,0.434,28,0 439 | 1,97,70,15,0,18.2,0.147,21,0 440 | 6,107,88,0,0,36.8,0.727,31,0 441 | 0,189,104,25,0,34.3,0.435,41,1 442 | 2,83,66,23,50,32.2,0.497,22,0 443 | 4,117,64,27,120,33.2,0.230,24,0 444 | 8,108,70,0,0,30.5,0.955,33,1 445 | 4,117,62,12,0,29.7,0.380,30,1 446 | 0,180,78,63,14,59.4,2.420,25,1 447 | 1,100,72,12,70,25.3,0.658,28,0 448 | 0,95,80,45,92,36.5,0.330,26,0 449 | 0,104,64,37,64,33.6,0.510,22,1 450 | 0,120,74,18,63,30.5,0.285,26,0 451 | 1,82,64,13,95,21.2,0.415,23,0 452 | 2,134,70,0,0,28.9,0.542,23,1 453 | 0,91,68,32,210,39.9,0.381,25,0 454 | 2,119,0,0,0,19.6,0.832,72,0 455 | 2,100,54,28,105,37.8,0.498,24,0 456 | 14,175,62,30,0,33.6,0.212,38,1 457 | 1,135,54,0,0,26.7,0.687,62,0 458 | 5,86,68,28,71,30.2,0.364,24,0 459 | 10,148,84,48,237,37.6,1.001,51,1 460 | 9,134,74,33,60,25.9,0.460,81,0 461 | 9,120,72,22,56,20.8,0.733,48,0 462 | 1,71,62,0,0,21.8,0.416,26,0 463 | 8,74,70,40,49,35.3,0.705,39,0 464 | 5,88,78,30,0,27.6,0.258,37,0 465 | 10,115,98,0,0,24.0,1.022,34,0 466 | 0,124,56,13,105,21.8,0.452,21,0 467 | 0,74,52,10,36,27.8,0.269,22,0 468 | 0,97,64,36,100,36.8,0.600,25,0 469 | 8,120,0,0,0,30.0,0.183,38,1 470 | 6,154,78,41,140,46.1,0.571,27,0 471 | 1,144,82,40,0,41.3,0.607,28,0 472 | 0,137,70,38,0,33.2,0.170,22,0 473 | 0,119,66,27,0,38.8,0.259,22,0 474 | 7,136,90,0,0,29.9,0.210,50,0 475 | 4,114,64,0,0,28.9,0.126,24,0 476 | 0,137,84,27,0,27.3,0.231,59,0 477 | 2,105,80,45,191,33.7,0.711,29,1 478 | 7,114,76,17,110,23.8,0.466,31,0 479 | 8,126,74,38,75,25.9,0.162,39,0 480 | 4,132,86,31,0,28.0,0.419,63,0 481 | 3,158,70,30,328,35.5,0.344,35,1 482 | 0,123,88,37,0,35.2,0.197,29,0 483 | 4,85,58,22,49,27.8,0.306,28,0 484 | 0,84,82,31,125,38.2,0.233,23,0 485 | 0,145,0,0,0,44.2,0.630,31,1 486 | 0,135,68,42,250,42.3,0.365,24,1 487 | 1,139,62,41,480,40.7,0.536,21,0 488 | 0,173,78,32,265,46.5,1.159,58,0 489 | 4,99,72,17,0,25.6,0.294,28,0 490 | 8,194,80,0,0,26.1,0.551,67,0 491 | 2,83,65,28,66,36.8,0.629,24,0 492 | 2,89,90,30,0,33.5,0.292,42,0 493 | 4,99,68,38,0,32.8,0.145,33,0 494 | 4,125,70,18,122,28.9,1.144,45,1 495 | 3,80,0,0,0,0.0,0.174,22,0 496 | 6,166,74,0,0,26.6,0.304,66,0 497 | 5,110,68,0,0,26.0,0.292,30,0 498 | 2,81,72,15,76,30.1,0.547,25,0 499 | 7,195,70,33,145,25.1,0.163,55,1 500 | 6,154,74,32,193,29.3,0.839,39,0 501 | 2,117,90,19,71,25.2,0.313,21,0 502 | 3,84,72,32,0,37.2,0.267,28,0 503 | 6,0,68,41,0,39.0,0.727,41,1 504 | 7,94,64,25,79,33.3,0.738,41,0 505 | 3,96,78,39,0,37.3,0.238,40,0 506 | 10,75,82,0,0,33.3,0.263,38,0 507 | 0,180,90,26,90,36.5,0.314,35,1 508 | 1,130,60,23,170,28.6,0.692,21,0 509 | 2,84,50,23,76,30.4,0.968,21,0 510 | 8,120,78,0,0,25.0,0.409,64,0 511 | 12,84,72,31,0,29.7,0.297,46,1 512 | 0,139,62,17,210,22.1,0.207,21,0 513 | 9,91,68,0,0,24.2,0.200,58,0 514 | 2,91,62,0,0,27.3,0.525,22,0 515 | 3,99,54,19,86,25.6,0.154,24,0 516 | 3,163,70,18,105,31.6,0.268,28,1 517 | 9,145,88,34,165,30.3,0.771,53,1 518 | 7,125,86,0,0,37.6,0.304,51,0 519 | 13,76,60,0,0,32.8,0.180,41,0 520 | 6,129,90,7,326,19.6,0.582,60,0 521 | 2,68,70,32,66,25.0,0.187,25,0 522 | 3,124,80,33,130,33.2,0.305,26,0 523 | 6,114,0,0,0,0.0,0.189,26,0 524 | 9,130,70,0,0,34.2,0.652,45,1 525 | 3,125,58,0,0,31.6,0.151,24,0 526 | 3,87,60,18,0,21.8,0.444,21,0 527 | 1,97,64,19,82,18.2,0.299,21,0 528 | 3,116,74,15,105,26.3,0.107,24,0 529 | 0,117,66,31,188,30.8,0.493,22,0 530 | 0,111,65,0,0,24.6,0.660,31,0 531 | 2,122,60,18,106,29.8,0.717,22,0 532 | 0,107,76,0,0,45.3,0.686,24,0 533 | 1,86,66,52,65,41.3,0.917,29,0 534 | 6,91,0,0,0,29.8,0.501,31,0 535 | 1,77,56,30,56,33.3,1.251,24,0 536 | 4,132,0,0,0,32.9,0.302,23,1 537 | 0,105,90,0,0,29.6,0.197,46,0 538 | 0,57,60,0,0,21.7,0.735,67,0 539 | 0,127,80,37,210,36.3,0.804,23,0 540 | 3,129,92,49,155,36.4,0.968,32,1 541 | 8,100,74,40,215,39.4,0.661,43,1 542 | 3,128,72,25,190,32.4,0.549,27,1 543 | 10,90,85,32,0,34.9,0.825,56,1 544 | 4,84,90,23,56,39.5,0.159,25,0 545 | 1,88,78,29,76,32.0,0.365,29,0 546 | 8,186,90,35,225,34.5,0.423,37,1 547 | 5,187,76,27,207,43.6,1.034,53,1 548 | 4,131,68,21,166,33.1,0.160,28,0 549 | 1,164,82,43,67,32.8,0.341,50,0 550 | 4,189,110,31,0,28.5,0.680,37,0 551 | 1,116,70,28,0,27.4,0.204,21,0 552 | 3,84,68,30,106,31.9,0.591,25,0 553 | 6,114,88,0,0,27.8,0.247,66,0 554 | 1,88,62,24,44,29.9,0.422,23,0 555 | 1,84,64,23,115,36.9,0.471,28,0 556 | 7,124,70,33,215,25.5,0.161,37,0 557 | 1,97,70,40,0,38.1,0.218,30,0 558 | 8,110,76,0,0,27.8,0.237,58,0 559 | 11,103,68,40,0,46.2,0.126,42,0 560 | 11,85,74,0,0,30.1,0.300,35,0 561 | 6,125,76,0,0,33.8,0.121,54,1 562 | 0,198,66,32,274,41.3,0.502,28,1 563 | 1,87,68,34,77,37.6,0.401,24,0 564 | 6,99,60,19,54,26.9,0.497,32,0 565 | 0,91,80,0,0,32.4,0.601,27,0 566 | 2,95,54,14,88,26.1,0.748,22,0 567 | 1,99,72,30,18,38.6,0.412,21,0 568 | 6,92,62,32,126,32.0,0.085,46,0 569 | 4,154,72,29,126,31.3,0.338,37,0 570 | 0,121,66,30,165,34.3,0.203,33,1 571 | 3,78,70,0,0,32.5,0.270,39,0 572 | 2,130,96,0,0,22.6,0.268,21,0 573 | 3,111,58,31,44,29.5,0.430,22,0 574 | 2,98,60,17,120,34.7,0.198,22,0 575 | 1,143,86,30,330,30.1,0.892,23,0 576 | 1,119,44,47,63,35.5,0.280,25,0 577 | 6,108,44,20,130,24.0,0.813,35,0 578 | 2,118,80,0,0,42.9,0.693,21,1 579 | 10,133,68,0,0,27.0,0.245,36,0 580 | 2,197,70,99,0,34.7,0.575,62,1 581 | 0,151,90,46,0,42.1,0.371,21,1 582 | 6,109,60,27,0,25.0,0.206,27,0 583 | 12,121,78,17,0,26.5,0.259,62,0 584 | 8,100,76,0,0,38.7,0.190,42,0 585 | 8,124,76,24,600,28.7,0.687,52,1 586 | 1,93,56,11,0,22.5,0.417,22,0 587 | 8,143,66,0,0,34.9,0.129,41,1 588 | 6,103,66,0,0,24.3,0.249,29,0 589 | 3,176,86,27,156,33.3,1.154,52,1 590 | 0,73,0,0,0,21.1,0.342,25,0 591 | 11,111,84,40,0,46.8,0.925,45,1 592 | 2,112,78,50,140,39.4,0.175,24,0 593 | 3,132,80,0,0,34.4,0.402,44,1 594 | 2,82,52,22,115,28.5,1.699,25,0 595 | 6,123,72,45,230,33.6,0.733,34,0 596 | 0,188,82,14,185,32.0,0.682,22,1 597 | 0,67,76,0,0,45.3,0.194,46,0 598 | 1,89,24,19,25,27.8,0.559,21,0 599 | 1,173,74,0,0,36.8,0.088,38,1 600 | 1,109,38,18,120,23.1,0.407,26,0 601 | 1,108,88,19,0,27.1,0.400,24,0 602 | 6,96,0,0,0,23.7,0.190,28,0 603 | 1,124,74,36,0,27.8,0.100,30,0 604 | 7,150,78,29,126,35.2,0.692,54,1 605 | 4,183,0,0,0,28.4,0.212,36,1 606 | 1,124,60,32,0,35.8,0.514,21,0 607 | 1,181,78,42,293,40.0,1.258,22,1 608 | 1,92,62,25,41,19.5,0.482,25,0 609 | 0,152,82,39,272,41.5,0.270,27,0 610 | 1,111,62,13,182,24.0,0.138,23,0 611 | 3,106,54,21,158,30.9,0.292,24,0 612 | 3,174,58,22,194,32.9,0.593,36,1 613 | 7,168,88,42,321,38.2,0.787,40,1 614 | 6,105,80,28,0,32.5,0.878,26,0 615 | 11,138,74,26,144,36.1,0.557,50,1 616 | 3,106,72,0,0,25.8,0.207,27,0 617 | 6,117,96,0,0,28.7,0.157,30,0 618 | 2,68,62,13,15,20.1,0.257,23,0 619 | 9,112,82,24,0,28.2,1.282,50,1 620 | 0,119,0,0,0,32.4,0.141,24,1 621 | 2,112,86,42,160,38.4,0.246,28,0 622 | 2,92,76,20,0,24.2,1.698,28,0 623 | 6,183,94,0,0,40.8,1.461,45,0 624 | 0,94,70,27,115,43.5,0.347,21,0 625 | 2,108,64,0,0,30.8,0.158,21,0 626 | 4,90,88,47,54,37.7,0.362,29,0 627 | 0,125,68,0,0,24.7,0.206,21,0 628 | 0,132,78,0,0,32.4,0.393,21,0 629 | 5,128,80,0,0,34.6,0.144,45,0 630 | 4,94,65,22,0,24.7,0.148,21,0 631 | 7,114,64,0,0,27.4,0.732,34,1 632 | 0,102,78,40,90,34.5,0.238,24,0 633 | 2,111,60,0,0,26.2,0.343,23,0 634 | 1,128,82,17,183,27.5,0.115,22,0 635 | 10,92,62,0,0,25.9,0.167,31,0 636 | 13,104,72,0,0,31.2,0.465,38,1 637 | 5,104,74,0,0,28.8,0.153,48,0 638 | 2,94,76,18,66,31.6,0.649,23,0 639 | 7,97,76,32,91,40.9,0.871,32,1 640 | 1,100,74,12,46,19.5,0.149,28,0 641 | 0,102,86,17,105,29.3,0.695,27,0 642 | 4,128,70,0,0,34.3,0.303,24,0 643 | 6,147,80,0,0,29.5,0.178,50,1 644 | 4,90,0,0,0,28.0,0.610,31,0 645 | 3,103,72,30,152,27.6,0.730,27,0 646 | 2,157,74,35,440,39.4,0.134,30,0 647 | 1,167,74,17,144,23.4,0.447,33,1 648 | 0,179,50,36,159,37.8,0.455,22,1 649 | 11,136,84,35,130,28.3,0.260,42,1 650 | 0,107,60,25,0,26.4,0.133,23,0 651 | 1,91,54,25,100,25.2,0.234,23,0 652 | 1,117,60,23,106,33.8,0.466,27,0 653 | 5,123,74,40,77,34.1,0.269,28,0 654 | 2,120,54,0,0,26.8,0.455,27,0 655 | 1,106,70,28,135,34.2,0.142,22,0 656 | 2,155,52,27,540,38.7,0.240,25,1 657 | 2,101,58,35,90,21.8,0.155,22,0 658 | 1,120,80,48,200,38.9,1.162,41,0 659 | 11,127,106,0,0,39.0,0.190,51,0 660 | 3,80,82,31,70,34.2,1.292,27,1 661 | 10,162,84,0,0,27.7,0.182,54,0 662 | 1,199,76,43,0,42.9,1.394,22,1 663 | 8,167,106,46,231,37.6,0.165,43,1 664 | 9,145,80,46,130,37.9,0.637,40,1 665 | 6,115,60,39,0,33.7,0.245,40,1 666 | 1,112,80,45,132,34.8,0.217,24,0 667 | 4,145,82,18,0,32.5,0.235,70,1 668 | 10,111,70,27,0,27.5,0.141,40,1 669 | 6,98,58,33,190,34.0,0.430,43,0 670 | 9,154,78,30,100,30.9,0.164,45,0 671 | 6,165,68,26,168,33.6,0.631,49,0 672 | 1,99,58,10,0,25.4,0.551,21,0 673 | 10,68,106,23,49,35.5,0.285,47,0 674 | 3,123,100,35,240,57.3,0.880,22,0 675 | 8,91,82,0,0,35.6,0.587,68,0 676 | 6,195,70,0,0,30.9,0.328,31,1 677 | 9,156,86,0,0,24.8,0.230,53,1 678 | 0,93,60,0,0,35.3,0.263,25,0 679 | 3,121,52,0,0,36.0,0.127,25,1 680 | 2,101,58,17,265,24.2,0.614,23,0 681 | 2,56,56,28,45,24.2,0.332,22,0 682 | 0,162,76,36,0,49.6,0.364,26,1 683 | 0,95,64,39,105,44.6,0.366,22,0 684 | 4,125,80,0,0,32.3,0.536,27,1 685 | 5,136,82,0,0,0.0,0.640,69,0 686 | 2,129,74,26,205,33.2,0.591,25,0 687 | 3,130,64,0,0,23.1,0.314,22,0 688 | 1,107,50,19,0,28.3,0.181,29,0 689 | 1,140,74,26,180,24.1,0.828,23,0 690 | 1,144,82,46,180,46.1,0.335,46,1 691 | 8,107,80,0,0,24.6,0.856,34,0 692 | 13,158,114,0,0,42.3,0.257,44,1 693 | 2,121,70,32,95,39.1,0.886,23,0 694 | 7,129,68,49,125,38.5,0.439,43,1 695 | 2,90,60,0,0,23.5,0.191,25,0 696 | 7,142,90,24,480,30.4,0.128,43,1 697 | 3,169,74,19,125,29.9,0.268,31,1 698 | 0,99,0,0,0,25.0,0.253,22,0 699 | 4,127,88,11,155,34.5,0.598,28,0 700 | 4,118,70,0,0,44.5,0.904,26,0 701 | 2,122,76,27,200,35.9,0.483,26,0 702 | 6,125,78,31,0,27.6,0.565,49,1 703 | 1,168,88,29,0,35.0,0.905,52,1 704 | 2,129,0,0,0,38.5,0.304,41,0 705 | 4,110,76,20,100,28.4,0.118,27,0 706 | 6,80,80,36,0,39.8,0.177,28,0 707 | 10,115,0,0,0,0.0,0.261,30,1 708 | 2,127,46,21,335,34.4,0.176,22,0 709 | 9,164,78,0,0,32.8,0.148,45,1 710 | 2,93,64,32,160,38.0,0.674,23,1 711 | 3,158,64,13,387,31.2,0.295,24,0 712 | 5,126,78,27,22,29.6,0.439,40,0 713 | 10,129,62,36,0,41.2,0.441,38,1 714 | 0,134,58,20,291,26.4,0.352,21,0 715 | 3,102,74,0,0,29.5,0.121,32,0 716 | 7,187,50,33,392,33.9,0.826,34,1 717 | 3,173,78,39,185,33.8,0.970,31,1 718 | 10,94,72,18,0,23.1,0.595,56,0 719 | 1,108,60,46,178,35.5,0.415,24,0 720 | 5,97,76,27,0,35.6,0.378,52,1 721 | 4,83,86,19,0,29.3,0.317,34,0 722 | 1,114,66,36,200,38.1,0.289,21,0 723 | 1,149,68,29,127,29.3,0.349,42,1 724 | 5,117,86,30,105,39.1,0.251,42,0 725 | 1,111,94,0,0,32.8,0.265,45,0 726 | 4,112,78,40,0,39.4,0.236,38,0 727 | 1,116,78,29,180,36.1,0.496,25,0 728 | 0,141,84,26,0,32.4,0.433,22,0 729 | 2,175,88,0,0,22.9,0.326,22,0 730 | 2,92,52,0,0,30.1,0.141,22,0 731 | 3,130,78,23,79,28.4,0.323,34,1 732 | 8,120,86,0,0,28.4,0.259,22,1 733 | 2,174,88,37,120,44.5,0.646,24,1 734 | 2,106,56,27,165,29.0,0.426,22,0 735 | 2,105,75,0,0,23.3,0.560,53,0 736 | 4,95,60,32,0,35.4,0.284,28,0 737 | 0,126,86,27,120,27.4,0.515,21,0 738 | 8,65,72,23,0,32.0,0.600,42,0 739 | 2,99,60,17,160,36.6,0.453,21,0 740 | 1,102,74,0,0,39.5,0.293,42,1 741 | 11,120,80,37,150,42.3,0.785,48,1 742 | 3,102,44,20,94,30.8,0.400,26,0 743 | 1,109,58,18,116,28.5,0.219,22,0 744 | 9,140,94,0,0,32.7,0.734,45,1 745 | 13,153,88,37,140,40.6,1.174,39,0 746 | 12,100,84,33,105,30.0,0.488,46,0 747 | 1,147,94,41,0,49.3,0.358,27,1 748 | 1,81,74,41,57,46.3,1.096,32,0 749 | 3,187,70,22,200,36.4,0.408,36,1 750 | 6,162,62,0,0,24.3,0.178,50,1 751 | 4,136,70,0,0,31.2,1.182,22,1 752 | 1,121,78,39,74,39.0,0.261,28,0 753 | 3,108,62,24,0,26.0,0.223,25,0 754 | 0,181,88,44,510,43.3,0.222,26,1 755 | 8,154,78,32,0,32.4,0.443,45,1 756 | 1,128,88,39,110,36.5,1.057,37,1 757 | 7,137,90,41,0,32.0,0.391,39,0 758 | 0,123,72,0,0,36.3,0.258,52,1 759 | 1,106,76,0,0,37.5,0.197,26,0 760 | 6,190,92,0,0,35.5,0.278,66,1 761 | 2,88,58,26,16,28.4,0.766,22,0 762 | 9,170,74,31,0,44.0,0.403,43,1 763 | 9,89,62,0,0,22.5,0.142,33,0 764 | 10,101,76,48,180,32.9,0.171,63,0 765 | 2,122,70,27,0,36.8,0.340,27,0 766 | 5,121,72,23,112,26.2,0.245,30,0 767 | 1,126,60,0,0,30.1,0.349,47,1 768 | 1,93,70,31,0,30.4,0.315,23,0 -------------------------------------------------------------------------------- /chapter03/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter04/OReilly_C4_hub_intro_embedding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "OReilly_C4_hub_intro_embedding.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyMdcRVrSLn0SJQO9nI28Zdm", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "language_info": { 16 | "name": "python" 17 | } 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "metadata": { 33 | "colab": { 34 | "base_uri": "https://localhost:8080/" 35 | }, 36 | "id": "r5mtX8jhMpeK", 37 | "outputId": "39147e4a-577e-4527-894f-bc2960da360c" 38 | }, 39 | "source": [ 40 | " !pip install --upgrade tensorflow_hub" 41 | ], 42 | "execution_count": 1, 43 | "outputs": [ 44 | { 45 | "output_type": "stream", 46 | "text": [ 47 | "Requirement already satisfied: tensorflow_hub in /usr/local/lib/python3.7/dist-packages (0.12.0)\n", 48 | "Requirement already satisfied: protobuf>=3.8.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow_hub) (3.17.3)\n", 49 | "Requirement already satisfied: numpy>=1.12.0 in /usr/local/lib/python3.7/dist-packages (from tensorflow_hub) (1.19.5)\n", 50 | "Requirement already satisfied: six>=1.9 in /usr/local/lib/python3.7/dist-packages (from protobuf>=3.8.0->tensorflow_hub) (1.15.0)\n" 51 | ], 52 | "name": "stdout" 53 | } 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "metadata": { 59 | "colab": { 60 | "base_uri": "https://localhost:8080/" 61 | }, 62 | "id": "HR5GBK44MsrF", 63 | "outputId": "19753416-ff78-42b0-f301-7a7a92d8a12c" 64 | }, 65 | "source": [ 66 | " import tensorflow_hub as hub\n", 67 | " print(hub.__version__)\n", 68 | " model = hub.KerasLayer(\"https://tfhub.dev/google/nnlm-en-dim128/2\")\n", 69 | " embeddings = model([\"The rain in Spain.\", \"falls\",\n", 70 | " \"mainly\", \"In the plain!\"])\n", 71 | "\n", 72 | " print(embeddings.shape) #(4,128)" 73 | ], 74 | "execution_count": 2, 75 | "outputs": [ 76 | { 77 | "output_type": "stream", 78 | "text": [ 79 | "0.12.0\n", 80 | "(4, 128)\n" 81 | ], 82 | "name": "stdout" 83 | } 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "metadata": { 89 | "colab": { 90 | "base_uri": "https://localhost:8080/" 91 | }, 92 | "id": "FBNiTm1CM0O2", 93 | "outputId": "69c7ef9e-d9c3-4ba5-cfe6-906274ca3a7a" 94 | }, 95 | "source": [ 96 | "len(embeddings[0])" 97 | ], 98 | "execution_count": 3, 99 | "outputs": [ 100 | { 101 | "output_type": "execute_result", 102 | "data": { 103 | "text/plain": [ 104 | "128" 105 | ] 106 | }, 107 | "metadata": { 108 | "tags": [] 109 | }, 110 | "execution_count": 3 111 | } 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "metadata": { 117 | "colab": { 118 | "base_uri": "https://localhost:8080/" 119 | }, 120 | "id": "3mEpOdkWNJoR", 121 | "outputId": "063f4201-fefd-476b-d36e-ae8e18c14bff" 122 | }, 123 | "source": [ 124 | "print(embeddings[0])" 125 | ], 126 | "execution_count": 4, 127 | "outputs": [ 128 | { 129 | "output_type": "stream", 130 | "text": [ 131 | "tf.Tensor(\n", 132 | "[ 1.59735829e-01 -1.29867658e-01 -1.35819957e-01 -9.24930871e-02\n", 133 | " 2.10374016e-02 -7.29679167e-02 -1.11245513e-01 -9.90176424e-02\n", 134 | " 1.71865672e-02 -7.84225911e-02 3.15940194e-02 6.22844100e-02\n", 135 | " -8.64286795e-02 -7.39999115e-02 4.68084142e-02 2.64361743e-02\n", 136 | " -1.51468843e-01 8.37246105e-02 -4.84693572e-02 3.62105906e-01\n", 137 | " -4.43154667e-03 8.15866888e-02 -4.09569405e-02 -1.05201520e-01\n", 138 | " -1.68916266e-02 2.99027748e-02 8.40939730e-02 4.29377109e-02\n", 139 | " -3.50504890e-02 -9.36504006e-02 -3.67183425e-02 3.71749699e-02\n", 140 | " 2.95477919e-02 1.72754392e-01 1.08686648e-02 -1.57570407e-01\n", 141 | " -3.82928923e-02 -4.58077006e-02 -9.41693187e-02 -2.65682978e-03\n", 142 | " -5.25422022e-03 -3.45592424e-02 -1.69853773e-02 1.99845843e-02\n", 143 | " 2.64117979e-02 1.15411691e-02 3.72448415e-02 6.33172691e-02\n", 144 | " 5.32621630e-02 1.91026106e-02 6.38045296e-02 5.82209881e-03\n", 145 | " -1.88273005e-03 1.63987540e-02 1.42705105e-02 -4.10080254e-02\n", 146 | " 6.77466765e-02 -5.85823879e-02 3.11339628e-02 -4.20597643e-02\n", 147 | " -6.45544473e-03 3.64223719e-02 6.28872663e-02 6.90744817e-03\n", 148 | " 9.09396559e-02 -9.11951065e-06 -3.28868106e-02 -1.33488700e-02\n", 149 | " 5.82060106e-02 -1.18812937e-02 -4.62300964e-02 3.13238539e-02\n", 150 | " -5.73746674e-02 1.17937967e-01 -2.24236436e-02 -7.00195804e-02\n", 151 | " -1.50378808e-01 -3.55751067e-03 -1.21659540e-01 1.49155945e-01\n", 152 | " 6.68335930e-02 -8.45783874e-02 -4.99900915e-02 -1.23323306e-01\n", 153 | " -9.72058177e-02 2.34775878e-02 -8.11939389e-02 -1.21891469e-01\n", 154 | " -1.04129739e-01 -1.12477146e-01 5.28658256e-02 2.75909528e-03\n", 155 | " -1.19978651e-01 -1.63873881e-01 -1.46854967e-02 4.54718731e-02\n", 156 | " 1.23541318e-01 -7.64916986e-02 2.16103956e-01 -1.42051399e-01\n", 157 | " 3.95136364e-02 1.21098757e-02 -1.42837271e-01 -7.10892025e-03\n", 158 | " 6.83816895e-03 -2.80113220e-02 -3.36971357e-02 -1.60303190e-02\n", 159 | " 7.51627758e-02 -2.00884849e-01 -5.17385006e-02 7.98245817e-02\n", 160 | " 1.49128556e-01 2.33411700e-01 4.77640852e-02 -2.76381411e-02\n", 161 | " 1.69027522e-02 6.41705990e-02 -1.05072603e-01 4.28162888e-03\n", 162 | " 5.64674214e-02 8.22738558e-03 -6.20092526e-02 7.22365454e-03\n", 163 | " 9.19964239e-02 3.05843651e-02 1.27910644e-01 3.83963063e-02], shape=(128,), dtype=float32)\n" 164 | ], 165 | "name": "stdout" 166 | } 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "metadata": { 172 | "colab": { 173 | "base_uri": "https://localhost:8080/" 174 | }, 175 | "id": "HgDPMzOtNLTP", 176 | "outputId": "77b6ee04-51f0-465d-f843-616fb955e4e4" 177 | }, 178 | "source": [ 179 | "import tensorflow as tf\n", 180 | "print(tf.__version__)" 181 | ], 182 | "execution_count": 5, 183 | "outputs": [ 184 | { 185 | "output_type": "stream", 186 | "text": [ 187 | "2.5.0\n" 188 | ], 189 | "name": "stdout" 190 | } 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "metadata": { 196 | "id": "NRpamG7_OAsV" 197 | }, 198 | "source": [ 199 | "" 200 | ], 201 | "execution_count": null, 202 | "outputs": [] 203 | } 204 | ] 205 | } -------------------------------------------------------------------------------- /chapter04/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter05/Oreilly_C5_flow_from_dataframe.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "id": "eq7sBQbN6Oot" 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "!wget https://data.mendeley.com/public-files/datasets/jxmfrvhpyz/files/283004ff-e529-4c3c-a1ee-4fb90024dc94/file_downloaded --output-document flower_photos.zip" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": { 18 | "id": "oXqg2EiP6hXQ" 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "!unzip -q flower_photos.zip" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": { 29 | "id": "kk6YNiRX6YRo" 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "!ls -lrt\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": { 40 | "id": "cNfClmj_PXvJ" 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "\n", 45 | "!ls -alt flower_photos" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "id": "pVw9QC9fW50z" 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "!ls -lrt" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": { 63 | "id": "v4OZ_zt07MsQ" 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "!ls" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": { 74 | "id": "kGPECDScIlTT" 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "import tensorflow as tf\n", 79 | "import tensorflow_hub as hub\n", 80 | "import pandas as pd\n", 81 | "import numpy as np\n", 82 | "import matplotlib.pyplot as plt" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": { 89 | "id": "B8UlYVp_I01B" 90 | }, 91 | "outputs": [], 92 | "source": [ 93 | "print(tf.__version__)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": { 100 | "id": "-oKF6-uGJODs" 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "traindf=pd.read_csv('flower_photos/all_labels.csv',dtype=str)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": { 111 | "id": "sCm7J7-6JtDb" 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "traindf" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "id": "QmSeCn7bJuAy" 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "# Create data generator for training and validation\n", 127 | "data_root = 'flower_photos/flowers'\n", 128 | "IMAGE_SIZE = (224, 224)\n", 129 | "TRAINING_DATA_DIR = str(data_root)\n", 130 | "BATCH_SIZE = 32" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": { 137 | "id": "qbJXgWlALMiR" 138 | }, 139 | "outputs": [], 140 | "source": [ 141 | "datagen_kwargs = dict(rescale=1./255, validation_split=.20)\n", 142 | "dataflow_kwargs = dict(target_size=IMAGE_SIZE, batch_size=BATCH_SIZE,\n", 143 | " interpolation=\"bilinear\")" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": { 150 | "id": "OSdPb0N3LSer" 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "valid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(**datagen_kwargs)\n", 155 | "\n", 156 | "valid_generator=valid_datagen.flow_from_dataframe(\n", 157 | "dataframe=traindf,\n", 158 | "directory=data_root,\n", 159 | "x_col=\"file_name\",\n", 160 | "y_col=\"label\",\n", 161 | "subset=\"validation\",\n", 162 | "seed=10,\n", 163 | "shuffle=True,\n", 164 | "class_mode=\"categorical\",\n", 165 | "**dataflow_kwargs)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": { 172 | "id": "ZX0JOE4RL2ss" 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(**datagen_kwargs)\n", 177 | "train_generator=train_datagen.flow_from_dataframe(\n", 178 | "dataframe=traindf,\n", 179 | "directory=data_root,\n", 180 | "x_col=\"file_name\",\n", 181 | "y_col=\"label\",\n", 182 | "subset=\"training\",\n", 183 | "seed=10,\n", 184 | "shuffle=True,\n", 185 | "class_mode=\"categorical\",\n", 186 | "**dataflow_kwargs)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": { 193 | "id": "iwyxWMf3MSJz" 194 | }, 195 | "outputs": [], 196 | "source": [ 197 | "image_batch_train, label_batch_train = next(iter(train_generator))\n", 198 | "print(\"Image batch shape: \", image_batch_train.shape)\n", 199 | "print(\"Label batch shape: \", label_batch_train.shape)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": { 206 | "id": "etxrSUMfYkcr" 207 | }, 208 | "outputs": [], 209 | "source": [ 210 | "labels_idx = (train_generator.class_indices)\n", 211 | "idx_labels = dict((v,k) for k,v in labels_idx.items())" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": { 218 | "id": "Yf5AMGavYnr_" 219 | }, 220 | "outputs": [], 221 | "source": [ 222 | "idx_labels" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": { 229 | "id": "SlDaXzP6ObqA" 230 | }, 231 | "outputs": [], 232 | "source": [ 233 | "dataset_labels = sorted(train_generator.class_indices.items(), key=lambda pair:pair[1])\n", 234 | "dataset_labels = np.array([key.title() for key, value in dataset_labels])\n", 235 | "print(dataset_labels)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": { 241 | "id": "z78n_YF_QPFJ" 242 | }, 243 | "source": [ 244 | "Find label index and order of classes." 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": { 251 | "id": "424TXHuIOfa4" 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "labels_idx = (train_generator.class_indices)\n", 256 | "idx_labels = dict((v,k) for k,v in labels_idx.items())" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": { 263 | "id": "0XHZ6vlsOuDe" 264 | }, 265 | "outputs": [], 266 | "source": [ 267 | "idx_labels" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": { 274 | "id": "dzF4n7MUQ7nO" 275 | }, 276 | "outputs": [], 277 | "source": [ 278 | "NUM_CLASSES = len(idx_labels)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": { 285 | "id": "_QFefeTNQU7Y" 286 | }, 287 | "outputs": [], 288 | "source": [ 289 | "\n", 290 | "image_batch, label_batch = next(iter(train_generator))\n", 291 | "\n", 292 | "fig, axes = plt.subplots(8, 4, figsize=(20, 40))\n", 293 | "axes = axes.flatten()\n", 294 | "for img, lbl, ax in zip(image_batch, label_batch, axes):\n", 295 | " ax.imshow(img)\n", 296 | " label_ = np.argmax(lbl)\n", 297 | " label = idx_labels[label_]\n", 298 | " ax.set_title(label)\n", 299 | " ax.axis('off')\n", 300 | "#plt.tight_layout(h_pad=0.01, w_pad=1)\n", 301 | "plt.show()\n", 302 | "\n" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": { 309 | "id": "s4UvabXtQc9G" 310 | }, 311 | "outputs": [], 312 | "source": [ 313 | "'''\n", 314 | "# build a custom model\n", 315 | "mdl = tf.keras.Sequential([\n", 316 | " #tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),\n", 317 | " tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', \n", 318 | " kernel_initializer='glorot_uniform', padding='same', input_shape =IMAGE_SIZE + (3,)),\n", 319 | " tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", 320 | " tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu', \n", 321 | " kernel_initializer='glorot_uniform', padding='same'),\n", 322 | " tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", 323 | " tf.keras.layers.Flatten(),\n", 324 | " tf.keras.layers.Dense(256, activation='relu', kernel_initializer='glorot_uniform'),\n", 325 | " tf.keras.layers.Dense(NUM_CLASSES, activation='softmax', name = 'custom_class')\n", 326 | "])\n", 327 | "mdl.build([None, 224, 224, 3])\n", 328 | "'''\n" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": { 335 | "id": "y4GHpu7Z3GxM" 336 | }, 337 | "outputs": [], 338 | "source": [ 339 | "mdl = tf.keras.Sequential([\n", 340 | " tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),\n", 341 | " hub.KerasLayer(\"https://tfhub.dev/tensorflow/resnet_50/feature_vector/1\", trainable=False), \n", 342 | " tf.keras.layers.Dense(NUM_CLASSES, activation='softmax', name = 'custom_class')\n", 343 | "])\n", 344 | "mdl.build([None, 224, 224, 3])" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": { 351 | "id": "5qc4KMO0RAUL" 352 | }, 353 | "outputs": [], 354 | "source": [ 355 | "mdl.summary()" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": { 362 | "id": "UaNs24AuaugP" 363 | }, 364 | "outputs": [], 365 | "source": [ 366 | "mdl.compile(\n", 367 | " optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), \n", 368 | " loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),\n", 369 | " metrics=['accuracy'])" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": null, 375 | "metadata": { 376 | "id": "I8mmakSQawne" 377 | }, 378 | "outputs": [], 379 | "source": [ 380 | "steps_per_epoch = train_generator.samples // train_generator.batch_size\n", 381 | "validation_steps = valid_generator.samples // valid_generator.batch_size\n", 382 | "hist = mdl.fit(\n", 383 | " train_generator,\n", 384 | " epochs=13, steps_per_epoch=steps_per_epoch,\n", 385 | " validation_data=valid_generator,\n", 386 | " validation_steps=validation_steps).history" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "metadata": { 393 | "id": "74jqWBhray74" 394 | }, 395 | "outputs": [], 396 | "source": [] 397 | } 398 | ], 399 | "metadata": { 400 | "accelerator": "GPU", 401 | "colab": { 402 | "collapsed_sections": [], 403 | "name": "Oreilly_C5_flow_from_dataframe.ipynb", 404 | "private_outputs": true, 405 | "provenance": [] 406 | }, 407 | "kernelspec": { 408 | "display_name": "Python 3", 409 | "language": "python", 410 | "name": "python3" 411 | }, 412 | "language_info": { 413 | "codemirror_mode": { 414 | "name": "ipython", 415 | "version": 3 416 | }, 417 | "file_extension": ".py", 418 | "mimetype": "text/x-python", 419 | "name": "python", 420 | "nbconvert_exporter": "python", 421 | "pygments_lexer": "ipython3", 422 | "version": "3.8.5" 423 | } 424 | }, 425 | "nbformat": 4, 426 | "nbformat_minor": 1 427 | } 428 | -------------------------------------------------------------------------------- /chapter05/Oreilly_C5_flow_numpy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Oreilly_C5_flow_numpy.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "accelerator": "GPU" 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "5-PPVtXwmICr" 21 | }, 22 | "source": [ 23 | "## Reference\n", 24 | "https://www.tensorflow.org/tutorials/keras/classification\n", 25 | "https://www.tensorflow.org/tutorials/load_data/numpy#load_numpy_arrays_with_tfdatadataset" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "metadata": { 31 | "id": "mgR9pJQLl5ED", 32 | "colab": { 33 | "base_uri": "https://localhost:8080/" 34 | }, 35 | "outputId": "a150fac0-0cbf-425f-c77b-723b252cb29d" 36 | }, 37 | "source": [ 38 | "# TensorFlow and tf.keras\n", 39 | "import tensorflow as tf\n", 40 | "import numpy as np\n", 41 | "import matplotlib.pyplot as plt\n", 42 | "\n", 43 | "print(tf.__version__)" 44 | ], 45 | "execution_count": null, 46 | "outputs": [ 47 | { 48 | "output_type": "stream", 49 | "text": [ 50 | "2.3.0\n" 51 | ], 52 | "name": "stdout" 53 | } 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "metadata": { 59 | "id": "Ee1qQhjol_bl" 60 | }, 61 | "source": [ 62 | "fashion_mnist = tf.keras.datasets.fashion_mnist\n", 63 | "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()" 64 | ], 65 | "execution_count": null, 66 | "outputs": [] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "metadata": { 71 | "id": "Ds2nDvnUmRaV", 72 | "colab": { 73 | "base_uri": "https://localhost:8080/" 74 | }, 75 | "outputId": "3fead462-a7fa-47b3-d2a6-55741cf709eb" 76 | }, 77 | "source": [ 78 | "print(type(train_images), type(train_labels))" 79 | ], 80 | "execution_count": null, 81 | "outputs": [ 82 | { 83 | "output_type": "stream", 84 | "text": [ 85 | " \n" 86 | ], 87 | "name": "stdout" 88 | } 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "metadata": { 94 | "id": "6kmwhYwgtNPs", 95 | "colab": { 96 | "base_uri": "https://localhost:8080/" 97 | }, 98 | "outputId": "a6bfc9c7-ac5e-4e5c-cd18-0c90d0c6e7f5" 99 | }, 100 | "source": [ 101 | "print(train_images.shape, train_labels.shape)" 102 | ], 103 | "execution_count": null, 104 | "outputs": [ 105 | { 106 | "output_type": "stream", 107 | "text": [ 108 | "(60000, 28, 28) (60000,)\n" 109 | ], 110 | "name": "stdout" 111 | } 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "metadata": { 117 | "id": "vevLylBfmVG1", 118 | "colab": { 119 | "base_uri": "https://localhost:8080/", 120 | "height": 265 121 | }, 122 | "outputId": "b29a186f-6f38-4b14-9f84-fd2b5a4be268" 123 | }, 124 | "source": [ 125 | "plt.figure()\n", 126 | "plt.imshow(train_images[5])\n", 127 | "plt.colorbar()\n", 128 | "plt.grid(False)\n", 129 | "plt.show()" 130 | ], 131 | "execution_count": null, 132 | "outputs": [ 133 | { 134 | "output_type": "display_data", 135 | "data": { 136 | "image/png": "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\n", 137 | "text/plain": [ 138 | "
" 139 | ] 140 | }, 141 | "metadata": { 142 | "tags": [], 143 | "needs_background": "light" 144 | } 145 | } 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "id": "9UD5V_7lm1pI" 152 | }, 153 | "source": [ 154 | "train_images = train_images/255" 155 | ], 156 | "execution_count": null, 157 | "outputs": [] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "metadata": { 162 | "id": "WKSXPGQgnm20", 163 | "colab": { 164 | "base_uri": "https://localhost:8080/" 165 | }, 166 | "outputId": "bbfce46f-6bcd-456c-d627-a35ad68c9bf6" 167 | }, 168 | "source": [ 169 | "train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels))\n", 170 | "dataset" 171 | ], 172 | "execution_count": null, 173 | "outputs": [ 174 | { 175 | "output_type": "execute_result", 176 | "data": { 177 | "text/plain": [ 178 | "" 179 | ] 180 | }, 181 | "metadata": { 182 | "tags": [] 183 | }, 184 | "execution_count": 36 185 | } 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "metadata": { 191 | "id": "ig_W8cEpoMHx", 192 | "colab": { 193 | "base_uri": "https://localhost:8080/" 194 | }, 195 | "outputId": "60977128-872c-4498-bc2b-3cf77310e014" 196 | }, 197 | "source": [ 198 | "train_images.shape" 199 | ], 200 | "execution_count": null, 201 | "outputs": [ 202 | { 203 | "output_type": "execute_result", 204 | "data": { 205 | "text/plain": [ 206 | "(60000, 28, 28)" 207 | ] 208 | }, 209 | "metadata": { 210 | "tags": [] 211 | }, 212 | "execution_count": 37 213 | } 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "metadata": { 219 | "id": "g-CGlRoJHcW0" 220 | }, 221 | "source": [ 222 | "SHUFFLE_BUFFER_SIZE = 10000\n", 223 | "TRAIN_BATCH_SIZE = 50\n", 224 | "VALIDATION_BATCH_SIZE = 10000\n", 225 | "\n", 226 | "validation_ds = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).take(VALIDATION_SAMPLE_SIZE).batch(VALIDATION_BATCH_SIZE)\n", 227 | "train_ds = train_dataset.skip(VALIDATION_BATCH_SIZE).batch(TRAIN_BATCH_SIZE).repeat()" 228 | ], 229 | "execution_count": null, 230 | "outputs": [] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "metadata": { 235 | "id": "3Ya_rI93JXWE", 236 | "colab": { 237 | "base_uri": "https://localhost:8080/" 238 | }, 239 | "outputId": "49e53221-5e3f-4f86-8655-e6bcf8bb219e" 240 | }, 241 | "source": [ 242 | "50000/50" 243 | ], 244 | "execution_count": null, 245 | "outputs": [ 246 | { 247 | "output_type": "execute_result", 248 | "data": { 249 | "text/plain": [ 250 | "1000.0" 251 | ] 252 | }, 253 | "metadata": { 254 | "tags": [] 255 | }, 256 | "execution_count": 49 257 | } 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "metadata": { 263 | "id": "byWDwt_9IyDU" 264 | }, 265 | "source": [ 266 | "steps_per_epoch = 50000 // BATCH_SIZE\n", 267 | "validation_steps = 10000 // VALIDATION_BATCH_SIZE\n" 268 | ], 269 | "execution_count": null, 270 | "outputs": [] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "metadata": { 275 | "id": "WJ8t2CS-IgaH" 276 | }, 277 | "source": [ 278 | "model = tf.keras.Sequential([\n", 279 | " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", 280 | " tf.keras.layers.Dense(30, activation='relu'),\n", 281 | " tf.keras.layers.Dense(10)\n", 282 | "])\n", 283 | "\n", 284 | "model.compile(optimizer=tf.keras.optimizers.RMSprop(),\n", 285 | " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", 286 | " metrics=['sparse_categorical_accuracy'])" 287 | ], 288 | "execution_count": null, 289 | "outputs": [] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "dpJsP7HFIyuN", 295 | "colab": { 296 | "base_uri": "https://localhost:8080/" 297 | }, 298 | "outputId": "53253014-da08-4095-c00d-cab2762216d6" 299 | }, 300 | "source": [ 301 | "model.fit(\n", 302 | " train_ds,\n", 303 | " epochs=13, steps_per_epoch=steps_per_epoch,\n", 304 | " validation_data=validation_ds,\n", 305 | " validation_steps=validation_steps)" 306 | ], 307 | "execution_count": null, 308 | "outputs": [ 309 | { 310 | "output_type": "stream", 311 | "text": [ 312 | "Epoch 1/13\n", 313 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.5442 - sparse_categorical_accuracy: 0.8134 - val_loss: 0.4159 - val_sparse_categorical_accuracy: 0.8524\n", 314 | "Epoch 2/13\n", 315 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.4135 - sparse_categorical_accuracy: 0.8538 - val_loss: 0.3828 - val_sparse_categorical_accuracy: 0.8632\n", 316 | "Epoch 3/13\n", 317 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3801 - sparse_categorical_accuracy: 0.8651 - val_loss: 0.3775 - val_sparse_categorical_accuracy: 0.8669\n", 318 | "Epoch 4/13\n", 319 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3592 - sparse_categorical_accuracy: 0.8713 - val_loss: 0.3682 - val_sparse_categorical_accuracy: 0.8688\n", 320 | "Epoch 5/13\n", 321 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3405 - sparse_categorical_accuracy: 0.8791 - val_loss: 0.3452 - val_sparse_categorical_accuracy: 0.8745\n", 322 | "Epoch 6/13\n", 323 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3290 - sparse_categorical_accuracy: 0.8811 - val_loss: 0.3304 - val_sparse_categorical_accuracy: 0.8813\n", 324 | "Epoch 7/13\n", 325 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3201 - sparse_categorical_accuracy: 0.8848 - val_loss: 0.3521 - val_sparse_categorical_accuracy: 0.8738\n", 326 | "Epoch 8/13\n", 327 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3096 - sparse_categorical_accuracy: 0.8880 - val_loss: 0.3441 - val_sparse_categorical_accuracy: 0.8819\n", 328 | "Epoch 9/13\n", 329 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.3039 - sparse_categorical_accuracy: 0.8904 - val_loss: 0.3327 - val_sparse_categorical_accuracy: 0.8843\n", 330 | "Epoch 10/13\n", 331 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.2982 - sparse_categorical_accuracy: 0.8931 - val_loss: 0.3476 - val_sparse_categorical_accuracy: 0.8778\n", 332 | "Epoch 11/13\n", 333 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.2923 - sparse_categorical_accuracy: 0.8954 - val_loss: 0.3431 - val_sparse_categorical_accuracy: 0.8831\n", 334 | "Epoch 12/13\n", 335 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.2867 - sparse_categorical_accuracy: 0.8990 - val_loss: 0.3385 - val_sparse_categorical_accuracy: 0.8854\n", 336 | "Epoch 13/13\n", 337 | "1562/1562 [==============================] - 4s 3ms/step - loss: 0.2826 - sparse_categorical_accuracy: 0.8997 - val_loss: 0.3553 - val_sparse_categorical_accuracy: 0.8811\n" 338 | ], 339 | "name": "stdout" 340 | }, 341 | { 342 | "output_type": "execute_result", 343 | "data": { 344 | "text/plain": [ 345 | "" 346 | ] 347 | }, 348 | "metadata": { 349 | "tags": [] 350 | }, 351 | "execution_count": 54 352 | } 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "metadata": { 358 | "id": "sSizZ9JwoURM" 359 | }, 360 | "source": [ 361 | "for i in train_dataset.take(1):\n", 362 | " print(i)" 363 | ], 364 | "execution_count": null, 365 | "outputs": [] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "metadata": { 370 | "id": "loc1qhOEqBip" 371 | }, 372 | "source": [ 373 | "BATCH_SIZE = 32\n", 374 | "\n", 375 | "\n", 376 | "train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE).take()\n" 377 | ], 378 | "execution_count": null, 379 | "outputs": [] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "metadata": { 384 | "id": "K8jZj4iMGOVL", 385 | "colab": { 386 | "base_uri": "https://localhost:8080/" 387 | }, 388 | "outputId": "13aa0384-dfe9-47ce-9a92-6dc15dbeb4e8" 389 | }, 390 | "source": [ 391 | "train_dataset" 392 | ], 393 | "execution_count": null, 394 | "outputs": [ 395 | { 396 | "output_type": "execute_result", 397 | "data": { 398 | "text/plain": [ 399 | "" 400 | ] 401 | }, 402 | "metadata": { 403 | "tags": [] 404 | }, 405 | "execution_count": 26 406 | } 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "metadata": { 412 | "id": "uBAguyLQG74T" 413 | }, 414 | "source": [ 415 | "train" 416 | ], 417 | "execution_count": null, 418 | "outputs": [] 419 | } 420 | ] 421 | } -------------------------------------------------------------------------------- /chapter05/Oreilly_C5_text_data_from_directory.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Oreilly_C5_text_data_from_directory.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "accelerator": "GPU" 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "LdIQFHzSVKRM" 21 | }, 22 | "source": [ 23 | "## Reference\n", 24 | "https://www.tensorflow.org/tutorials/text/word_embeddings" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "metadata": { 30 | "id": "SGe0Z2OiUTEi" 31 | }, 32 | "source": [ 33 | "import io\n", 34 | "import os\n", 35 | "import re\n", 36 | "import shutil\n", 37 | "import string\n", 38 | "import tensorflow as tf\n", 39 | "\n", 40 | "from datetime import datetime\n", 41 | "from tensorflow.keras import Model, Sequential\n", 42 | "from tensorflow.keras.layers import Activation, Dense, Embedding, GlobalAveragePooling1D\n", 43 | "from tensorflow.keras.layers.experimental.preprocessing import TextVectorization" 44 | ], 45 | "execution_count": null, 46 | "outputs": [] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "metadata": { 51 | "id": "rkTnFKQ8VP97", 52 | "colab": { 53 | "base_uri": "https://localhost:8080/" 54 | }, 55 | "outputId": "5a0302a9-1c06-44c3-b243-c2de14aa6840" 56 | }, 57 | "source": [ 58 | "url = \"https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\"\n", 59 | "\n", 60 | "dataset = tf.keras.utils.get_file(\"aclImdb_v1.tar.gz\", url,\n", 61 | " untar=True, cache_dir='.',\n", 62 | " cache_subdir='')\n", 63 | "\n", 64 | "dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')\n", 65 | "os.listdir(dataset_dir)" 66 | ], 67 | "execution_count": null, 68 | "outputs": [ 69 | { 70 | "output_type": "stream", 71 | "text": [ 72 | "Downloading data from https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz\n", 73 | "84131840/84125825 [==============================] - 1s 0us/step\n" 74 | ], 75 | "name": "stdout" 76 | }, 77 | { 78 | "output_type": "execute_result", 79 | "data": { 80 | "text/plain": [ 81 | "['imdb.vocab', 'test', 'imdbEr.txt', 'README', 'train']" 82 | ] 83 | }, 84 | "metadata": { 85 | "tags": [] 86 | }, 87 | "execution_count": 2 88 | } 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "metadata": { 94 | "id": "zd-yc9Ehjyat", 95 | "colab": { 96 | "base_uri": "https://localhost:8080/", 97 | "height": 35 98 | }, 99 | "outputId": "8e384611-1b49-4e91-8907-ab1008023dd4" 100 | }, 101 | "source": [ 102 | "dataset_dir" 103 | ], 104 | "execution_count": null, 105 | "outputs": [ 106 | { 107 | "output_type": "execute_result", 108 | "data": { 109 | "application/vnd.google.colaboratory.intrinsic+json": { 110 | "type": "string" 111 | }, 112 | "text/plain": [ 113 | "'./aclImdb'" 114 | ] 115 | }, 116 | "metadata": { 117 | "tags": [] 118 | }, 119 | "execution_count": 3 120 | } 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "metadata": { 126 | "id": "Ept3aTb5jHVO", 127 | "colab": { 128 | "base_uri": "https://localhost:8080/", 129 | "height": 35 130 | }, 131 | "outputId": "adc457b3-0573-4ead-fef3-bd9008959caa" 132 | }, 133 | "source": [ 134 | "dataset" 135 | ], 136 | "execution_count": null, 137 | "outputs": [ 138 | { 139 | "output_type": "execute_result", 140 | "data": { 141 | "application/vnd.google.colaboratory.intrinsic+json": { 142 | "type": "string" 143 | }, 144 | "text/plain": [ 145 | "'./aclImdb_v1.tar.gz'" 146 | ] 147 | }, 148 | "metadata": { 149 | "tags": [] 150 | }, 151 | "execution_count": 4 152 | } 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "metadata": { 158 | "id": "gGfyjEq6jROO", 159 | "colab": { 160 | "base_uri": "https://localhost:8080/" 161 | }, 162 | "outputId": "65028b20-cc95-4605-8fea-59e8ee5536d5" 163 | }, 164 | "source": [ 165 | "!ls -lrt" 166 | ], 167 | "execution_count": null, 168 | "outputs": [ 169 | { 170 | "output_type": "stream", 171 | "text": [ 172 | "total 82168\n", 173 | "drwxr-xr-x 4 7297 1000 4096 Jun 26 2011 aclImdb\n", 174 | "drwxr-xr-x 1 root root 4096 Jan 20 17:27 sample_data\n", 175 | "-rw-r--r-- 1 root root 84125825 Jan 26 01:52 aclImdb_v1.tar.gz.tar.gz\n" 176 | ], 177 | "name": "stdout" 178 | } 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "metadata": { 184 | "id": "I94cLAG8iRbF", 185 | "colab": { 186 | "base_uri": "https://localhost:8080/", 187 | "height": 35 188 | }, 189 | "outputId": "3037ed02-2faa-43fd-d66f-88ff55ad2626" 190 | }, 191 | "source": [ 192 | "dataset_dir" 193 | ], 194 | "execution_count": null, 195 | "outputs": [ 196 | { 197 | "output_type": "execute_result", 198 | "data": { 199 | "application/vnd.google.colaboratory.intrinsic+json": { 200 | "type": "string" 201 | }, 202 | "text/plain": [ 203 | "'./aclImdb'" 204 | ] 205 | }, 206 | "metadata": { 207 | "tags": [] 208 | }, 209 | "execution_count": 6 210 | } 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "metadata": { 216 | "id": "gmhyc_RiVZyT", 217 | "colab": { 218 | "base_uri": "https://localhost:8080/" 219 | }, 220 | "outputId": "87ed3cae-1d15-4244-94cd-82a838080824" 221 | }, 222 | "source": [ 223 | "train_dir = os.path.join(dataset_dir, 'train')\n", 224 | "os.listdir(train_dir)" 225 | ], 226 | "execution_count": null, 227 | "outputs": [ 228 | { 229 | "output_type": "execute_result", 230 | "data": { 231 | "text/plain": [ 232 | "['unsupBow.feat',\n", 233 | " 'neg',\n", 234 | " 'pos',\n", 235 | " 'unsup',\n", 236 | " 'urls_pos.txt',\n", 237 | " 'urls_neg.txt',\n", 238 | " 'urls_unsup.txt',\n", 239 | " 'labeledBow.feat']" 240 | ] 241 | }, 242 | "metadata": { 243 | "tags": [] 244 | }, 245 | "execution_count": 7 246 | } 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "metadata": { 252 | "id": "tzRGokaLWFF2", 253 | "colab": { 254 | "base_uri": "https://localhost:8080/", 255 | "height": 35 256 | }, 257 | "outputId": "640f28c7-8bb4-44c6-be44-fa2a1cc2d8e1" 258 | }, 259 | "source": [ 260 | "dataset_dir" 261 | ], 262 | "execution_count": null, 263 | "outputs": [ 264 | { 265 | "output_type": "execute_result", 266 | "data": { 267 | "application/vnd.google.colaboratory.intrinsic+json": { 268 | "type": "string" 269 | }, 270 | "text/plain": [ 271 | "'./aclImdb'" 272 | ] 273 | }, 274 | "metadata": { 275 | "tags": [] 276 | }, 277 | "execution_count": 8 278 | } 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "metadata": { 284 | "id": "xrQ_zkyCWHZK", 285 | "colab": { 286 | "base_uri": "https://localhost:8080/" 287 | }, 288 | "outputId": "2e57b288-395f-4c1b-8a62-a022fb581c17" 289 | }, 290 | "source": [ 291 | "!ls -lrt ./aclImdb/train/pos | head -3" 292 | ], 293 | "execution_count": null, 294 | "outputs": [ 295 | { 296 | "output_type": "stream", 297 | "text": [ 298 | "total 51624\n", 299 | "-rw-r--r-- 1 7297 1000 975 Apr 12 2011 99_8.txt\n", 300 | "-rw-r--r-- 1 7297 1000 638 Apr 12 2011 98_10.txt\n" 301 | ], 302 | "name": "stdout" 303 | } 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "metadata": { 309 | "id": "IOKPICjEWnAl", 310 | "colab": { 311 | "base_uri": "https://localhost:8080/" 312 | }, 313 | "outputId": "355a7633-08d6-4b04-8827-a08da19d6f52" 314 | }, 315 | "source": [ 316 | "!cat ./aclImdb/train/pos/99_8.txt " 317 | ], 318 | "execution_count": null, 319 | "outputs": [ 320 | { 321 | "output_type": "stream", 322 | "text": [ 323 | "A Christmas Together actually came before my time, but I've been raised on John Denver and the songs from this special were always my family's Christmas music. For years we had a crackling cassette made from a record that meant it was Christmas. A few years ago, I was finally able to track down a video of it on Ebay, so after listening to all the music for some 21 years, I got to see John and the Muppets in action for myself. If you ever get the chance, it's a lot of fun--great music, heart-warming and cheesy. It's also interesting to see the 70's versions of the Muppets and compare them to their newer versions today. I believe Denver actually took some heat for doing a show like this--I guess normally performers don't compromise their images by doing sing-a-longs with the Muppets, but I'm glad he did. Even if you can't track down the video, the soundtrack is worth it too. It has some Muppified traditional favorites, but also some original Denver tunes as well." 324 | ], 325 | "name": "stdout" 326 | } 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "metadata": { 332 | "id": "3eRYIB0PVjUc" 333 | }, 334 | "source": [ 335 | "remove_dir = os.path.join(train_dir, 'unsup')\n", 336 | "shutil.rmtree(remove_dir)" 337 | ], 338 | "execution_count": null, 339 | "outputs": [] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "metadata": { 344 | "id": "6GO14bPXXhh8", 345 | "colab": { 346 | "base_uri": "https://localhost:8080/" 347 | }, 348 | "outputId": "1a346fc1-5377-44e0-920e-6eb9d8d99cf6" 349 | }, 350 | "source": [ 351 | "!ls -lrt ./aclImdb/train" 352 | ], 353 | "execution_count": null, 354 | "outputs": [ 355 | { 356 | "output_type": "stream", 357 | "text": [ 358 | "total 65200\n", 359 | "-rw-r--r-- 1 7297 1000 2450000 Apr 12 2011 urls_unsup.txt\n", 360 | "drwxr-xr-x 2 7297 1000 348160 Apr 12 2011 pos\n", 361 | "drwxr-xr-x 2 7297 1000 356352 Apr 12 2011 neg\n", 362 | "-rw-r--r-- 1 7297 1000 612500 Apr 12 2011 urls_pos.txt\n", 363 | "-rw-r--r-- 1 7297 1000 612500 Apr 12 2011 urls_neg.txt\n", 364 | "-rw-r--r-- 1 7297 1000 21021197 Apr 12 2011 labeledBow.feat\n", 365 | "-rw-r--r-- 1 7297 1000 41348699 Apr 12 2011 unsupBow.feat\n" 366 | ], 367 | "name": "stdout" 368 | } 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "metadata": { 374 | "id": "pQph5yFtVxnb", 375 | "colab": { 376 | "base_uri": "https://localhost:8080/" 377 | }, 378 | "outputId": "49299c2d-b412-4f10-e390-99afb8f9643d" 379 | }, 380 | "source": [ 381 | "batch_size = 1024\n", 382 | "seed = 123\n", 383 | "train_ds = tf.keras.preprocessing.text_dataset_from_directory(\n", 384 | " 'aclImdb/train', batch_size=batch_size, validation_split=0.2, \n", 385 | " subset='training', seed=seed)\n", 386 | "val_ds = tf.keras.preprocessing.text_dataset_from_directory(\n", 387 | " 'aclImdb/train', batch_size=batch_size, validation_split=0.2, \n", 388 | " subset='validation', seed=seed)" 389 | ], 390 | "execution_count": null, 391 | "outputs": [ 392 | { 393 | "output_type": "stream", 394 | "text": [ 395 | "Found 25000 files belonging to 2 classes.\n", 396 | "Using 20000 files for training.\n", 397 | "Found 25000 files belonging to 2 classes.\n", 398 | "Using 5000 files for validation.\n" 399 | ], 400 | "name": "stdout" 401 | } 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "metadata": { 407 | "id": "txq9_uC5Vz9s", 408 | "colab": { 409 | "base_uri": "https://localhost:8080/" 410 | }, 411 | "outputId": "b1fcecca-9ccb-4a27-ca35-db3e6ccc80a9" 412 | }, 413 | "source": [ 414 | "import random\n", 415 | "idx = random.sample(range(1, batch_size), 5)\n", 416 | "for text_batch, label_batch in train_ds.take(1):\n", 417 | " for i in idx:\n", 418 | " print(i, label_batch[i].numpy(), text_batch.numpy()[i])" 419 | ], 420 | "execution_count": null, 421 | "outputs": [ 422 | { 423 | "output_type": "stream", 424 | "text": [ 425 | "974 0 b'Bloodsuckers has the potential to be a somewhat decent movie, the concept of military types tracking down and battling vampires in space is one with some potential in the cheesier realm of things. Even the idea of the universe being full of various different breeds of vampire, all with different attributes, many of which the characters have yet to find out about, is kind of cool as well. As to how most of the life in the galaxy outside of earth is vampire, I\\'m not sure how the makers meant for that to work, given the nature of vampires. Who the hell they are meant to be feeding on if almost everyone is a vampire I don\\'t know. As it is the movie comes across a low budget mix of Firefly/Serenity and vampires movies with a dash of Aliens.

The action parts of the movie are pretty average and derivative (Particularly of Serenity) but passable- they are reasonably well executed and there is enough gore for a vampire flick, including some of the comical blood-spurting variety. There is a lot of character stuff, most of which is tedious, coming from conflicts between characters who mostly seem like whiny, immature arseholes- primarily cowboy dude and Asian woman. There are a few character scenes that actually kind of work and the actors don\\'t play it too badly but it mostly slows things down. A nice try at fleshing the characters out but people don\\'t watch a movie called Bloodsuckers for character development and drama. The acting is actually okay. Michael Ironside hams it up and is as fun to watch as ever and at least of a couple of the women are hot. The space SFX aren\\'t too bad for what is clearly a low budget work. The story is again pretty average and derivative but as I said the world created has a little bit of potential. The way things are set up Bloodsuckers really does seem like the pilot for a TV series- character dynamics introduced, the world introduced but not explored, etc.

The film does have a some highlights and head scratching moments- the kind of stuff that actually makes these dodgy productions watchable. -The scene where our heroes interrogate a talking sock puppet chestburster type creature. Hilarious. - The \"sex scene.\" WTF indeed. -The credit \"And Michael Ironside as Muco.\" The most annoying aspect of it all though is the really awful and usually inappropriate pop music they have playing very loud over half the scenes of the movie. It is painful to listen to and only detracts from what is only average at best.

Basically an okay watch is you\\'re up for something cheesy, even if it is just for the \"chestburster\" scene.'\n", 426 | "921 0 b\"This movie . . . I don't know. Why they would take such an indellible character as Pippi Longstocking and cast the singularly charmless Tami Erin, I will never know. Why they would spend money on art direction and some not-all-that-bad special effects, then not bother to edit it properly, I will never know. Why the sets and costumes are sometimes in period, and sometimes bizarrely not, why they commissioned SUCH bad songs, why the script doesn't make any sense whatsoever (not even on a silly, children's film level) . . . . what were they thinking?? Nothing about this movie is quite as it should be. Every single part is dubbed (and always poorly,) every sound effect is slightly wrong, every edit is in the wrong place, every performance is bad in some way. It does manage to create an appropriate atmosphere, despite all the problems, but it NEVER captures the magic that is Astrid Lindgren's creation.\"\n", 427 | "54 0 b\"Killer Tomatoes movies have this special kind of humor - you either love it or hate it. I personally like it, but in this fourth movie the feeling is gone. The tomatoes aren't the same, jokes are lame, even the actors aren't as funny. Because that's the only thing this kind of movies are supposed to be - funny.

So now following the plot made to laugh, is annoying. They really shouldn't have done the fourth part to the Killer Tomatoes trilogy.\"\n", 428 | "1003 0 b'Right up until the end the bad guys have the upper-hand - always - which kind of put into question the competence of the good guys. A couple of innocent-man-accused-of-a-crime plots are irritating. Some unnecessary dialogue in which various dull legal issues get debated. This is just a mediocre dumb old western, so what\\'s this nonsense about trying to keep things \"realistic\"? Cagney\\'s atypical presence in a western is one of the few - if not the only - entertaining thing about the movie. Somewhere around the middle there is a ridiculously-timed marriage proposal; sort of like \"Where is the Kid hiding??!! Where is he?!... Oh, and by the way, will you marry me?\"'\n", 429 | "839 1 b'Other commentators have detailed the plot and the social parables and commentary as well (or better) that I could, but I would like to join in my admiration for this little jewel of a film. It holds up very well indeed more that 50 years later in every category - screenplay, acting, photography, set design, sound design...it really is a classic of sorts. This was my first exposure to the \"young\" Alec Guinness, and it\\'s obvious from the first frames what made him so special as to eventually receive a knighthood.

I only rate it an \"8\" because it\\'s essentially a rather lightweight parable that examines human nature but doesn\\'t really skewer it; and because the plot takes the easy way out at the end, rather than actually resolving conflict between the inventor and the mill workers and industrialists who are chasing him all over town. Also, a couple minutes of thought reveals the basic flaw in the logic of the screenplay - wear and tear is hardly ever the determining factor in buying new clothes (especially dress clothes); children grow up, people change sizes, gain and lose weight, and go with the latest fashions all the time, and have as long as looms have woven cloth. And if nothing else, the manufacturer would make a fortune providing indestructible material for military uniforms (especially BDUs).

Still, this is a great film. If you get a chance to see it on a classic movie channel, you should.'\n" 430 | ], 431 | "name": "stdout" 432 | } 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "metadata": { 438 | "id": "UXpkVYbJX3fx", 439 | "colab": { 440 | "base_uri": "https://localhost:8080/" 441 | }, 442 | "outputId": "e78135cc-7708-45f0-a803-0665636faf13" 443 | }, 444 | "source": [ 445 | "type(train_ds)" 446 | ], 447 | "execution_count": null, 448 | "outputs": [ 449 | { 450 | "output_type": "execute_result", 451 | "data": { 452 | "text/plain": [ 453 | "tensorflow.python.data.ops.dataset_ops.BatchDataset" 454 | ] 455 | }, 456 | "metadata": { 457 | "tags": [] 458 | }, 459 | "execution_count": 15 460 | } 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "metadata": { 466 | "id": "IhyZRxhHY1Th" 467 | }, 468 | "source": [ 469 | "" 470 | ], 471 | "execution_count": null, 472 | "outputs": [] 473 | } 474 | ] 475 | } -------------------------------------------------------------------------------- /chapter05/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter06/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter07/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter07/oreilly_c7_cifar10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "oreilly_c7_cifar10.ipynb", 7 | "provenance": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "accelerator": "GPU" 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "brfUj9kxYh7r" 20 | }, 21 | "source": [ 22 | "import tensorflow as tf\n", 23 | "from tensorflow.keras import datasets, layers, models\n", 24 | "import numpy as np\n", 25 | "import matplotlib.pylab as plt\n", 26 | "import os" 27 | ], 28 | "execution_count": null, 29 | "outputs": [] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "metadata": { 34 | "id": "Hsx5ywgQYqhz" 35 | }, 36 | "source": [ 37 | "(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()\n", 38 | "\n", 39 | "# Normalize pixel values to be between 0 and 1\n", 40 | "train_images, test_images = train_images / 255.0, test_images / 255.0" 41 | ], 42 | "execution_count": null, 43 | "outputs": [] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "metadata": { 48 | "id": "hhPHjdeYYv7u" 49 | }, 50 | "source": [ 51 | "# Plain text name in alphabetical order. https://www.cs.toronto.edu/~kriz/cifar.html\n", 52 | "CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', \n", 53 | " 'deer','dog', 'frog', 'horse', 'ship', 'truck']" 54 | ], 55 | "execution_count": null, 56 | "outputs": [] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": { 61 | "id": "0Gxr-5SVe5xf" 62 | }, 63 | "source": [ 64 | "Let's use half of test data as the validation data." 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "metadata": { 70 | "id": "IelZX9Nger2R" 71 | }, 72 | "source": [ 73 | "validation_dataset = tf.data.Dataset.from_tensor_slices((test_images[:500], test_labels[:500]))\n", 74 | "test_dataset = tf.data.Dataset.from_tensor_slices((test_images[500:], test_labels[500:]))" 75 | ], 76 | "execution_count": null, 77 | "outputs": [] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": { 82 | "id": "EsxmMi_He94F" 83 | }, 84 | "source": [ 85 | "Now the datasets are built for validation and test data. Next, we will keep all training data for training." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "metadata": { 91 | "id": "lN0jT3XvewP3" 92 | }, 93 | "source": [ 94 | "# Create an instance of dataset from raw numpy images and labels.\n", 95 | "train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels))" 96 | ], 97 | "execution_count": null, 98 | "outputs": [] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "metadata": { 103 | "colab": { 104 | "base_uri": "https://localhost:8080/" 105 | }, 106 | "id": "yqkLO8EOfARt", 107 | "outputId": "848ef08f-b0e7-4b0f-b1ea-cbfac74be1a7" 108 | }, 109 | "source": [ 110 | "# https://www.tensorflow.org/api_docs/python/tf/data/Dataset#transformations_2\n", 111 | "train_dataset_size = len(list(train_dataset.as_numpy_iterator()))\n", 112 | "print('Training data sample size: ', train_dataset_size)" 113 | ], 114 | "execution_count": null, 115 | "outputs": [ 116 | { 117 | "output_type": "stream", 118 | "text": [ 119 | "Training data sample size: 50000\n" 120 | ], 121 | "name": "stdout" 122 | } 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "metadata": { 128 | "id": "-GRVCZYlfC7W" 129 | }, 130 | "source": [ 131 | "TRAIN_BATCH_SIZE = 200\n", 132 | "train_dataset = train_dataset.shuffle(50000).batch(TRAIN_BATCH_SIZE, drop_remainder=True)" 133 | ], 134 | "execution_count": null, 135 | "outputs": [] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "metadata": { 140 | "id": "D9rcvZhngWia" 141 | }, 142 | "source": [ 143 | "validation_dataset = validation_dataset.batch(500)\n", 144 | "test_dataset = test_dataset.batch(500)" 145 | ], 146 | "execution_count": null, 147 | "outputs": [] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "metadata": { 152 | "id": "UacH_ItBgZG7" 153 | }, 154 | "source": [ 155 | "STEPS_PER_EPOCH = train_dataset_size // TRAIN_BATCH_SIZE\n", 156 | "VALIDATION_STEPS = 1 #validation data // validation batch size" 157 | ], 158 | "execution_count": null, 159 | "outputs": [] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "metadata": { 164 | "id": "s6nRE_rTgb4T" 165 | }, 166 | "source": [ 167 | "model = tf.keras.Sequential([\n", 168 | " tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', \n", 169 | " kernel_initializer='glorot_uniform', padding='same', input_shape = (32,32,3)),\n", 170 | " tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", 171 | " tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu', \n", 172 | " kernel_initializer='glorot_uniform', padding='same'),\n", 173 | " tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),\n", 174 | " tf.keras.layers.Flatten(),\n", 175 | " tf.keras.layers.Dense(256, activation='relu', kernel_initializer='glorot_uniform'),\n", 176 | " tf.keras.layers.Dense(10, activation='softmax', name = 'custom_class')\n", 177 | "])\n", 178 | "model.build([None, 32, 32, 3])" 179 | ], 180 | "execution_count": null, 181 | "outputs": [] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "metadata": { 186 | "id": "hJ71ACqKgg17" 187 | }, 188 | "source": [ 189 | "model.compile(\n", 190 | " loss='sparse_categorical_crossentropy',\n", 191 | " optimizer=tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9),\n", 192 | " metrics=['accuracy'])" 193 | ], 194 | "execution_count": null, 195 | "outputs": [] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": { 200 | "id": "6LlkmbgBi9l5" 201 | }, 202 | "source": [ 203 | "Let's define some alias for file path to save model checkpoints." 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "metadata": { 209 | "colab": { 210 | "base_uri": "https://localhost:8080/" 211 | }, 212 | "id": "06Oed8lrgjCB", 213 | "outputId": "480b964c-39bd-411b-99d2-547b4f010f56" 214 | }, 215 | "source": [ 216 | "checkpoint_dir = './cifar10_training_checkpoints'\n", 217 | "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_{epoch}\")\n", 218 | "print(checkpoint_prefix)" 219 | ], 220 | "execution_count": null, 221 | "outputs": [ 222 | { 223 | "output_type": "stream", 224 | "text": [ 225 | "./cifar10_training_checkpoints/ckpt_{epoch}\n" 226 | ], 227 | "name": "stdout" 228 | } 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "id": "PhvXn7-M5yVM" 235 | }, 236 | "source": [ 237 | "See https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/ModelCheckpoint for details of function signature for `ModelCheckpoint`." 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "metadata": { 243 | "id": "2MwuK2ccjQXp" 244 | }, 245 | "source": [ 246 | "myCheckPoint = tf.keras.callbacks.ModelCheckpoint(\n", 247 | " filepath=checkpoint_prefix\n", 248 | " ,monitor='val_accuracy'\n", 249 | " ,mode='auto')" 250 | ], 251 | "execution_count": null, 252 | "outputs": [] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "metadata": { 257 | "id": "YrBygME81FCU" 258 | }, 259 | "source": [ 260 | "myCallbacks = [\n", 261 | " myCheckPoint\n", 262 | "]" 263 | ], 264 | "execution_count": null, 265 | "outputs": [] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "metadata": { 270 | "colab": { 271 | "base_uri": "https://localhost:8080/" 272 | }, 273 | "id": "_aTUigIA1KrJ", 274 | "outputId": "e8a6278f-18f1-4945-fc89-e276adf74066" 275 | }, 276 | "source": [ 277 | "hist = model.fit(\n", 278 | " train_dataset\n", 279 | " ,epochs=12\n", 280 | " ,steps_per_epoch=STEPS_PER_EPOCH\n", 281 | " ,validation_data=validation_dataset\n", 282 | " ,validation_steps=VALIDATION_STEPS\n", 283 | " ,callbacks=myCallbacks).history" 284 | ], 285 | "execution_count": null, 286 | "outputs": [ 287 | { 288 | "output_type": "stream", 289 | "text": [ 290 | "Epoch 1/12\n", 291 | "250/250 [==============================] - 10s 11ms/step - loss: 2.0132 - accuracy: 0.2614 - val_loss: 1.4556 - val_accuracy: 0.4720\n", 292 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_1/assets\n", 293 | "Epoch 2/12\n", 294 | "250/250 [==============================] - 4s 14ms/step - loss: 1.3883 - accuracy: 0.5050 - val_loss: 1.2688 - val_accuracy: 0.5680\n", 295 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_2/assets\n", 296 | "Epoch 3/12\n", 297 | "250/250 [==============================] - 4s 14ms/step - loss: 1.1359 - accuracy: 0.5997 - val_loss: 1.1601 - val_accuracy: 0.6000\n", 298 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_3/assets\n", 299 | "Epoch 4/12\n", 300 | "250/250 [==============================] - 4s 14ms/step - loss: 0.9931 - accuracy: 0.6470 - val_loss: 1.1630 - val_accuracy: 0.5900\n", 301 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_4/assets\n", 302 | "Epoch 5/12\n", 303 | "250/250 [==============================] - 4s 14ms/step - loss: 0.8318 - accuracy: 0.7064 - val_loss: 1.1640 - val_accuracy: 0.6120\n", 304 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_5/assets\n", 305 | "Epoch 6/12\n", 306 | "250/250 [==============================] - 4s 14ms/step - loss: 0.7329 - accuracy: 0.7394 - val_loss: 1.1168 - val_accuracy: 0.6020\n", 307 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_6/assets\n", 308 | "Epoch 7/12\n", 309 | "250/250 [==============================] - 4s 14ms/step - loss: 0.5943 - accuracy: 0.7902 - val_loss: 1.1273 - val_accuracy: 0.6100\n", 310 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_7/assets\n", 311 | "Epoch 8/12\n", 312 | "250/250 [==============================] - 4s 14ms/step - loss: 0.4931 - accuracy: 0.8261 - val_loss: 1.3179 - val_accuracy: 0.6380\n", 313 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_8/assets\n", 314 | "Epoch 9/12\n", 315 | "250/250 [==============================] - 4s 14ms/step - loss: 0.4000 - accuracy: 0.8598 - val_loss: 1.3279 - val_accuracy: 0.6100\n", 316 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_9/assets\n", 317 | "Epoch 10/12\n", 318 | "250/250 [==============================] - 4s 14ms/step - loss: 0.3438 - accuracy: 0.8832 - val_loss: 1.7378 - val_accuracy: 0.5700\n", 319 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_10/assets\n", 320 | "Epoch 11/12\n", 321 | "250/250 [==============================] - 4s 14ms/step - loss: 0.3488 - accuracy: 0.8808 - val_loss: 1.7586 - val_accuracy: 0.5620\n", 322 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_11/assets\n", 323 | "Epoch 12/12\n", 324 | "250/250 [==============================] - 4s 13ms/step - loss: 0.3316 - accuracy: 0.8883 - val_loss: 1.8673 - val_accuracy: 0.5960\n", 325 | "INFO:tensorflow:Assets written to: ./cifar10_training_checkpoints/ckpt_12/assets\n" 326 | ], 327 | "name": "stdout" 328 | } 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "metadata": { 334 | "colab": { 335 | "base_uri": "https://localhost:8080/" 336 | }, 337 | "id": "6Uddjxbd7Nkf", 338 | "outputId": "9d9f1981-1037-4446-88e6-826f8eb04b0c" 339 | }, 340 | "source": [ 341 | "type(hist)" 342 | ], 343 | "execution_count": null, 344 | "outputs": [ 345 | { 346 | "output_type": "execute_result", 347 | "data": { 348 | "text/plain": [ 349 | "dict" 350 | ] 351 | }, 352 | "metadata": { 353 | "tags": [] 354 | }, 355 | "execution_count": 16 356 | } 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "metadata": { 362 | "colab": { 363 | "base_uri": "https://localhost:8080/" 364 | }, 365 | "id": "z7qpF-_N7W5H", 366 | "outputId": "bae2725f-9289-44b1-c0fc-da7eee00ac1e" 367 | }, 368 | "source": [ 369 | "hist['val_accuracy']" 370 | ], 371 | "execution_count": null, 372 | "outputs": [ 373 | { 374 | "output_type": "execute_result", 375 | "data": { 376 | "text/plain": [ 377 | "[0.47200000286102295,\n", 378 | " 0.5680000185966492,\n", 379 | " 0.6000000238418579,\n", 380 | " 0.5899999737739563,\n", 381 | " 0.6119999885559082,\n", 382 | " 0.6019999980926514,\n", 383 | " 0.6100000143051147,\n", 384 | " 0.6380000114440918,\n", 385 | " 0.6100000143051147,\n", 386 | " 0.5699999928474426,\n", 387 | " 0.5619999766349792,\n", 388 | " 0.5960000157356262]" 389 | ] 390 | }, 391 | "metadata": { 392 | "tags": [] 393 | }, 394 | "execution_count": 17 395 | } 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "metadata": { 401 | "colab": { 402 | "base_uri": "https://localhost:8080/" 403 | }, 404 | "id": "-qllX1tQ7cnN", 405 | "outputId": "3c640ea3-76ce-4d87-e83d-a1a252e798fc" 406 | }, 407 | "source": [ 408 | "max_value = max(hist['val_accuracy'])\n", 409 | "max_index = hist['val_accuracy'].index(max_value)\n", 410 | "print('Best epoch: ', max_index + 1)" 411 | ], 412 | "execution_count": null, 413 | "outputs": [ 414 | { 415 | "output_type": "stream", 416 | "text": [ 417 | "Best epoch: 8\n" 418 | ], 419 | "name": "stdout" 420 | } 421 | ] 422 | }, 423 | { 424 | "cell_type": "markdown", 425 | "metadata": { 426 | "id": "eZGFV9zU2NsO" 427 | }, 428 | "source": [ 429 | "Epoch 7 yielded model with highest accuracy. Now take a look at the checkpoint directory:" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "metadata": { 435 | "colab": { 436 | "base_uri": "https://localhost:8080/" 437 | }, 438 | "id": "2lmy7ZZK1rIw", 439 | "outputId": "e0cc59cd-4f61-40c8-e7a0-baad32304aaa" 440 | }, 441 | "source": [ 442 | "!ls -lrt ./cifar10_training_checkpoints" 443 | ], 444 | "execution_count": null, 445 | "outputs": [ 446 | { 447 | "output_type": "stream", 448 | "text": [ 449 | "total 48\n", 450 | "drwxr-xr-x 4 root root 4096 Jan 14 01:56 ckpt_1\n", 451 | "drwxr-xr-x 4 root root 4096 Jan 14 01:56 ckpt_2\n", 452 | "drwxr-xr-x 4 root root 4096 Jan 14 01:56 ckpt_3\n", 453 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_4\n", 454 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_5\n", 455 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_6\n", 456 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_7\n", 457 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_8\n", 458 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_9\n", 459 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_10\n", 460 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_11\n", 461 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_12\n" 462 | ], 463 | "name": "stdout" 464 | } 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "metadata": { 470 | "colab": { 471 | "base_uri": "https://localhost:8080/" 472 | }, 473 | "id": "BlDuvbEd2GKR", 474 | "outputId": "b4f221b4-7164-4bf9-eddb-3286bc67b93d" 475 | }, 476 | "source": [ 477 | "!ls -lrt ./cifar10_training_checkpoints/ckpt_7" 478 | ], 479 | "execution_count": null, 480 | "outputs": [ 481 | { 482 | "output_type": "stream", 483 | "text": [ 484 | "total 136\n", 485 | "drwxr-xr-x 2 root root 4096 Jan 14 01:57 variables\n", 486 | "drwxr-xr-x 2 root root 4096 Jan 14 01:57 assets\n", 487 | "-rw-r--r-- 1 root root 127273 Jan 14 01:57 saved_model.pb\n" 488 | ], 489 | "name": "stdout" 490 | } 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "metadata": { 496 | "colab": { 497 | "base_uri": "https://localhost:8080/" 498 | }, 499 | "id": "MpbDvjg22iuz", 500 | "outputId": "4740fd37-59c8-4424-8e33-b10d748e9d30" 501 | }, 502 | "source": [ 503 | "best_only_checkpoint_dir = './best_only_cifar10_training_checkpoints'\n", 504 | "best_only_checkpoint_prefix = os.path.join(best_only_checkpoint_dir, \"ckpt_{epoch}\")\n", 505 | "print(best_only_checkpoint_prefix)" 506 | ], 507 | "execution_count": null, 508 | "outputs": [ 509 | { 510 | "output_type": "stream", 511 | "text": [ 512 | "./best_only_cifar10_training_checkpoints/ckpt_{epoch}\n" 513 | ], 514 | "name": "stdout" 515 | } 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "metadata": { 521 | "id": "-j0FEPcy3N2E" 522 | }, 523 | "source": [ 524 | "bestCheckPoint = tf.keras.callbacks.ModelCheckpoint(\n", 525 | " filepath=best_only_checkpoint_prefix\n", 526 | " ,monitor='val_accuracy'\n", 527 | " ,mode='auto'\n", 528 | " ,save_best_only=True)" 529 | ], 530 | "execution_count": null, 531 | "outputs": [] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "metadata": { 536 | "id": "vD9Zu1xt3Dgz" 537 | }, 538 | "source": [ 539 | "bestCallbacks = [\n", 540 | " bestCheckPoint\n", 541 | "]" 542 | ], 543 | "execution_count": null, 544 | "outputs": [] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "metadata": { 549 | "colab": { 550 | "base_uri": "https://localhost:8080/" 551 | }, 552 | "id": "TE-kzHwy3aJ4", 553 | "outputId": "05927bc6-e1e3-497e-ab44-8fabe1eb23d0" 554 | }, 555 | "source": [ 556 | "best_hist = model.fit(\n", 557 | " train_dataset\n", 558 | " ,epochs=12\n", 559 | " ,steps_per_epoch=STEPS_PER_EPOCH\n", 560 | " ,validation_data=validation_dataset\n", 561 | " ,validation_steps=VALIDATION_STEPS\n", 562 | " ,callbacks=bestCallbacks).history" 563 | ], 564 | "execution_count": null, 565 | "outputs": [ 566 | { 567 | "output_type": "stream", 568 | "text": [ 569 | "Epoch 1/12\n", 570 | "250/250 [==============================] - 3s 10ms/step - loss: 0.3301 - accuracy: 0.8901 - val_loss: 2.0081 - val_accuracy: 0.6000\n", 571 | "INFO:tensorflow:Assets written to: ./best_only_cifar10_training_checkpoints/ckpt_1/assets\n", 572 | "Epoch 2/12\n", 573 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3254 - accuracy: 0.8950 - val_loss: 2.0864 - val_accuracy: 0.6000\n", 574 | "Epoch 3/12\n", 575 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3404 - accuracy: 0.8919 - val_loss: 2.2601 - val_accuracy: 0.6020\n", 576 | "INFO:tensorflow:Assets written to: ./best_only_cifar10_training_checkpoints/ckpt_3/assets\n", 577 | "Epoch 4/12\n", 578 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3122 - accuracy: 0.9012 - val_loss: 2.2877 - val_accuracy: 0.5960\n", 579 | "Epoch 5/12\n", 580 | "250/250 [==============================] - 3s 9ms/step - loss: 0.2989 - accuracy: 0.9095 - val_loss: 2.3951 - val_accuracy: 0.6000\n", 581 | "Epoch 6/12\n", 582 | "250/250 [==============================] - 3s 9ms/step - loss: 0.2640 - accuracy: 0.9184 - val_loss: 2.6985 - val_accuracy: 0.5680\n", 583 | "Epoch 7/12\n", 584 | "250/250 [==============================] - 3s 9ms/step - loss: 0.2922 - accuracy: 0.9149 - val_loss: 2.7420 - val_accuracy: 0.5960\n", 585 | "Epoch 8/12\n", 586 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3041 - accuracy: 0.9122 - val_loss: 2.7777 - val_accuracy: 0.5760\n", 587 | "Epoch 9/12\n", 588 | "250/250 [==============================] - 3s 9ms/step - loss: 0.2666 - accuracy: 0.9224 - val_loss: 3.0843 - val_accuracy: 0.6200\n", 589 | "INFO:tensorflow:Assets written to: ./best_only_cifar10_training_checkpoints/ckpt_9/assets\n", 590 | "Epoch 10/12\n", 591 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3142 - accuracy: 0.9134 - val_loss: 3.1054 - val_accuracy: 0.5920\n", 592 | "Epoch 11/12\n", 593 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3350 - accuracy: 0.9095 - val_loss: 2.6478 - val_accuracy: 0.6160\n", 594 | "Epoch 12/12\n", 595 | "250/250 [==============================] - 3s 9ms/step - loss: 0.3093 - accuracy: 0.9159 - val_loss: 2.8342 - val_accuracy: 0.5840\n" 596 | ], 597 | "name": "stdout" 598 | } 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": { 604 | "id": "t71116tX3wzO" 605 | }, 606 | "source": [ 607 | "Let's take a look at checkpoint directory where you set `set_best_only` to `True`:" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "metadata": { 613 | "colab": { 614 | "base_uri": "https://localhost:8080/" 615 | }, 616 | "id": "QjByW4y43iX3", 617 | "outputId": "d5981cc2-e232-46e2-ddaf-44d6ce2972aa" 618 | }, 619 | "source": [ 620 | "!ls -lrt ./best_only_cifar10_training_checkpoints" 621 | ], 622 | "execution_count": null, 623 | "outputs": [ 624 | { 625 | "output_type": "stream", 626 | "text": [ 627 | "total 12\n", 628 | "drwxr-xr-x 4 root root 4096 Jan 14 01:57 ckpt_1\n", 629 | "drwxr-xr-x 4 root root 4096 Jan 14 01:58 ckpt_3\n", 630 | "drwxr-xr-x 4 root root 4096 Jan 14 01:58 ckpt_9\n" 631 | ], 632 | "name": "stdout" 633 | } 634 | ] 635 | }, 636 | { 637 | "cell_type": "markdown", 638 | "metadata": { 639 | "id": "MKlUy7TU4XrA" 640 | }, 641 | "source": [ 642 | "Not all checkpoints are saved. This is because `save_best_only` option lets you save checkpoints after the first epoch only if there is an incremental improvement to the model metric of your choice. You can how many times the model was saved. This means the model performance in validation accuracy (`val_accuracy`) is best at the last time the model was saved. Therefore the last checkpoint saved is the best model." 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "metadata": { 648 | "id": "Svqp_GS73-O6" 649 | }, 650 | "source": [ 651 | "" 652 | ], 653 | "execution_count": null, 654 | "outputs": [] 655 | } 656 | ] 657 | } -------------------------------------------------------------------------------- /chapter08/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter09/OReilly_C9_TFS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "2.4.1\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import tensorflow as tf\n", 18 | "from tensorflow.keras import datasets\n", 19 | "import requests\n", 20 | "import json\n", 21 | "import numpy as np\n", 22 | "print(tf.__version__)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 47, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()\n", 32 | "\n", 33 | "# Normalize pixel values to be between 0 and 1\n", 34 | "train_images, test_images = train_images / 255.0, test_images / 255.0\n", 35 | "test_images = test_images[500:510]" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 48, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "(10, 32, 32, 3)" 47 | ] 48 | }, 49 | "execution_count": 48, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "test_images.shape" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "## Build JSON Payload" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 52, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "DATA = json.dumps({ \n", 72 | " \"instances\": test_images.tolist()\n", 73 | "})\n", 74 | "HEADERS = {\"content-type\": \"application/json\"}" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 53, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "response = requests.post('http://localhost:8501/v1/models/cifar10:predict', data=data, headers=headers)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 54, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "{'predictions': [[9.83229938e-07,\n", 95 | " 1.24386987e-10,\n", 96 | " 0.0419323482,\n", 97 | " 0.00232415553,\n", 98 | " 0.91928196,\n", 99 | " 3.26286099e-05,\n", 100 | " 0.0276549552,\n", 101 | " 0.00877290778,\n", 102 | " 8.02750222e-08,\n", 103 | " 5.4040652e-09],\n", 104 | " [2.70792543e-06,\n", 105 | " 2.05851364e-10,\n", 106 | " 0.00334585016,\n", 107 | " 0.000109534645,\n", 108 | " 0.00604483904,\n", 109 | " 4.74581793e-06,\n", 110 | " 0.990420401,\n", 111 | " 7.20195e-05,\n", 112 | " 1.21018039e-12,\n", 113 | " 2.66253382e-13],\n", 114 | " [1.14282244e-07,\n", 115 | " 1.07032106e-13,\n", 116 | " 0.0244066827,\n", 117 | " 0.924315095,\n", 118 | " 0.000137513853,\n", 119 | " 0.00514720846,\n", 120 | " 0.0340753384,\n", 121 | " 0.0119126532,\n", 122 | " 5.44377508e-06,\n", 123 | " 3.3045755e-10],\n", 124 | " [2.12039424e-07,\n", 125 | " 3.43116681e-11,\n", 126 | " 0.00244250102,\n", 127 | " 0.000632671348,\n", 128 | " 0.996785164,\n", 129 | " 0.000100670804,\n", 130 | " 1.11723175e-05,\n", 131 | " 2.72208981e-05,\n", 132 | " 2.03737773e-08,\n", 133 | " 3.32338175e-07],\n", 134 | " [1.08417469e-10,\n", 135 | " 0.00288137654,\n", 136 | " 1.6033519e-10,\n", 137 | " 1.22380017e-09,\n", 138 | " 1.62264785e-10,\n", 139 | " 6.14383822e-10,\n", 140 | " 5.40419354e-09,\n", 141 | " 8.52533071e-15,\n", 142 | " 9.84657955e-09,\n", 143 | " 0.997118592],\n", 144 | " [8.84121675e-07,\n", 145 | " 9.42742417e-09,\n", 146 | " 0.00117226667,\n", 147 | " 0.0114231901,\n", 148 | " 0.986208558,\n", 149 | " 4.50054868e-05,\n", 150 | " 2.09770815e-06,\n", 151 | " 0.00114794262,\n", 152 | " 5.20608268e-08,\n", 153 | " 5.65778702e-10],\n", 154 | " [1.14234373e-08,\n", 155 | " 1.89127533e-12,\n", 156 | " 8.19314838e-09,\n", 157 | " 2.80295353e-06,\n", 158 | " 9.37847176e-07,\n", 159 | " 2.98986674e-06,\n", 160 | " 1.6240443e-11,\n", 161 | " 0.999993205,\n", 162 | " 1.2253294e-15,\n", 163 | " 3.27461461e-11],\n", 164 | " [2.12445173e-07,\n", 165 | " 1.08091717e-05,\n", 166 | " 8.17003265e-09,\n", 167 | " 1.66651784e-12,\n", 168 | " 1.17434074e-09,\n", 169 | " 7.21443304e-12,\n", 170 | " 3.89313416e-16,\n", 171 | " 4.04915795e-10,\n", 172 | " 6.81396344e-08,\n", 173 | " 0.999988794],\n", 174 | " [0.00395464711,\n", 175 | " 0.0116107687,\n", 176 | " 0.0180630274,\n", 177 | " 0.846499,\n", 178 | " 0.00650618831,\n", 179 | " 0.0216390081,\n", 180 | " 0.00277129305,\n", 181 | " 4.85931189e-08,\n", 182 | " 0.000520401,\n", 183 | " 0.0884355903],\n", 184 | " [2.60355654e-10,\n", 185 | " 5.17050935e-09,\n", 186 | " 0.000181202529,\n", 187 | " 1.92517109e-06,\n", 188 | " 0.999798834,\n", 189 | " 1.04122219e-05,\n", 190 | " 3.32912987e-06,\n", 191 | " 4.38272036e-06,\n", 192 | " 4.2479078e-09,\n", 193 | " 9.54967494e-11]]}" 194 | ] 195 | }, 196 | "execution_count": 54, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "response.json()" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 28, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "array([[[[0.61960784, 0.43921569, 0.19215686],\n", 214 | " [0.62352941, 0.43529412, 0.18431373],\n", 215 | " [0.64705882, 0.45490196, 0.2 ],\n", 216 | " ...,\n", 217 | " [0.5372549 , 0.37254902, 0.14117647],\n", 218 | " [0.49411765, 0.35686275, 0.14117647],\n", 219 | " [0.45490196, 0.33333333, 0.12941176]],\n", 220 | "\n", 221 | " [[0.59607843, 0.43921569, 0.2 ],\n", 222 | " [0.59215686, 0.43137255, 0.15686275],\n", 223 | " [0.62352941, 0.44705882, 0.17647059],\n", 224 | " ...,\n", 225 | " [0.53333333, 0.37254902, 0.12156863],\n", 226 | " [0.49019608, 0.35686275, 0.1254902 ],\n", 227 | " [0.46666667, 0.34509804, 0.13333333]],\n", 228 | "\n", 229 | " [[0.59215686, 0.43137255, 0.18431373],\n", 230 | " [0.59215686, 0.42745098, 0.12941176],\n", 231 | " [0.61960784, 0.43529412, 0.14117647],\n", 232 | " ...,\n", 233 | " [0.54509804, 0.38431373, 0.13333333],\n", 234 | " [0.50980392, 0.37254902, 0.13333333],\n", 235 | " [0.47058824, 0.34901961, 0.12941176]],\n", 236 | "\n", 237 | " ...,\n", 238 | "\n", 239 | " [[0.26666667, 0.48627451, 0.69411765],\n", 240 | " [0.16470588, 0.39215686, 0.58039216],\n", 241 | " [0.12156863, 0.34509804, 0.5372549 ],\n", 242 | " ...,\n", 243 | " [0.14901961, 0.38039216, 0.57254902],\n", 244 | " [0.05098039, 0.25098039, 0.42352941],\n", 245 | " [0.15686275, 0.33333333, 0.49803922]],\n", 246 | "\n", 247 | " [[0.23921569, 0.45490196, 0.65882353],\n", 248 | " [0.19215686, 0.4 , 0.58039216],\n", 249 | " [0.1372549 , 0.33333333, 0.51764706],\n", 250 | " ...,\n", 251 | " [0.10196078, 0.32156863, 0.50980392],\n", 252 | " [0.11372549, 0.32156863, 0.49411765],\n", 253 | " [0.07843137, 0.25098039, 0.41960784]],\n", 254 | "\n", 255 | " [[0.21176471, 0.41960784, 0.62745098],\n", 256 | " [0.21960784, 0.41176471, 0.58431373],\n", 257 | " [0.17647059, 0.34901961, 0.51764706],\n", 258 | " ...,\n", 259 | " [0.09411765, 0.30196078, 0.48627451],\n", 260 | " [0.13333333, 0.32941176, 0.50588235],\n", 261 | " [0.08235294, 0.2627451 , 0.43137255]]]])" 262 | ] 263 | }, 264 | "execution_count": 28, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "test_images" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 3", 284 | "language": "python", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 3 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython3", 297 | "version": "3.8.5" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 4 302 | } 303 | -------------------------------------------------------------------------------- /chapter09/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung -------------------------------------------------------------------------------- /chapter10/OReilly_C10_CIFAR10_Hyperparameter_Tuning.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "OReilly_C10_CIFAR10_Hyperparameter_Tuning.ipynb", 7 | "provenance": [], 8 | "authorship_tag": "ABX9TyPnT8RQGF7yNNcGJ2B92+0t", 9 | "include_colab_link": true 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | }, 15 | "language_info": { 16 | "name": "python" 17 | }, 18 | "accelerator": "GPU" 19 | }, 20 | "cells": [ 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "id": "view-in-github", 25 | "colab_type": "text" 26 | }, 27 | "source": [ 28 | "\"Open" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "metadata": { 34 | "colab": { 35 | "base_uri": "https://localhost:8080/" 36 | }, 37 | "id": "sloNQ67s-jeN", 38 | "outputId": "9dbf1c70-de25-4fd5-c90a-cf23f19b8413" 39 | }, 40 | "source": [ 41 | "pip install -q -U keras-tuner" 42 | ], 43 | "execution_count": null, 44 | "outputs": [ 45 | { 46 | "output_type": "stream", 47 | "text": [ 48 | "\u001b[?25l\r\u001b[K |█████▏ | 10kB 23.7MB/s eta 0:00:01\r\u001b[K |██████████▍ | 20kB 15.4MB/s eta 0:00:01\r\u001b[K |███████████████▋ | 30kB 13.0MB/s eta 0:00:01\r\u001b[K |████████████████████▉ | 40kB 12.2MB/s eta 0:00:01\r\u001b[K |██████████████████████████ | 51kB 7.8MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▎| 61kB 8.2MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 71kB 5.4MB/s \n", 49 | "\u001b[?25h Building wheel for keras-tuner (setup.py) ... \u001b[?25l\u001b[?25hdone\n", 50 | " Building wheel for terminaltables (setup.py) ... \u001b[?25l\u001b[?25hdone\n" 51 | ], 52 | "name": "stdout" 53 | } 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "metadata": { 59 | "id": "uYegFb29-Se1" 60 | }, 61 | "source": [ 62 | "import tensorflow as tf\n", 63 | "import kerastuner as kt\n", 64 | "from tensorflow.keras import datasets, layers, models\n", 65 | "import numpy as np\n", 66 | "import matplotlib.pylab as plt\n", 67 | "import os\n", 68 | "from datetime import datetime" 69 | ], 70 | "execution_count": null, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "metadata": { 76 | "id": "zJcUMQnOfnF9" 77 | }, 78 | "source": [ 79 | "from datetime import datetime" 80 | ], 81 | "execution_count": null, 82 | "outputs": [] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "metadata": { 87 | "colab": { 88 | "base_uri": "https://localhost:8080/" 89 | }, 90 | "id": "OCitkpUU-Z_0", 91 | "outputId": "ba534c69-dd5f-4083-d6aa-55b5d5c4fefa" 92 | }, 93 | "source": [ 94 | "print(tf.__version__)" 95 | ], 96 | "execution_count": null, 97 | "outputs": [ 98 | { 99 | "output_type": "stream", 100 | "text": [ 101 | "2.4.1\n" 102 | ], 103 | "name": "stdout" 104 | } 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "metadata": { 110 | "colab": { 111 | "base_uri": "https://localhost:8080/" 112 | }, 113 | "id": "GBvJyq48j6Zp", 114 | "outputId": "82a46933-f707-435e-aef8-aa64886ed19d" 115 | }, 116 | "source": [ 117 | "(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()\n", 118 | "\n", 119 | "# Normalize pixel values to be between 0 and 1\n", 120 | "train_images, test_images = train_images / 255.0, test_images / 255.0" 121 | ], 122 | "execution_count": null, 123 | "outputs": [ 124 | { 125 | "output_type": "stream", 126 | "text": [ 127 | "Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n", 128 | "170500096/170498071 [==============================] - 3s 0us/step\n" 129 | ], 130 | "name": "stdout" 131 | } 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "metadata": { 137 | "id": "NRC5A5oNkHwi" 138 | }, 139 | "source": [ 140 | "# Plain text name in alphabetical order. https://www.cs.toronto.edu/~kriz/cifar.html\n", 141 | "CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', \n", 142 | " 'deer','dog', 'frog', 'horse', 'ship', 'truck']" 143 | ], 144 | "execution_count": null, 145 | "outputs": [] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "metadata": { 150 | "id": "jJfzAFG7kMEm" 151 | }, 152 | "source": [ 153 | "validation_dataset = tf.data.Dataset.from_tensor_slices((test_images[:500], test_labels[:500]))\n", 154 | "test_dataset = tf.data.Dataset.from_tensor_slices((test_images[500:], test_labels[500:]))" 155 | ], 156 | "execution_count": null, 157 | "outputs": [] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "metadata": { 162 | "id": "-5jPKkxEkO_s" 163 | }, 164 | "source": [ 165 | "# Create an instance of dataset from raw numpy images and labels.\n", 166 | "train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels))" 167 | ], 168 | "execution_count": null, 169 | "outputs": [] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "metadata": { 174 | "colab": { 175 | "base_uri": "https://localhost:8080/" 176 | }, 177 | "id": "bjnRZrTjkTeR", 178 | "outputId": "ff811034-de99-45e8-ab07-0aebe907e4b1" 179 | }, 180 | "source": [ 181 | "# https://www.tensorflow.org/api_docs/python/tf/data/Dataset#transformations_2\n", 182 | "train_dataset_size = len(list(train_dataset.as_numpy_iterator()))\n", 183 | "print('Training data sample size: ', train_dataset_size)\n", 184 | "\n", 185 | "validation_dataset_size = len(list(validation_dataset.as_numpy_iterator()))\n", 186 | "print('Validation data sample size: ', validation_dataset_size)\n", 187 | "\n", 188 | "test_dataset_size = len(list(test_dataset.as_numpy_iterator()))\n", 189 | "print('Test data sample size: ', test_dataset_size)" 190 | ], 191 | "execution_count": null, 192 | "outputs": [ 193 | { 194 | "output_type": "stream", 195 | "text": [ 196 | "Training data sample size: 50000\n", 197 | "Validation data sample size: 500\n", 198 | "Test data sample size: 9500\n" 199 | ], 200 | "name": "stdout" 201 | } 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": { 207 | "id": "aelXu2mkkdXp" 208 | }, 209 | "source": [ 210 | "## Define a distribution strategy\n", 211 | "Create a `MirroredStrategy` object to handle distributed training." 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/" 219 | }, 220 | "id": "siE2_JaBkWYB", 221 | "outputId": "93f92d78-999a-4af6-a569-f0097821e2aa" 222 | }, 223 | "source": [ 224 | "strategy = tf.distribute.MirroredStrategy()" 225 | ], 226 | "execution_count": null, 227 | "outputs": [ 228 | { 229 | "output_type": "stream", 230 | "text": [ 231 | "INFO:tensorflow:Using MirroredStrategy with devices ('/job:localhost/replica:0/task:0/device:GPU:0',)\n" 232 | ], 233 | "name": "stdout" 234 | } 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "metadata": { 240 | "colab": { 241 | "base_uri": "https://localhost:8080/" 242 | }, 243 | "id": "tB6hxFwCkfqT", 244 | "outputId": "aa215130-e17f-48fc-c38e-a75ffc56fab3" 245 | }, 246 | "source": [ 247 | "print('Number of devices: {}'.format(strategy.num_replicas_in_sync))" 248 | ], 249 | "execution_count": null, 250 | "outputs": [ 251 | { 252 | "output_type": "stream", 253 | "text": [ 254 | "Number of devices: 1\n" 255 | ], 256 | "name": "stdout" 257 | } 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "metadata": { 263 | "id": "ncEy9lgNkiMI" 264 | }, 265 | "source": [ 266 | "BUFFER_SIZE = 10000\n", 267 | "\n", 268 | "BATCH_SIZE_PER_REPLICA = 64\n", 269 | "BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync" 270 | ], 271 | "execution_count": null, 272 | "outputs": [] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "metadata": { 277 | "id": "C0JYDprLkkrV" 278 | }, 279 | "source": [ 280 | "train_dataset = train_dataset.repeat().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)\n", 281 | "validation_dataset = validation_dataset.shuffle(BUFFER_SIZE).batch(validation_dataset_size)\n", 282 | "test_dataset = test_dataset.batch(test_dataset_size)\n" 283 | ], 284 | "execution_count": null, 285 | "outputs": [] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "metadata": { 290 | "id": "D9LsFdm-km78" 291 | }, 292 | "source": [ 293 | "STEPS_PER_EPOCH = train_dataset_size // BATCH_SIZE_PER_REPLICA\n", 294 | "VALIDATION_STEPS = 1" 295 | ], 296 | "execution_count": null, 297 | "outputs": [] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "metadata": { 302 | "id": "IqxCig1FlqVE" 303 | }, 304 | "source": [ 305 | "\n", 306 | "def build_model(hp):\n", 307 | " model = tf.keras.Sequential()\n", 308 | " # Node count for next layer as hyperparameter\n", 309 | " hp_node_count = hp.Int('units', min_value=16, max_value=32, step=8)\n", 310 | " model.add(tf.keras.layers.Conv2D(filters = hp_node_count, \n", 311 | " kernel_size=(3, 3), \n", 312 | " activation='relu', \n", 313 | " name = 'conv_1',\n", 314 | " kernel_initializer='glorot_uniform', \n", 315 | " padding='same', input_shape = (32,32,3)))\n", 316 | " model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))\n", 317 | " model.add(tf.keras.layers.Flatten(name = 'flat_1'))\n", 318 | " # Activation function for next layer as hyperparameter\n", 319 | " hp_AF = hp.Choice('dense_activation', values = ['relu', 'tanh'])\n", 320 | " model.add(tf.keras.layers.Dense(256, activation=hp_AF, \n", 321 | " kernel_initializer='glorot_uniform', \n", 322 | " name = 'dense_1'))\n", 323 | " model.add(tf.keras.layers.Dense(10, activation='softmax', \n", 324 | " name = 'custom_class'))\n", 325 | "\n", 326 | " \n", 327 | " model.build([None, 32, 32, 3])\n", 328 | " # Compile model with optimizer \n", 329 | " # Learning rate as hyperparameter\n", 330 | " hp_LR = hp.Float('learning_rate', 1e-2, 1e-4)\n", 331 | " \n", 332 | " model.compile(\n", 333 | " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", 334 | " optimizer=tf.keras.optimizers.Adam(learning_rate=hp_LR),\n", 335 | " metrics=['accuracy'])\n", 336 | " \n", 337 | " return model" 338 | ], 339 | "execution_count": null, 340 | "outputs": [] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "metadata": { 345 | "id": "ZJh67gbEko_T" 346 | }, 347 | "source": [ 348 | "tuner = kt.Hyperband(build_model,\n", 349 | " objective='val_accuracy',\n", 350 | " max_epochs=10,\n", 351 | " factor=3,\n", 352 | " directory='my_dir',\n", 353 | " project_name='intro_to_kt')" 354 | ], 355 | "execution_count": null, 356 | "outputs": [] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "metadata": { 361 | "id": "febib-xqtOp6" 362 | }, 363 | "source": [ 364 | "!ls -lrt ./my_dir/intro_to_kt " 365 | ], 366 | "execution_count": null, 367 | "outputs": [] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "metadata": { 372 | "id": "bsfM031Fp0GO" 373 | }, 374 | "source": [ 375 | "early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5)" 376 | ], 377 | "execution_count": null, 378 | "outputs": [] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "metadata": { 383 | "id": "Q5esVKF9auYM" 384 | }, 385 | "source": [ 386 | "import time\n", 387 | "import datetime\n", 388 | "\n", 389 | "ts = time.time()\n", 390 | "print(ts) # finish epoch time in second \n", 391 | "datetime_time = datetime.datetime.fromtimestamp(ts)\n", 392 | "print(datetime_time)" 393 | ], 394 | "execution_count": null, 395 | "outputs": [] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "metadata": { 400 | "id": "cCntSPNbrLDh" 401 | }, 402 | "source": [ 403 | "tuner.search(train_dataset, \n", 404 | " steps_per_epoch = STEPS_PER_EPOCH,\n", 405 | " validation_data = validation_dataset,\n", 406 | " validation_steps = VALIDATION_STEPS,\n", 407 | " epochs = 15,\n", 408 | " callbacks = [early_stop]\n", 409 | " )\n", 410 | "\n", 411 | "# Get the optimal hyperparameters\n", 412 | "best_hps=tuner.get_best_hyperparameters(num_trials=1)[0]\n", 413 | "\n", 414 | "print(f\"\"\"\n", 415 | "The hyperparameter search is complete. The optimal number of units in conv_1\n", 416 | "layer is {best_hps.get('units')} and the optimal learning rate for the optimizer\n", 417 | "is {best_hps.get('learning_rate')} and the optimal activation for dense_1 layer\n", 418 | "is {best_hps.get('dense_activation')}.\n", 419 | "\"\"\")" 420 | ], 421 | "execution_count": null, 422 | "outputs": [] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "metadata": { 427 | "id": "a27h6azXs1Uj" 428 | }, 429 | "source": [ 430 | "ts = time.time()\n", 431 | "print(ts) # finish epoch time in second \n", 432 | "datetime_time = datetime.datetime.fromtimestamp(ts)\n", 433 | "print(datetime_time)" 434 | ], 435 | "execution_count": null, 436 | "outputs": [] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "metadata": { 441 | "id": "_fJU2HrZtmVm" 442 | }, 443 | "source": [ 444 | "#Started in 5:28 pm CDT. 1617402511" 445 | ], 446 | "execution_count": null, 447 | "outputs": [] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "metadata": { 452 | "id": "RO9RoOUxvfgB" 453 | }, 454 | "source": [ 455 | "1617403117 - 1617402511\n", 456 | "# 10 minutes running time." 457 | ], 458 | "execution_count": null, 459 | "outputs": [] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "metadata": { 464 | "id": "CpRHTNS9viav" 465 | }, 466 | "source": [ 467 | "best_hps.get('units')" 468 | ], 469 | "execution_count": null, 470 | "outputs": [] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "metadata": { 475 | "id": "1otzROItv2Wc" 476 | }, 477 | "source": [ 478 | "best_hps.get('learning_rate')" 479 | ], 480 | "execution_count": null, 481 | "outputs": [] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "metadata": { 486 | "id": "78OapOudv7eq" 487 | }, 488 | "source": [ 489 | "best_hps.get('dense_activation')" 490 | ], 491 | "execution_count": null, 492 | "outputs": [] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "id": "Zq-aGFWyexqi" 498 | }, 499 | "source": [ 500 | "## Launch full training with best hyperparameters" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "metadata": { 506 | "id": "y7dY-Y5Pv9wN" 507 | }, 508 | "source": [ 509 | "best_hp_model = tuner.hypermodel.build(best_hps)" 510 | ], 511 | "execution_count": null, 512 | "outputs": [] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "metadata": { 517 | "id": "-bmLjw8NfZRn" 518 | }, 519 | "source": [ 520 | "MODEL_NAME = 'myCIFAR10-{}'.format(datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n", 521 | "print(MODEL_NAME)\n", 522 | "checkpoint_dir = './' + MODEL_NAME\n", 523 | "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt-{epoch}\")\n", 524 | "print(checkpoint_prefix)" 525 | ], 526 | "execution_count": null, 527 | "outputs": [] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "metadata": { 532 | "id": "Rx85JT6sfU_F" 533 | }, 534 | "source": [ 535 | "myCheckPoint = tf.keras.callbacks.ModelCheckpoint(\n", 536 | " filepath=checkpoint_prefix,\n", 537 | " monitor='val_accuracy',\n", 538 | " mode='max',\n", 539 | " save_weights_only = True,\n", 540 | " save_best_only = True\n", 541 | " )" 542 | ], 543 | "execution_count": null, 544 | "outputs": [] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "metadata": { 549 | "id": "rxPFeNIZfBl4" 550 | }, 551 | "source": [ 552 | "best_hp_model.fit(train_dataset, \n", 553 | " steps_per_epoch = STEPS_PER_EPOCH,\n", 554 | " validation_data = validation_dataset,\n", 555 | " validation_steps = VALIDATION_STEPS,\n", 556 | " epochs = 15,\n", 557 | " callbacks = [early_stop, myCheckPoint])" 558 | ], 559 | "execution_count": null, 560 | "outputs": [] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "metadata": { 565 | "id": "Rx1UtBQUfvp3" 566 | }, 567 | "source": [ 568 | "tf.train.latest_checkpoint(checkpoint_dir)" 569 | ], 570 | "execution_count": null, 571 | "outputs": [] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "metadata": { 576 | "id": "PJ8zc-HfqYhn" 577 | }, 578 | "source": [ 579 | "best_hp_model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))" 580 | ], 581 | "execution_count": null, 582 | "outputs": [] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "metadata": { 587 | "id": "btkmxJSJqgF_" 588 | }, 589 | "source": [ 590 | "" 591 | ], 592 | "execution_count": null, 593 | "outputs": [] 594 | } 595 | ] 596 | } -------------------------------------------------------------------------------- /chapter10/README.md: -------------------------------------------------------------------------------- 1 | # tensorflow-pocket-ref 2 | 3 | This is the code repo for the book TensorFlow 2 Pocket Reference by K.C. Tung --------------------------------------------------------------------------------