aboutsummaryrefslogtreecommitdiff
path: root/notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2023-09-18 20:53:06 -0500
committerChristian Cleberg <hello@cleberg.net>2023-09-18 20:53:06 -0500
commit5e2bb53d528d60e0a44607377fa3d09553630d5b (patch)
tree1bba9eb02c09f8c758a64d24fb9f80fc4cf24726 /notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb
parenta26d0140151902c594def7e0f6a234b973ddee0d (diff)
downloaddata-science-5e2bb53d528d60e0a44607377fa3d09553630d5b.tar.gz
data-science-5e2bb53d528d60e0a44607377fa3d09553630d5b.tar.bz2
data-science-5e2bb53d528d60e0a44607377fa3d09553630d5b.zip
add tensorflow notebook
Diffstat (limited to 'notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb')
-rw-r--r--notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb433
1 files changed, 433 insertions, 0 deletions
diff --git a/notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb
new file mode 100644
index 0000000..9323a3f
--- /dev/null
+++ b/notebooks/.ipynb_checkpoints/TensorFlow_QuickStart-checkpoint.ipynb
@@ -0,0 +1,433 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "368ae4ce-f2d4-4d31-b4b6-4c95c01c472c",
+ "metadata": {},
+ "source": [
+ "# TensorFlow Quickstart\n",
+ "\n",
+ "Getting started with neural network machine learning models in TensorFlow."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "f97fa6a5-b5db-45ac-98f4-54a4fee7ddaf",
+ "metadata": {},
+ "source": [
+ "## Set up TensorFlow"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "3ce17707-7c32-4ccf-8ef1-fbad5a78db7b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# pip3 install tensorflow"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "9e0d2030-33c0-4da7-bf65-919cdb3c113c",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "TensorFlow version: 2.13.0\n"
+ ]
+ }
+ ],
+ "source": [
+ "import tensorflow as tf\n",
+ "print(\"TensorFlow version:\", tf.__version__)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "d23e7ecb-d531-426d-85dd-1d1d0f926439",
+ "metadata": {},
+ "source": [
+ "## Load a dataset"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "d04bb60f-346b-44f5-bae8-16bb1cc60f87",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255.\n",
+ "# Scale these values to a range of 0 to 1 by dividing the values by 255.0.\n",
+ "# This also converts the sample data from integers to floating-point numbers:\n",
+ "mnist = tf.keras.datasets.mnist\n",
+ "\n",
+ "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
+ "x_train, x_test = x_train / 255.0, x_test / 255.0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "id": "7895b2b4-3666-4a00-9536-8cdf4c4357da",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "((array([[[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " ...,\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [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",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " ...,\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]],\n",
+ "\n",
+ " [[0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " ...,\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0],\n",
+ " [0, 0, 0, ..., 0, 0, 0]]], dtype=uint8), array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)))\n"
+ ]
+ }
+ ],
+ "source": [
+ "# You can preview the raw data prior to training the model\n",
+ "print(mnist.load_data())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "82f07fd0-3341-4ac7-b2cc-ddc99802896f",
+ "metadata": {},
+ "source": [
+ "## Build a machine learning model"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "e3903d22-f584-4305-85a7-d7e1494cf909",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Build a tf.keras.Sequential model:\n",
+ "model = tf.keras.models.Sequential([\n",
+ " tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
+ " tf.keras.layers.Dense(128, activation='relu'),\n",
+ " tf.keras.layers.Dropout(0.2),\n",
+ " tf.keras.layers.Dense(10)\n",
+ "])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "f4dd7df9-feb6-48b3-b332-4652812571d4",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0.28218323, -0.2626474 , -0.16938315, 0.15272117, -0.2957897 ,\n",
+ " -0.0528494 , 0.02909562, 0.06403146, 0.67431676, -0.35960984]],\n",
+ " dtype=float32)"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# For each example, the model returns a vector of logits or log-odds scores, one for each class.\n",
+ "predictions = model(x_train[:1]).numpy()\n",
+ "predictions"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "b9a5a663-8d95-4fc5-a569-efb6362454e9",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[0.12565382, 0.07287167, 0.07999501, 0.1103954 , 0.07049612,\n",
+ " 0.08988202, 0.0975576 , 0.1010261 , 0.18598464, 0.0661376 ]],\n",
+ " dtype=float32)"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# The tf.nn.softmax function converts these logits to probabilities for each class: \n",
+ "tf.nn.softmax(predictions).numpy()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "c11f1e4e-c6a8-4a65-a4cb-36486209797c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Define a loss function for training using losses.SparseCategoricalCrossentropy:\n",
+ "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "b23213f0-7818-4c58-9672-495bc6bd240a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Configure and compile the model\n",
+ "model.compile(optimizer='adam',\n",
+ " loss=loss_fn,\n",
+ " metrics=['accuracy'])\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "74edfcf4-7b45-407f-8523-40cfab8cabc7",
+ "metadata": {},
+ "source": [
+ "## Train and evaluate your model"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "id": "c0438b2f-78f5-469f-a2f9-d31198ac6411",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Epoch 1/5\n",
+ "1875/1875 [==============================] - 1s 509us/step - loss: 0.3016 - accuracy: 0.9124\n",
+ "Epoch 2/5\n",
+ "1875/1875 [==============================] - 1s 514us/step - loss: 0.1462 - accuracy: 0.9572\n",
+ "Epoch 3/5\n",
+ "1875/1875 [==============================] - 1s 505us/step - loss: 0.1087 - accuracy: 0.9663\n",
+ "Epoch 4/5\n",
+ "1875/1875 [==============================] - 1s 512us/step - loss: 0.0893 - accuracy: 0.9718\n",
+ "Epoch 5/5\n",
+ "1875/1875 [==============================] - 1s 499us/step - loss: 0.0774 - accuracy: 0.9758\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "<keras.src.callbacks.History at 0x2977640d0>"
+ ]
+ },
+ "execution_count": 13,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Use the Model.fit method to adjust your model parameters and minimize the loss: \n",
+ "model.fit(x_train, y_train, epochs=5)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "id": "7778bac2-ebd4-43eb-94a8-03b79152c58a",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "313/313 - 0s - loss: 0.0790 - accuracy: 0.9757 - 126ms/epoch - 403us/step\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "[0.07904709875583649, 0.9757000207901001]"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# The Model.evaluate method checks the model's performance, usually on a validation set or test set.\n",
+ "model.evaluate(x_test, y_test, verbose=2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "id": "96d86df4-4f22-4d76-ac4b-720192239015",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<tf.Tensor: shape=(5, 10), dtype=float32, numpy=\n",
+ "array([[2.1381442e-07, 1.2493059e-08, 4.6679975e-06, 5.0975598e-04,\n",
+ " 2.3767580e-10, 8.8744054e-07, 5.8283575e-13, 9.9947828e-01,\n",
+ " 4.8932998e-07, 5.7814891e-06],\n",
+ " [1.6270951e-08, 3.1651885e-05, 9.9994957e-01, 1.1931742e-05,\n",
+ " 4.2942398e-15, 9.1026629e-07, 1.0364544e-06, 8.1141607e-17,\n",
+ " 4.9234400e-06, 7.9949551e-15],\n",
+ " [1.7301611e-06, 9.9930012e-01, 5.5941098e-05, 2.8840779e-05,\n",
+ " 8.1860111e-05, 3.5271249e-05, 8.1873928e-05, 2.4437119e-04,\n",
+ " 1.6496866e-04, 5.0269696e-06],\n",
+ " [9.9992669e-01, 4.8858471e-08, 1.1441392e-05, 2.0616257e-07,\n",
+ " 5.4289058e-07, 6.2358333e-07, 7.2935950e-06, 5.1983669e-05,\n",
+ " 3.5523688e-09, 1.0397144e-06],\n",
+ " [4.4057975e-07, 7.7009216e-10, 7.9363446e-07, 5.2758939e-08,\n",
+ " 9.9748683e-01, 9.6599024e-08, 8.6932334e-07, 1.1146701e-05,\n",
+ " 5.3453311e-07, 2.4991999e-03]], dtype=float32)>"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:\n",
+ "\n",
+ "probability_model = tf.keras.Sequential([\n",
+ " model,\n",
+ " tf.keras.layers.Softmax()\n",
+ "])\n",
+ "probability_model(x_test[:5])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ab098ffa-ab7d-4a76-90e0-255aa0763d22",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}