This brief introduction uses Keras to:
- Load a precompiled dataset
- Create a neural network machine learning model that classifies the images
- Train this neural network
- Evaluate the accuracy of the model.
.
.
.
This tutorial is a Google Colaboratory notebook. Python programs run directly in the browser, a great way to learn and use TensorFlow. To follow this tutorial, run Notepad in Google Colab by clicking the button at the top of this page.
In
- Colab, connect to a Python runtime: At the top right of the menu bar,
- To run all notebook code, select Runtime > Run All. To run the code cells one at a time, hover over each cell and select the Run Cell icon.
select CONNECT.
Set up TensorFlow Import TensorFlow
into your program to get started
: import tensorflow as tf print(“
TensorFlow
version:”, tf.__version__) 2023-01-05 02:22:11.570663: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Failed to load dynamic library ‘libnvinfer.so.7’; dlerror: libnvinfer.so.7: Unable to open File: shared object No such file or directory 2023-01-05 02:22:11.570853: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Failed to load dynamic library ‘libnvinfer_plugin.so.7’; dlerror: libnvinfer_plugin.so.7: Unable to open File: shared object No such file or directory 2023-01-05 02:22:11.570868: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Some TensorRT libraries cannot be opened. If you want to use the Nvidia GPU with TensorRT, make sure the missing libraries mentioned above are installed correctly. TensorFlow&colon version; 2.11.0 If you are following in your own development environment,
rather than Colab, refer to the installation guide to configure
TensorFlow for development. note: Make sure you have upgraded to the latest pip to install the TensorFlow 2 package if you are using your own development environment. See the installation guide for more information.
Load a
dataset
Upload and prepare the MNIST dataset. Pixel values for images range from 0 to 255. Scale these values to a range of 0 to 1 by dividing the values by 255.0. This also converts the sample data from integers to floating-point numbers:
mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 Download data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11490434
/11490434 [
====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation=’relu’), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10) ])
Sequential is useful for stacking layers where each layer has an input tensor and an output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are made up of layers. This model uses the Aflatar, Dense, and Abandon layers.
For each example, the model returns a vector of logits or log-odds scores, one for each class.
predictions = model(x_train[:1]).numpy() predictions array([[-0.07360977, -0.29190028, 0.0.29190028, 0.6083694 , -0.0.0.0.00977, -0.29190028, 0.6083694 , -0.0.0.0.0083694 , -0.0.0.07360977, -0.29190028, 0.6083694 , -0.0.6083694 , -0.0.0.6083694 , -0.0.0.0.08 18077262, 0.19444077, 0.27551615, 1.0136158 , -0.18320227, -0.3634336 , 0.09294549]], dtype=float32)
The tf.nn.softmax function converts these logits into probabilities for each class: tf.nn.softmax
(predictions).numpy() array([[0.07577389, 0.06091401, 0.14986472, 0.0680737 , 0.09906778, 0.10743432, 0.22474791, 0.0679085 , 0.05670884, 0.0895063 ]], dtype=float32) Note: You can bake the tf.nn.softmax function into the activation function for the last layer of the network. While this may make the model output more directly interpretable, this approach is discouraged, as it is impossible to provide an accurate and numerically stable loss calculation for all models when using a softmax output.
Define a loss function for loss training.
SparseCategoricalCrossentropy: loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) The loss function takes a vector
of terrain truth values and a vector of logits and returns a scalar loss for each example. This loss is equal to the negative logarithmic probability of the true class: the loss is zero if the model is sure of the correct class.
This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to -tf.math.log(1/10) ~= 2.3.
loss_fn(y_train[:1], predictions).numpy() 2.2308755
Before you begin training, configure and build the model using Keras Model.compile. Set the optimizer class to adam, set the loss to the loss_fn function you defined earlier, and specify a metric to evaluate for the model by setting the metrics parameter to accuracy.
model.compile(optimizer=’adam’, loss=loss_fn, metrics=[‘accuracy’])
Train and evaluate
the model
Use the Model.fit method to adjust model parameters and minimize loss: model.fit
(x_train, y_train, epochs=5) Epoch 1/5 1875/1875 [=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================9156 Epoch 2/5 1875/1875 [=========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================1423 – precision: 0.9579 Epoch 3/5 1875/1875 [====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== 0.9682 Epoch 4/5 1875/1875 [============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================0864 – precision: 0.9729 Epoch 5/5 1875/1875 [================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================== 0.0737 – precision&two points; 0.9765 <keras.callbacks.History in 0x7f2330b2eb50>
The Model.evaluate method checks the performance of the model, typically in a validation set or in a test suite. model.evaluate
(x_test, y_test, verbose=2) 313/313 – 1s – loss: 0.0737 – precision&two points; 0.9767 – 730 ms/epoch – 2 ms/step [0.07370643317699432, 0.9767000079154968]
The image classifier is now trained for ~98% accuracy in this dataset. To learn more, read the TensorFlow tutorials.
If you want the model
to return a probability, you can adjust the trained model and attach the softmax to it
: probability_model = tf.keras.Sequential([ model, tf.keras.layers.Softmax() ]) probability_model(x_test[:5]) <tf. Tensor: shape=(5, 10), dtype=float32, numpy= array([[7.8583760e-08, 2.0315771e-07, 1.9298657e-05, 2.3320188e-04, 9.5139407e-10, 4.2845312e-07, 8.9628999e-11, 9.9970192e-01, 1.2923241e-06, 4.3568154e-05], [3.7064692e-06, 1.1.964692e-06, 1.9064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.7064692e-06, 1.28451e-06], [3.7064692e-06, 1.1.2845312e-07, 8.9628999E-11, 9.9970192E-01, 1.2923241E-06, 4.3568154E-05], [3.7064692E-06, 1.1.2845312E-07, 8.9628999E-11, 9.9970192E-01, 1.2923241E-06, 4.3568154E-05], [3.7064692E-06, 1..2923241E-06, 4.3568154E-05], [3.7064692E-06, 1.1.2845312E-07, 8.9628999E-11, 9.9970192E-01, 1.2923241E-06, 4.3568154E-05], [3.7064692E-06, 1.1.2845312E-07, 8.9628999E-11, 9.9970192E-01, 1.2923241E-06, 4.3568154E-05], [3.7064692E-06, 1.1.2923241E-06, 4.4.2923241E-06, 4.2923241E-06, 4.2923241E-06, 4.7064692E-06, 1.1.2923241E-06, 4.2923241E-06, 3568154E-05], [3.7064692E-06, 1.1.2845312E-07, 8.9628999E-11, 9.9970192E-01, 1.2923241E-06, 4.3568154E-05], [3.7064692E-06, 1.1.2923241E-06, 4.3568150523147E-04, 9.9985254E-01, 3.8119510E-05, 8.8884385E-14, 3.9520441E-07, 9.4119859E-09, 2.8896094E-13, 4.9883461E-08, 4.3276201E-13], [3.8692704E-07, 9.9827552E-01, 3.2572538E-04, 6.6360576E-06, 1.7578177E-05, 3.5310466E-05, 1.7701784E-04, 4.1463875E-04, 7.4556196E-04 , 1.6867848E-06], [9.9951339E-01, 1.4372141E-08, 3.8163405E-04, 3.4556196E-04, 3.9951339E-01, 1.4372141E-08, 3.8163405E-04, 3.8163405E-04, 3.9951348E-06], 5188049E-07, 1..687848E-06], [9.9951339E-01, 1.4372141E-08, 3.8163405E-04, 3.5188049E-07, 1..5188049E-07, 1..5188049E-07, 1..1.8163405E-07, 1.1.8188049E-07, 1..9279923E-06, 1.9152633E-06, 3.0800169E-05, 4.2913445E-07, 1.4327527E-08, 6.9521411E-05], [4.3396111E-07, 6.1635697e-12, 9.5976225e-07, 2.0294257e-09, 9.9976498e-01, 1.1044161e-08, 1.3728661e-06, 1.5635873e-06, 1.8131770e-08, 2.3059935e-04]], dtype=float32)>
Conclusion
Congratulations! You have trained a machine learning model using a prebuilt dataset using the Keras API.
For more examples of using Keras, see the tutorials. To learn more about creating models with Keras, read the guides. For more information about loading and preparing data, see the tutorials on loading image data or loading CSV data.