TensorFlow 2 Pocket Reference

Book description

This easy-to-use reference for TensorFlow 2 design patterns in Python will help you make informed decisions for various use cases. Author KC Tung addresses common topics and tasks in enterprise data science and machine learning practices rather than focusing on TensorFlow itself.

When and why would you feed training data as using NumPy or a streaming dataset? How would you set up cross-validations in the training process? How do you leverage a pretrained model using transfer learning? How do you perform hyperparameter tuning? Pick up this pocket reference and reduce the time you spend searching through options for your TensorFlow use cases.

  • Understand best practices in TensorFlow model patterns and ML workflows
  • Use code snippets as templates in building TensorFlow models and workflows
  • Save development time by integrating prebuilt models in TensorFlow Hub
  • Make informed design choices about data ingestion, training paradigms, model saving, and inferencing
  • Address common scenarios such as model design style, data ingestion workflow, model training, and tuning

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
  2. 1. Introduction to TensorFlow 2
    1. Improvements in TensorFlow 2
      1. Keras API
      2. Reusable Models in TensorFlow
    2. Making Commonly Used Operations Easy
      1. Open Source Data
      2. Working with Distributed Datasets
      3. Data Streaming
      4. Data Engineering
      5. Transfer Learning
      6. Model Styles
      7. Monitoring the Training Process
      8. Distributed Training
      9. Serving Your TensorFlow Model
      10. Improving the Training Experience
    3. Wrapping Up
  3. 2. Data Storage and Ingestion
    1. Streaming Data with Python Generators
    2. Streaming File Content with a Generator
    3. JSON Data Structures
    4. Setting Up a Pattern for Filenames
    5. Splitting a Single CSV File into Multiple CSV Files
    6. Creating a File Pattern Object Using tf.io
    7. Creating a Streaming Dataset Object
    8. Streaming a CSV Dataset
    9. Organizing Image Data
    10. Using TensorFlow Image Generator
    11. Streaming Cross-Validation Images
    12. Inspecting Resized Images
    13. Wrapping Up
  4. 3. Data Preprocessing
    1. Preparing Tabular Data for Training
      1. Marking Columns
      2. Encoding Column Interactions as Possible Features
      3. Creating a Cross-Validation Dataset
      4. Starting the Model Training Process
      5. Summary
    2. Preparing Image Data for Processing
      1. Transforming Images to a Fixed Specification
      2. Training the Model
      3. Summary
    3. Preparing Text Data for Processing
      1. Tokenizing Text
      2. Creating a Dictionary and Reverse Dictionary
    4. Wrapping Up
  5. 4. Reusable Model Elements
    1. The Basic TensorFlow Hub Workflow
    2. Image Classification by Transfer Learning
      1. Model Requirements
      2. Data Transformation and Input Processing
      3. Model Implementation with TensorFlow Hub
      4. Defining the Output
      5. Mapping Output to Plain-Text Format
      6. Evaluation: Creating a Confusion Matrix
      7. Summary
    3. Using the tf.keras.applications Module for Pretrained Models
      1. Model Implementation with tf.keras.applications
      2. Fine-Tuning Models from tf.keras.applications
    4. Wrapping Up
  6. 5. Data Pipelines for Streaming Ingestion
    1. Streaming Text Files with the text_dataset_from_directory Function
      1. Downloading Text Data and Setting Up Directories
      2. Creating the Data Pipeline
      3. Inspecting the Dataset
      4. Summary
    2. Streaming Images with a File List Using the flow_from_dataframe Method
      1. Downloading Images and Setting Up Directories
      2. Creating the Data Ingestion Pipeline
      3. Inspecting the Dataset
      4. Building and Training the tf.keras Model
    3. Streaming a NumPy Array with the from_tensor_slices Method
      1. Loading Example Data and Libraries
      2. Inspecting the NumPy Array
      3. Building the Input Pipeline for NumPy Data
    4. Wrapping Up
  7. 6. Model Creation Styles
    1. Using the Symbolic API
      1. Loading the CIFAR-10 Images
      2. Inspecting Label Distribution
      3. Inspecting Images
      4. Building a Data Pipeline
      5. Batching the Dataset for Training
      6. Building the Model
    2. Understanding Inheritance
    3. Using the Imperative API
      1. Defining a Model as a Class
    4. Choosing the API
    5. Using the Built-In Training Loop
    6. Creating and Using a Custom Training Loop
      1. Creating the Elements of the Loop
      2. Putting the Elements Together in a Custom Training Loop
    7. Wrapping Up
  8. 7. Monitoring the Training Process
    1. Callback Objects
      1. ModelCheckpoint
      2. EarlyStopping
      3. Summary
    2. TensorBoard
      1. Invoking TensorBoard by Local Jupyter Notebook
      2. Invoking TensorBoard by Local Command Terminal
      3. Invoking TensorBoard by Colab Notebook
      4. Visualizing Model Overfitting Using TensorBoard
      5. Visualizing the Learning Process Using TensorBoard
    3. Wrapping Up
  9. 8. Distributed Training
    1. Data Parallelism
      1. Asynchronous Parameter Server
      2. Synchronous Allreduce
    2. Using the Class tf.distribute.MirroredStrategy
      1. Setting Up Distributed Training
      2. Using a GPU Cluster with tf.distribute.MirroredStrategy
      3. Summary
    3. The Horovod API
      1. Code Pattern for Implementing the Horovod API
      2. Encapsulating the Model Architecture
      3. Encapsulating the Data Separation and Sharding Processes
      4. Parameter Synchronization Among Workers
      5. Model Checkpoint as a Callback
      6. Distributed Optimizer for Gradient Aggregation
      7. Distributed Training Using the Horovod API
    4. Wrapping Up
  10. 9. Serving TensorFlow Models
    1. Model Serialization
      1. Saving a Model to h5 Format
      2. Saving a Model to pb Format
      3. Selecting the Model Format
    2. TensorFlow Serving
      1. Running TensorFlow Serving with a Docker Image
    3. Wrapping Up
  11. 10. Improving the Modeling Experience: Fairness Evaluation and Hyperparameter Tuning
    1. Model Fairness
      1. Model Training and Scoring
      2. Fairness Evaluation
      3. Rendering Fairness Indicators
    2. Hyperparameter Tuning
      1. Integer Lists as Hyperparameters
      2. Item Choice as Hyperparameters
      3. Floating-Point Values as Hyperparameters
    3. End-to-End Hyperparameter Tuning
      1. Import Libraries and Load Data
    4. Wrapping Up
  12. Index

Product information

  • Title: TensorFlow 2 Pocket Reference
  • Author(s): KC Tung
  • Release date: July 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492089186