Commit 5e2bb53d52
Unsigned
Layout: unified · split
.virtual_documents/notebooks/IBM Watson Visual Recognition.ipynb added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | pip install --upgrade --user "ibm-watson>=4.5.0" | |
| 5 | ||
| 6 | ||
| 7 | apikey = "<your-apikey>" | |
| 8 | version = "2018-03-19" | |
| 9 | url = "<your-url>" | |
| 10 | ||
| 11 | ||
| 12 | import json | |
| 13 | from ibm_watson import VisualRecognitionV3 | |
| 14 | from ibm_cloud_sdk_core.authenticators import IAMAuthenticator | |
| 15 | ||
| 16 | authenticator = IAMAuthenticator(apikey) | |
| 17 | visual_recognition = VisualRecognitionV3( | |
| 18 | version=version, | |
| 19 | authenticator=authenticator | |
| 20 | ) | |
| 21 | ||
| 22 | visual_recognition.set_service_url(url) | |
| 23 | ||
| 24 | ||
| 25 | visual_recognition.set_default_headers({'x-watson-learning-opt-out': "true"}) | |
| 26 | ||
| 27 | ||
| 28 | data = [ | |
| 29 | { | |
| 30 | "title": "Bear Country, South Dakota", | |
| 31 | "url": "https://example.com/photos/highres/20140717.jpg" | |
| 32 | }, | |
| 33 | { | |
| 34 | "title": "Pactola Lake", | |
| 35 | "url": "https://example.com/photos/highres/20140718.jpg" | |
| 36 | }, | |
| 37 | { | |
| 38 | "title": "Welcome to Utah", | |
| 39 | "url": "https://example.com/photos/highres/20190608_02.jpg" | |
| 40 | }, | |
| 41 | { | |
| 42 | "title": "Honey Badger", | |
| 43 | "url": "https://example.com/photos/highres/20190611_03.jpg" | |
| 44 | }, | |
| 45 | { | |
| 46 | "title": "Grand Canyon Lizard", | |
| 47 | "url": "https://example.com/photos/highres/20190612.jpg" | |
| 48 | }, | |
| 49 | { | |
| 50 | "title": "The Workhouse", | |
| 51 | "url": "https://example.com/photos/highres/20191116_01.jpg" | |
| 52 | } | |
| 53 | ] | |
| 54 | ||
| 55 | ||
| 56 | from ibm_watson import ApiException | |
| 57 | ||
| 58 | for x in range(len(data)): | |
| 59 | try: | |
| 60 | url = data[x]["url"] | |
| 61 | images_filename = data[x]["title"] | |
| 62 | classes = visual_recognition.classify( | |
| 63 | url=url, | |
| 64 | images_filename=images_filename, | |
| 65 | threshold='0.6', | |
| 66 | owners=["IBM"]).get_result() | |
| 67 | print("-------------------------------------------------------------------------------------------------------------------------------------") | |
| 68 | print("Image Title: ", data[x]["title"], "\n") | |
| 69 | print("Image URL: ", data[x]["url"], "\n") | |
| 70 | classification_results = classes["images"][0]["classifiers"][0]["classes"] | |
| 71 | for result in classification_results: | |
| 72 | print(result["class"], "(", result["score"], ")") | |
| 73 | print("-------------------------------------------------------------------------------------------------------------------------------------") | |
| 74 | except ApiException as ex: | |
| 75 | print("Method failed with status code " + str(ex.code) + ": " + ex.message) | |
.virtual_documents/notebooks/TensorFlow_QuickStart.ipynb added +82
| @@ -0,0 +1,82 @@ | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | # pip3 install tensorflow | |
| 8 | ||
| 9 | ||
| 10 | import tensorflow as tf | |
| 11 | print("TensorFlow version:", tf.__version__) | |
| 12 | ||
| 13 | ||
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | # Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. | |
| 18 | # Scale these values to a range of 0 to 1 by dividing the values by 255.0. | |
| 19 | # This also converts the sample data from integers to floating-point numbers: | |
| 20 | mnist = tf.keras.datasets.mnist | |
| 21 | ||
| 22 | (x_train, y_train), (x_test, y_test) = mnist.load_data() | |
| 23 | x_train, x_test = x_train / 255.0, x_test / 255.0 | |
| 24 | ||
| 25 | ||
| 26 | # You can preview the raw data prior to training the model | |
| 27 | print(mnist.load_data()) | |
| 28 | ||
| 29 | ||
| 30 | ||
| 31 | ||
| 32 | ||
| 33 | # Build a tf.keras.Sequential model: | |
| 34 | model = tf.keras.models.Sequential([ | |
| 35 | tf.keras.layers.Flatten(input_shape=(28, 28)), | |
| 36 | tf.keras.layers.Dense(128, activation='relu'), | |
| 37 | tf.keras.layers.Dropout(0.2), | |
| 38 | tf.keras.layers.Dense(10) | |
| 39 | ]) | |
| 40 | ||
| 41 | ||
| 42 | # For each example, the model returns a vector of logits or log-odds scores, one for each class. | |
| 43 | predictions = model(x_train[:1]).numpy() | |
| 44 | predictions | |
| 45 | ||
| 46 | ||
| 47 | # The tf.nn.softmax function converts these logits to probabilities for each class: | |
| 48 | tf.nn.softmax(predictions).numpy() | |
| 49 | ||
| 50 | ||
| 51 | # Define a loss function for training using losses.SparseCategoricalCrossentropy: | |
| 52 | loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) | |
| 53 | ||
| 54 | ||
| 55 | # Configure and compile the model | |
| 56 | model.compile(optimizer='adam', | |
| 57 | loss=loss_fn, | |
| 58 | metrics=['accuracy']) | |
| 59 | ||
| 60 | ||
| 61 | ||
| 62 | ||
| 63 | ||
| 64 | ||
| 65 | # Use the Model.fit method to adjust your model parameters and minimize the loss: | |
| 66 | model.fit(x_train, y_train, epochs=5) | |
| 67 | ||
| 68 | ||
| 69 | # The Model.evaluate method checks the model's performance, usually on a validation set or test set. | |
| 70 | model.evaluate(x_test, y_test, verbose=2) | |
| 71 | ||
| 72 | ||
| 73 | # If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it: | |
| 74 | ||
| 75 | probability_model = tf.keras.Sequential([ | |
| 76 | model, | |
| 77 | tf.keras.layers.Softmax() | |
| 78 | ]) | |
| 79 | probability_model(x_test[:5]) | |
| 80 | ||
| 81 | ||
| 82 | ||
.virtual_documents/notebooks/Untitled.ipynb added +47
| @@ -0,0 +1,47 @@ | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | # pip3 install tensorflow | |
| 5 | ||
| 6 | ||
| 7 | import tensorflow as tf | |
| 8 | print("TensorFlow version:", tf.__version__) | |
| 9 | ||
| 10 | ||
| 11 | # Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. | |
| 12 | # Scale these values to a range of 0 to 1 by dividing the values by 255.0. | |
| 13 | # This also converts the sample data from integers to floating-point numbers: | |
| 14 | mnist = tf.keras.datasets.mnist | |
| 15 | ||
| 16 | (x_train, y_train), (x_test, y_test) = mnist.load_data() | |
| 17 | x_train, x_test = x_train / 255.0, x_test / 255.0 | |
| 18 | ||
| 19 | ||
| 20 | # Build a tf.keras.Sequential model: | |
| 21 | model = tf.keras.models.Sequential([ | |
| 22 | tf.keras.layers.Flatten(input_shape=(28, 28)), | |
| 23 | tf.keras.layers.Dense(128, activation='relu'), | |
| 24 | tf.keras.layers.Dropout(0.2), | |
| 25 | tf.keras.layers.Dense(10) | |
| 26 | ]) | |
| 27 | ||
| 28 | ||
| 29 | # For each example, the model returns a vector of logits or log-odds scores, one for each class. | |
| 30 | predictions = model(x_train[:1]).numpy() | |
| 31 | predictions | |
| 32 | ||
| 33 | ||
| 34 | # The tf.nn.softmax function converts these logits to probabilities for each class: | |
| 35 | tf.nn.softmax(predictions).numpy() | |
| 36 | ||
| 37 | ||
| 38 | ||
| 39 | ||
| 40 | ||
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | ||
| 46 | ||
| 47 | ||
notebooks/.ipynb_checkpoints/IBM Watson Visual Recognition-checkpoint.ipynb added +209
| @@ -0,0 +1,209 @@ | ||
| 1 | { | |
| 2 | "cells": [ | |
| 3 | { | |
| 4 | "cell_type": "markdown", | |
| 5 | "metadata": {}, | |
| 6 | "source": [ | |
| 7 | "# IBM Watson Visual Recognition\n", | |
| 8 | "Create an account on [IBM Watson Studio](https://www.ibm.com/cloud/watson-studio) and add the [Watson Visual Recognition](https://www.ibm.com/cloud/watson-visual-recognition) service to your free account." | |
| 9 | ] | |
| 10 | }, | |
| 11 | { | |
| 12 | "cell_type": "code", | |
| 13 | "execution_count": 22, | |
| 14 | "metadata": {}, | |
| 15 | "outputs": [], | |
| 16 | "source": [ | |
| 17 | "pip install --upgrade --user \"ibm-watson>=4.5.0\"" | |
| 18 | ] | |
| 19 | }, | |
| 20 | { | |
| 21 | "cell_type": "code", | |
| 22 | "execution_count": 23, | |
| 23 | "metadata": {}, | |
| 24 | "outputs": [], | |
| 25 | "source": [ | |
| 26 | "apikey = \"<your-apikey>\"\n", | |
| 27 | "version = \"2018-03-19\"\n", | |
| 28 | "url = \"<your-url>\"" | |
| 29 | ] | |
| 30 | }, | |
| 31 | { | |
| 32 | "cell_type": "code", | |
| 33 | "execution_count": 24, | |
| 34 | "metadata": {}, | |
| 35 | "outputs": [], | |
| 36 | "source": [ | |
| 37 | "import json\n", | |
| 38 | "from ibm_watson import VisualRecognitionV3\n", | |
| 39 | "from ibm_cloud_sdk_core.authenticators import IAMAuthenticator\n", | |
| 40 | "\n", | |
| 41 | "authenticator = IAMAuthenticator(apikey)\n", | |
| 42 | "visual_recognition = VisualRecognitionV3(\n", | |
| 43 | " version=version,\n", | |
| 44 | " authenticator=authenticator\n", | |
| 45 | ")\n", | |
| 46 | "\n", | |
| 47 | "visual_recognition.set_service_url(url)" | |
| 48 | ] | |
| 49 | }, | |
| 50 | { | |
| 51 | "cell_type": "code", | |
| 52 | "execution_count": 25, | |
| 53 | "metadata": {}, | |
| 54 | "outputs": [], | |
| 55 | "source": [ | |
| 56 | "visual_recognition.set_default_headers({'x-watson-learning-opt-out': \"true\"})" | |
| 57 | ] | |
| 58 | }, | |
| 59 | { | |
| 60 | "cell_type": "code", | |
| 61 | "execution_count": 60, | |
| 62 | "metadata": {}, | |
| 63 | "outputs": [], | |
| 64 | "source": [ | |
| 65 | "data = [\n", | |
| 66 | "{\n", | |
| 67 | " \"title\": \"Bear Country, South Dakota\",\n", | |
| 68 | " \"url\": \"https://example.com/photos/highres/20140717.jpg\"\n", | |
| 69 | "},\n", | |
| 70 | "{\n", | |
| 71 | " \"title\": \"Pactola Lake\",\n", | |
| 72 | " \"url\": \"https://example.com/photos/highres/20140718.jpg\"\n", | |
| 73 | "},\n", | |
| 74 | "{\n", | |
| 75 | " \"title\": \"Welcome to Utah\",\n", | |
| 76 | " \"url\": \"https://example.com/photos/highres/20190608_02.jpg\"\n", | |
| 77 | "},\n", | |
| 78 | "{\n", | |
| 79 | " \"title\": \"Honey Badger\",\n", | |
| 80 | " \"url\": \"https://example.com/photos/highres/20190611_03.jpg\"\n", | |
| 81 | "},\n", | |
| 82 | "{\n", | |
| 83 | " \"title\": \"Grand Canyon Lizard\",\n", | |
| 84 | " \"url\": \"https://example.com/photos/highres/20190612.jpg\"\n", | |
| 85 | "},\n", | |
| 86 | "{\n", | |
| 87 | " \"title\": \"The Workhouse\",\n", | |
| 88 | " \"url\": \"https://example.com/photos/highres/20191116_01.jpg\"\n", | |
| 89 | "}\n", | |
| 90 | "]" | |
| 91 | ] | |
| 92 | }, | |
| 93 | { | |
| 94 | "cell_type": "code", | |
| 95 | "execution_count": 59, | |
| 96 | "metadata": {}, | |
| 97 | "outputs": [ | |
| 98 | { | |
| 99 | "name": "stdout", | |
| 100 | "output_type": "stream", | |
| 101 | "text": [ | |
| 102 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 103 | "Image Title: Bear Country, South Dakota \n", | |
| 104 | "\n", | |
| 105 | "brown bear ( 0.944 )\n", | |
| 106 | "bear ( 1 )\n", | |
| 107 | "carnivore ( 1 )\n", | |
| 108 | "mammal ( 1 )\n", | |
| 109 | "animal ( 1 )\n", | |
| 110 | "Alaskan brown bear ( 0.759 )\n", | |
| 111 | "greenishness color ( 0.975 )\n", | |
| 112 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 113 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 114 | "Image Title: Pactola Lake \n", | |
| 115 | "\n", | |
| 116 | "ponderosa pine ( 0.763 )\n", | |
| 117 | "pine tree ( 0.867 )\n", | |
| 118 | "tree ( 0.867 )\n", | |
| 119 | "plant ( 0.867 )\n", | |
| 120 | "blue color ( 0.959 )\n", | |
| 121 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 122 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 123 | "Image Title: Welcome to Utah \n", | |
| 124 | "\n", | |
| 125 | "signboard ( 0.953 )\n", | |
| 126 | "building ( 0.79 )\n", | |
| 127 | "blue color ( 0.822 )\n", | |
| 128 | "purplish blue color ( 0.619 )\n", | |
| 129 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 130 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 131 | "Image Title: Honey Badger \n", | |
| 132 | "\n", | |
| 133 | "American badger ( 0.689 )\n", | |
| 134 | "carnivore ( 0.689 )\n", | |
| 135 | "mammal ( 0.864 )\n", | |
| 136 | "animal ( 0.864 )\n", | |
| 137 | "armadillo ( 0.618 )\n", | |
| 138 | "light brown color ( 0.9 )\n", | |
| 139 | "reddish brown color ( 0.751 )\n", | |
| 140 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 141 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 142 | "Image Title: Grand Canyon Lizard \n", | |
| 143 | "\n", | |
| 144 | "western fence lizard ( 0.724 )\n", | |
| 145 | "lizard ( 0.93 )\n", | |
| 146 | "reptile ( 0.93 )\n", | |
| 147 | "animal ( 0.93 )\n", | |
| 148 | "ultramarine color ( 0.633 )\n", | |
| 149 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 150 | "-------------------------------------------------------------------------------------------------------------------------------------\n", | |
| 151 | "Image Title: The Workhouse \n", | |
| 152 | "\n", | |
| 153 | "castle ( 0.896 )\n", | |
| 154 | "fortification ( 0.905 )\n", | |
| 155 | "defensive structure ( 0.96 )\n", | |
| 156 | "stronghold ( 0.642 )\n", | |
| 157 | "building ( 0.799 )\n", | |
| 158 | "mound ( 0.793 )\n", | |
| 159 | "blue color ( 0.745 )\n", | |
| 160 | "-------------------------------------------------------------------------------------------------------------------------------------\n" | |
| 161 | ] | |
| 162 | } | |
| 163 | ], | |
| 164 | "source": [ | |
| 165 | "from ibm_watson import ApiException\n", | |
| 166 | "\n", | |
| 167 | "for x in range(len(data)):\n", | |
| 168 | " try:\n", | |
| 169 | " url = data[x][\"url\"]\n", | |
| 170 | " images_filename = data[x][\"title\"]\n", | |
| 171 | " classes = visual_recognition.classify(\n", | |
| 172 | " url=url,\n", | |
| 173 | " images_filename=images_filename,\n", | |
| 174 | " threshold='0.6',\n", | |
| 175 | " owners=[\"IBM\"]).get_result()\n", | |
| 176 | " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n", | |
| 177 | " print(\"Image Title: \", data[x][\"title\"], \"\\n\")\n", | |
| 178 | " print(\"Image URL: \", data[x][\"url\"], \"\\n\")\n", | |
| 179 | " classification_results = classes[\"images\"][0][\"classifiers\"][0][\"classes\"]\n", | |
| 180 | " for result in classification_results:\n", | |
| 181 | " print(result[\"class\"], \"(\", result[\"score\"], \")\")\n", | |
| 182 | " print(\"-------------------------------------------------------------------------------------------------------------------------------------\")\n", | |
| 183 | " except ApiException as ex:\n", | |
| 184 | " print(\"Method failed with status code \" + str(ex.code) + \": \" + ex.message)" | |
| 185 | ] | |
| 186 | } | |
| 187 | ], | |
| 188 | "metadata": { | |
| 189 | "kernelspec": { | |
| 190 | "display_name": "Python 3", | |
| 191 | "language": "python", | |
| 192 | "name": "python3" | |
| 193 | }, | |
| 194 | "language_info": { | |
| 195 | "codemirror_mode": { | |
| 196 | "name": "ipython", | |
| 197 | "version": 3 | |
| 198 | }, | |
| 199 | "file_extension": ".py", | |
| 200 | "mimetype": "text/x-python", | |
| 201 | "name": "python", | |
| 202 | "nbconvert_exporter": "python", | |
| 203 | "pygments_lexer": "ipython3", | |
| 204 | "version": "3.8.5" | |
| 205 | } | |
| 206 | }, | |
| 207 | "nbformat": 4, | |
| 208 | "nbformat_minor": 4 | |
| 209 | } | |
notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb added +433
| @@ -0,0 +1,433 @@ | ||
| 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 | } | |
notebooks/IBM Watson Visual Recognition.ipynb +2 −2
| @@ -187,7 +187,7 @@ | ||
| 187 | 187 | ], |
| 188 | 188 | "metadata": { |
| 189 | 189 | "kernelspec": { |
| 190 | "display_name": "Python 3", | |
| 190 | "display_name": "Python 3 (ipykernel)", | |
| 191 | 191 | "language": "python", |
| 192 | 192 | "name": "python3" |
| 193 | 193 | }, |
| @@ -201,7 +201,7 @@ | ||
| 201 | 201 | "name": "python", |
| 202 | 202 | "nbconvert_exporter": "python", |
| 203 | 203 | "pygments_lexer": "ipython3", |
| 204 | "version": "3.8.5" | |
| 204 | "version": "3.11.5" | |
| 205 | 205 | } |
| 206 | 206 | }, |
| 207 | 207 | "nbformat": 4, |
notebooks/TensorFlow_QuickStart.ipynb added +433
| @@ -0,0 +1,433 @@ | ||
| 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 | } | |