- TensorFlow 2.0 Quick Start Guide
- Tony Holdroyd
- 803字
- 2021-06-24 16:02:02
Looking at the modern TensorFlow ecosystem
Let's discuss eager execution. The first incarnation of TensorFlow involved constructing a computational graph made up of operations and tensors, which had to be subsequently evaluated in what Google termed as session (this is known as declarative programming). This is still a common way to write TensorFlow programs. However, eager execution, available from release 1.5 onward in research form and baked into TensorFlow proper from release 1.7, involves the immediate evaluation of operations, with the consequence that tensors can be treated like NumPy arrays (this is known as imperative programming).
Google says that eager execution is the preferred method for research and development but that computational graphs are to be preferred for serving TensorFlow production applications.
tf.data is an API that allows you to build complicated data input pipelines from simpler, reusable parts. The highest level abstraction is Dataset, which comprises both elements of nested structures of tensors and a plan of transformations that are to act on those elements. There are classes for the following:
- There's Dataset consisting of fixed length record sets from at least one binary file (FixedLengthRecordDataset)
- There's Dataset consisting of records from at least one TFRecord file (TFRecordDataset)
- There's Dataset consisting of records that are lines from at least one text file (TFRecordDataset)
- There is also a class that represents the state of iterating through Dataset (tf.data.Iterator)
Let's move on to the estimator, which is a high-level API that allows you to build greatly simplified machine learning programs. Estimators take care of training, evaluation, prediction, and exports for serving.
TensorFlow.js is a collection of APIs that allow you to build and train models using either the low-level JavaScript linear algebra library or the high-level layers API. Hence, models can be trained and run in a browser.
TensorFlow Lite is a lightweight version of TensorFlow for mobile and embedded devices. It consists of a runtime interpreter and a set of utilities. The idea is that you train a model on a higher-powered machine and then convert your model into the .tflite format using the utilities. You then load the model into your device of choice. At the time of writing, TensorFlow Lite is supported on Android and iOS with a C++ API and has a Java wrapper for Android. If an Android device supports the Android Neural Networks (ANN) API for hardware acceleration, then the interpreter will use this, or else it will default to the CPU for execution.
TensorFlow Hub is a library designed to foster the publication, discovery, and use of reusable modules of machine learning models. In this context, a module is a self-contained piece of a TensorFlow graph together with its weights and other assets. The module can be reused in different tasks in a method known as transfer learning. The idea is that you train a model on a large dataset and then re-purpose the appropriate module for your different but related task. This approach brings a number of advantages—you can train a model with a smaller dataset, you can improve generalization, and you can significantly speed up training.
For example, the ImageNet dataset, together with a number of different neural network architectures such as inception_v3, has been very successfully used to jump-start many other image processing training problems.
TensorFlow Extended (TFX) is a TensorFlow-based general-purpose machine learning platform. Libraries released to open source to date include TensorFlow Transform, TensorFlow Model Analysis, and TensorFlow Serving.
tf.keras is a high-level neural networks API, written in Python, that interfaces to TensorFlow (and various other tensor tools). tf.keras supports fast prototyping and is user friendly, modular, and extensible. It supports both convolutional and recurrent networks and will run on CPUs and GPUs. Keras is the API of choice for developing in TensorFlow 2.
TensorBoard is a suite of visualization tools supporting the understanding, debugging, and optimizing of TensorFlow programs. It is compatible with both eager and graph execution environments. You can use TensorBoard to visualize various metrics of your model during training.
One recent development, and at the time of writing still very much in experimental form, integrates TensorFlow directly into the Swift programming language. TensorFlow applications in Swift are written using imperative code, that is, code that executes eagerly (at runtime). The Swift compiler automatically turns this source code into one TensorFlow Graph and this compiled code then executes with the full performance of TensorFlow Sessions on CPU, GPU, and TPU.
In this book, we will focus on those TensorFlow tools that allow us to get up and running with TensorFlow, using Python 3.6 and TensorFlow 2.0.0 alpha release. In particular, we will use eager execution as opposed to computational graphs and we will leverage the power of tf.keras for building networks wherever possible, as it is the modern way for research and experiment.