cmc/data-science

Personal data science learning files.

clone: git clone https://gitbay.org/cmc/data-science.git

main: notebooks/TensorFlow_QuickStart.ipynb · raw

  1{
  2 "cells": [
  3  {
  4   "cell_type": "markdown",
  5   "id": "368ae4ce-f2d4-4d31-b4b6-4c95c01c472c",
  6   "metadata": {},
  7   "source": [
  8    "# TensorFlow Quickstart\n",
  9    "\n",
 10    "Getting started with neural network machine learning models in TensorFlow."
 11   ]
 12  },
 13  {
 14   "cell_type": "markdown",
 15   "id": "f97fa6a5-b5db-45ac-98f4-54a4fee7ddaf",
 16   "metadata": {},
 17   "source": [
 18    "## Set up TensorFlow"
 19   ]
 20  },
 21  {
 22   "cell_type": "code",
 23   "execution_count": 3,
 24   "id": "3ce17707-7c32-4ccf-8ef1-fbad5a78db7b",
 25   "metadata": {},
 26   "outputs": [],
 27   "source": [
 28    "# pip3 install tensorflow"
 29   ]
 30  },
 31  {
 32   "cell_type": "code",
 33   "execution_count": 4,
 34   "id": "9e0d2030-33c0-4da7-bf65-919cdb3c113c",
 35   "metadata": {},
 36   "outputs": [
 37    {
 38     "name": "stdout",
 39     "output_type": "stream",
 40     "text": [
 41      "TensorFlow version: 2.13.0\n"
 42     ]
 43    }
 44   ],
 45   "source": [
 46    "import tensorflow as tf\n",
 47    "print(\"TensorFlow version:\", tf.__version__)"
 48   ]
 49  },
 50  {
 51   "cell_type": "markdown",
 52   "id": "d23e7ecb-d531-426d-85dd-1d1d0f926439",
 53   "metadata": {},
 54   "source": [
 55    "## Load a dataset"
 56   ]
 57  },
 58  {
 59   "cell_type": "code",
 60   "execution_count": 6,
 61   "id": "d04bb60f-346b-44f5-bae8-16bb1cc60f87",
 62   "metadata": {},
 63   "outputs": [],
 64   "source": [
 65    "# Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255.\n",
 66    "# Scale these values to a range of 0 to 1 by dividing the values by 255.0.\n",
 67    "# This also converts the sample data from integers to floating-point numbers:\n",
 68    "mnist = tf.keras.datasets.mnist\n",
 69    "\n",
 70    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
 71    "x_train, x_test = x_train / 255.0, x_test / 255.0"
 72   ]
 73  },
 74  {
 75   "cell_type": "code",
 76   "execution_count": 19,
 77   "id": "7895b2b4-3666-4a00-9536-8cdf4c4357da",
 78   "metadata": {},
 79   "outputs": [
 80    {
 81     "name": "stdout",
 82     "output_type": "stream",
 83     "text": [
 84      "((array([[[0, 0, 0, ..., 0, 0, 0],\n",
 85      "        [0, 0, 0, ..., 0, 0, 0],\n",
 86      "        [0, 0, 0, ..., 0, 0, 0],\n",
 87      "        ...,\n",
 88      "        [0, 0, 0, ..., 0, 0, 0],\n",
 89      "        [0, 0, 0, ..., 0, 0, 0],\n",
 90      "        [0, 0, 0, ..., 0, 0, 0]],\n",
 91      "\n",
 92      "       [[0, 0, 0, ..., 0, 0, 0],\n",
 93      "        [0, 0, 0, ..., 0, 0, 0],\n",
 94      "        [0, 0, 0, ..., 0, 0, 0],\n",
 95      "        ...,\n",
 96      "        [0, 0, 0, ..., 0, 0, 0],\n",
 97      "        [0, 0, 0, ..., 0, 0, 0],\n",
 98      "        [0, 0, 0, ..., 0, 0, 0]],\n",
 99      "\n",
100      "       [[0, 0, 0, ..., 0, 0, 0],\n",
101      "        [0, 0, 0, ..., 0, 0, 0],\n",
102      "        [0, 0, 0, ..., 0, 0, 0],\n",
103      "        ...,\n",
104      "        [0, 0, 0, ..., 0, 0, 0],\n",
105      "        [0, 0, 0, ..., 0, 0, 0],\n",
106      "        [0, 0, 0, ..., 0, 0, 0]],\n",
107      "\n",
108      "       ...,\n",
109      "\n",
110      "       [[0, 0, 0, ..., 0, 0, 0],\n",
111      "        [0, 0, 0, ..., 0, 0, 0],\n",
112      "        [0, 0, 0, ..., 0, 0, 0],\n",
113      "        ...,\n",
114      "        [0, 0, 0, ..., 0, 0, 0],\n",
115      "        [0, 0, 0, ..., 0, 0, 0],\n",
116      "        [0, 0, 0, ..., 0, 0, 0]],\n",
117      "\n",
118      "       [[0, 0, 0, ..., 0, 0, 0],\n",
119      "        [0, 0, 0, ..., 0, 0, 0],\n",
120      "        [0, 0, 0, ..., 0, 0, 0],\n",
121      "        ...,\n",
122      "        [0, 0, 0, ..., 0, 0, 0],\n",
123      "        [0, 0, 0, ..., 0, 0, 0],\n",
124      "        [0, 0, 0, ..., 0, 0, 0]],\n",
125      "\n",
126      "       [[0, 0, 0, ..., 0, 0, 0],\n",
127      "        [0, 0, 0, ..., 0, 0, 0],\n",
128      "        [0, 0, 0, ..., 0, 0, 0],\n",
129      "        ...,\n",
130      "        [0, 0, 0, ..., 0, 0, 0],\n",
131      "        [0, 0, 0, ..., 0, 0, 0],\n",
132      "        [0, 0, 0, ..., 0, 0, 0]]], dtype=uint8), array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)), (array([[[0, 0, 0, ..., 0, 0, 0],\n",
133      "        [0, 0, 0, ..., 0, 0, 0],\n",
134      "        [0, 0, 0, ..., 0, 0, 0],\n",
135      "        ...,\n",
136      "        [0, 0, 0, ..., 0, 0, 0],\n",
137      "        [0, 0, 0, ..., 0, 0, 0],\n",
138      "        [0, 0, 0, ..., 0, 0, 0]],\n",
139      "\n",
140      "       [[0, 0, 0, ..., 0, 0, 0],\n",
141      "        [0, 0, 0, ..., 0, 0, 0],\n",
142      "        [0, 0, 0, ..., 0, 0, 0],\n",
143      "        ...,\n",
144      "        [0, 0, 0, ..., 0, 0, 0],\n",
145      "        [0, 0, 0, ..., 0, 0, 0],\n",
146      "        [0, 0, 0, ..., 0, 0, 0]],\n",
147      "\n",
148      "       [[0, 0, 0, ..., 0, 0, 0],\n",
149      "        [0, 0, 0, ..., 0, 0, 0],\n",
150      "        [0, 0, 0, ..., 0, 0, 0],\n",
151      "        ...,\n",
152      "        [0, 0, 0, ..., 0, 0, 0],\n",
153      "        [0, 0, 0, ..., 0, 0, 0],\n",
154      "        [0, 0, 0, ..., 0, 0, 0]],\n",
155      "\n",
156      "       ...,\n",
157      "\n",
158      "       [[0, 0, 0, ..., 0, 0, 0],\n",
159      "        [0, 0, 0, ..., 0, 0, 0],\n",
160      "        [0, 0, 0, ..., 0, 0, 0],\n",
161      "        ...,\n",
162      "        [0, 0, 0, ..., 0, 0, 0],\n",
163      "        [0, 0, 0, ..., 0, 0, 0],\n",
164      "        [0, 0, 0, ..., 0, 0, 0]],\n",
165      "\n",
166      "       [[0, 0, 0, ..., 0, 0, 0],\n",
167      "        [0, 0, 0, ..., 0, 0, 0],\n",
168      "        [0, 0, 0, ..., 0, 0, 0],\n",
169      "        ...,\n",
170      "        [0, 0, 0, ..., 0, 0, 0],\n",
171      "        [0, 0, 0, ..., 0, 0, 0],\n",
172      "        [0, 0, 0, ..., 0, 0, 0]],\n",
173      "\n",
174      "       [[0, 0, 0, ..., 0, 0, 0],\n",
175      "        [0, 0, 0, ..., 0, 0, 0],\n",
176      "        [0, 0, 0, ..., 0, 0, 0],\n",
177      "        ...,\n",
178      "        [0, 0, 0, ..., 0, 0, 0],\n",
179      "        [0, 0, 0, ..., 0, 0, 0],\n",
180      "        [0, 0, 0, ..., 0, 0, 0]]], dtype=uint8), array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)))\n"
181     ]
182    }
183   ],
184   "source": [
185    "# You can preview the raw data prior to training the model\n",
186    "print(mnist.load_data())"
187   ]
188  },
189  {
190   "cell_type": "markdown",
191   "id": "82f07fd0-3341-4ac7-b2cc-ddc99802896f",
192   "metadata": {},
193   "source": [
194    "## Build a machine learning model"
195   ]
196  },
197  {
198   "cell_type": "code",
199   "execution_count": 7,
200   "id": "e3903d22-f584-4305-85a7-d7e1494cf909",
201   "metadata": {},
202   "outputs": [],
203   "source": [
204    "# Build a tf.keras.Sequential model:\n",
205    "model = tf.keras.models.Sequential([\n",
206    "  tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
207    "  tf.keras.layers.Dense(128, activation='relu'),\n",
208    "  tf.keras.layers.Dropout(0.2),\n",
209    "  tf.keras.layers.Dense(10)\n",
210    "])"
211   ]
212  },
213  {
214   "cell_type": "code",
215   "execution_count": 8,
216   "id": "f4dd7df9-feb6-48b3-b332-4652812571d4",
217   "metadata": {},
218   "outputs": [
219    {
220     "data": {
221      "text/plain": [
222       "array([[ 0.28218323, -0.2626474 , -0.16938315,  0.15272117, -0.2957897 ,\n",
223       "        -0.0528494 ,  0.02909562,  0.06403146,  0.67431676, -0.35960984]],\n",
224       "      dtype=float32)"
225      ]
226     },
227     "execution_count": 8,
228     "metadata": {},
229     "output_type": "execute_result"
230    }
231   ],
232   "source": [
233    "# For each example, the model returns a vector of logits or log-odds scores, one for each class.\n",
234    "predictions = model(x_train[:1]).numpy()\n",
235    "predictions"
236   ]
237  },
238  {
239   "cell_type": "code",
240   "execution_count": 9,
241   "id": "b9a5a663-8d95-4fc5-a569-efb6362454e9",
242   "metadata": {},
243   "outputs": [
244    {
245     "data": {
246      "text/plain": [
247       "array([[0.12565382, 0.07287167, 0.07999501, 0.1103954 , 0.07049612,\n",
248       "        0.08988202, 0.0975576 , 0.1010261 , 0.18598464, 0.0661376 ]],\n",
249       "      dtype=float32)"
250      ]
251     },
252     "execution_count": 9,
253     "metadata": {},
254     "output_type": "execute_result"
255    }
256   ],
257   "source": [
258    "# The tf.nn.softmax function converts these logits to probabilities for each class: \n",
259    "tf.nn.softmax(predictions).numpy()"
260   ]
261  },
262  {
263   "cell_type": "code",
264   "execution_count": 10,
265   "id": "c11f1e4e-c6a8-4a65-a4cb-36486209797c",
266   "metadata": {},
267   "outputs": [],
268   "source": [
269    "# Define a loss function for training using losses.SparseCategoricalCrossentropy:\n",
270    "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
271   ]
272  },
273  {
274   "cell_type": "code",
275   "execution_count": 12,
276   "id": "b23213f0-7818-4c58-9672-495bc6bd240a",
277   "metadata": {},
278   "outputs": [],
279   "source": [
280    "# Configure and compile the model\n",
281    "model.compile(optimizer='adam',\n",
282    "              loss=loss_fn,\n",
283    "              metrics=['accuracy'])\n"
284   ]
285  },
286  {
287   "cell_type": "markdown",
288   "id": "74edfcf4-7b45-407f-8523-40cfab8cabc7",
289   "metadata": {},
290   "source": [
291    "## Train and evaluate your model"
292   ]
293  },
294  {
295   "cell_type": "code",
296   "execution_count": 13,
297   "id": "c0438b2f-78f5-469f-a2f9-d31198ac6411",
298   "metadata": {},
299   "outputs": [
300    {
301     "name": "stdout",
302     "output_type": "stream",
303     "text": [
304      "Epoch 1/5\n",
305      "1875/1875 [==============================] - 1s 509us/step - loss: 0.3016 - accuracy: 0.9124\n",
306      "Epoch 2/5\n",
307      "1875/1875 [==============================] - 1s 514us/step - loss: 0.1462 - accuracy: 0.9572\n",
308      "Epoch 3/5\n",
309      "1875/1875 [==============================] - 1s 505us/step - loss: 0.1087 - accuracy: 0.9663\n",
310      "Epoch 4/5\n",
311      "1875/1875 [==============================] - 1s 512us/step - loss: 0.0893 - accuracy: 0.9718\n",
312      "Epoch 5/5\n",
313      "1875/1875 [==============================] - 1s 499us/step - loss: 0.0774 - accuracy: 0.9758\n"
314     ]
315    },
316    {
317     "data": {
318      "text/plain": [
319       "<keras.src.callbacks.History at 0x2977640d0>"
320      ]
321     },
322     "execution_count": 13,
323     "metadata": {},
324     "output_type": "execute_result"
325    }
326   ],
327   "source": [
328    "# Use the Model.fit method to adjust your model parameters and minimize the loss: \n",
329    "model.fit(x_train, y_train, epochs=5)"
330   ]
331  },
332  {
333   "cell_type": "code",
334   "execution_count": 14,
335   "id": "7778bac2-ebd4-43eb-94a8-03b79152c58a",
336   "metadata": {},
337   "outputs": [
338    {
339     "name": "stdout",
340     "output_type": "stream",
341     "text": [
342      "313/313 - 0s - loss: 0.0790 - accuracy: 0.9757 - 126ms/epoch - 403us/step\n"
343     ]
344    },
345    {
346     "data": {
347      "text/plain": [
348       "[0.07904709875583649, 0.9757000207901001]"
349      ]
350     },
351     "execution_count": 14,
352     "metadata": {},
353     "output_type": "execute_result"
354    }
355   ],
356   "source": [
357    "# The Model.evaluate method checks the model's performance, usually on a validation set or test set.\n",
358    "model.evaluate(x_test,  y_test, verbose=2)"
359   ]
360  },
361  {
362   "cell_type": "code",
363   "execution_count": 15,
364   "id": "96d86df4-4f22-4d76-ac4b-720192239015",
365   "metadata": {},
366   "outputs": [
367    {
368     "data": {
369      "text/plain": [
370       "<tf.Tensor: shape=(5, 10), dtype=float32, numpy=\n",
371       "array([[2.1381442e-07, 1.2493059e-08, 4.6679975e-06, 5.0975598e-04,\n",
372       "        2.3767580e-10, 8.8744054e-07, 5.8283575e-13, 9.9947828e-01,\n",
373       "        4.8932998e-07, 5.7814891e-06],\n",
374       "       [1.6270951e-08, 3.1651885e-05, 9.9994957e-01, 1.1931742e-05,\n",
375       "        4.2942398e-15, 9.1026629e-07, 1.0364544e-06, 8.1141607e-17,\n",
376       "        4.9234400e-06, 7.9949551e-15],\n",
377       "       [1.7301611e-06, 9.9930012e-01, 5.5941098e-05, 2.8840779e-05,\n",
378       "        8.1860111e-05, 3.5271249e-05, 8.1873928e-05, 2.4437119e-04,\n",
379       "        1.6496866e-04, 5.0269696e-06],\n",
380       "       [9.9992669e-01, 4.8858471e-08, 1.1441392e-05, 2.0616257e-07,\n",
381       "        5.4289058e-07, 6.2358333e-07, 7.2935950e-06, 5.1983669e-05,\n",
382       "        3.5523688e-09, 1.0397144e-06],\n",
383       "       [4.4057975e-07, 7.7009216e-10, 7.9363446e-07, 5.2758939e-08,\n",
384       "        9.9748683e-01, 9.6599024e-08, 8.6932334e-07, 1.1146701e-05,\n",
385       "        5.3453311e-07, 2.4991999e-03]], dtype=float32)>"
386      ]
387     },
388     "execution_count": 15,
389     "metadata": {},
390     "output_type": "execute_result"
391    }
392   ],
393   "source": [
394    "# If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:\n",
395    "\n",
396    "probability_model = tf.keras.Sequential([\n",
397    "  model,\n",
398    "  tf.keras.layers.Softmax()\n",
399    "])\n",
400    "probability_model(x_test[:5])"
401   ]
402  },
403  {
404   "cell_type": "code",
405   "execution_count": null,
406   "id": "ab098ffa-ab7d-4a76-90e0-255aa0763d22",
407   "metadata": {},
408   "outputs": [],
409   "source": []
410  }
411 ],
412 "metadata": {
413  "kernelspec": {
414   "display_name": "Python 3 (ipykernel)",
415   "language": "python",
416   "name": "python3"
417  },
418  "language_info": {
419   "codemirror_mode": {
420    "name": "ipython",
421    "version": 3
422   },
423   "file_extension": ".py",
424   "mimetype": "text/x-python",
425   "name": "python",
426   "nbconvert_exporter": "python",
427   "pygments_lexer": "ipython3",
428   "version": "3.11.5"
429  }
430 },
431 "nbformat": 4,
432 "nbformat_minor": 5
433}