TensorFlow For Dummies Cheat Sheet
TensorFlow is Google’s premier framework for machine learning, and each new version brings a wide range of capabilities and features. After you’ve ascended the learning curve, you can write sophisticated machine-learning applications and execute them at high speed.
But rising up the learning curve isn’t easy — with great power comes great complexity. To help you in your climb, you need to be aware of TensorFlow’s data types, the TensorBoard utility, and the deployment of applications to Google’s Machine Learning Engine.
Fundamental Data Types in TensorFlow
You can write TensorFlow applications in a number of different languages, such as Python, C++, and Java. But no matter which language you use, you need to be familiar with a series of TensorFlow-specific data types:
- Tensors and placeholders: A tensor is an instance of the
Tensorclass, and it serves as a general-purpose multidimensional array. A placeholder is also a
Tensor, but instead of being initialized in code, it receives data from a session that will be valid during one execution of the session. Placeholders make it possible to update a tensor’s content from one session execution to the next.
- Graphs: A graph is a container similar to a list or a tuple. Only one graph can be active at a time, and when you code an operation that accepts tensors or variables, the tensors, variables, and operation are stored as elements in the graph. When you create an optimizer and call its
minimizemethod, TensorFlow stores the resulting operation in the graph.
- Sessions. Graphs store operations, but they can’t perform operations by themselves. Instead, you need to create a session and call its
runmethod. If you call
runwith a tensor or operation, the method will execute the operations in the graph needed to obtain the desired result.
- Optimizers. The goal of machine learning is to refine a mathematical model of a real-world system until it resembles the system as closely as possible. This refinement process is called optimization, and researchers have devised many optimization methods. TensorFlow supports many of these algorithms and provides an optimizer class for each. Regardless of the class, you can obtain an optimization operation by calling the optimizer’s
- Variables. Unlike tensors and placeholders, a variable is an instance of the
Variableclass. Its primary purpose is to contain data to be updated during the optimization process. For example, if your application models a system with a straight line, you’ll store the line’s slope and y-intercept as variables. But before you can use variables to optimize a model, you need to create and execute special initialization operations.
- Estimators. If you’d rather not deal with low-level data structures like sessions and graphs, you can execute machine learning algorithms using TensorFlow’s Estimator API. An estimator is an instance of the
Estimatorclass, and each estimator embodies a machine learning algorithm. The major advantage of using estimators is that they all have the same three methods for launching the machine learning process:
How to Use TensorBoard in TensorFlow
When you install TensorFlow, the installer also provides a command-line utility named TensorBoard. This generates plots that allow you to visualize the operation of your TensorFlow application. TensorBoard makes it easy to find errors in your programs, but it’s not easy to use. To generate data and view the plots in TensorFlow, you need to perform six steps:
- Create summary operations.
Before you can view data in TensorBoard, you need to identify the data to be displayed by creating special operations called summary operations. You can create these operations by calling functions of the
tf.summarypackage. For example, you can create a summary operation for a single value by calling
tf.summary.scalar. You can create a summary operation for a series of values by calling
tf.summary.histogram. You can combine operations together by calling a function like
- Execute summary operations.
After you create one or more summary operations, you can obtain the summary data by executing the operations in a session. As a result, the session will return a protocol buffer containing the application’s summary data.
- Create a FileWriter.
Before you can print the summary data to a file, you need to create a
tf.summary.FileWriter. This constructor accepts many arguments, but the only required argument is the name of the directory to contain the summary data.
- Print the summary data.
FileWriterclass doesn’t have a simple print method. Instead, you need to call the FileWriter’s
add_summarymethod to print summary data to a file. This method writes the event file to the directory specified in the
FileWriterconstructor. After you print the data, it’s a good idea to call the FileWriter’s
closemethod to destroy the instance.
- Launch TensorBoard.
After you install TensorFlow, the
tensorboardutility appears in the top-level scripts directory. You can launch the utility by executing the
tensorboardcommand and setting the
logdiroption to the name of the directory containing the summary data. For example, if the summary data is in the output directory, you can launch TensorBoard by executing
tensorboard –logdir=outputon a command line.
- View TensorBoard in a browser.
After you launch the TensorBoard utility, you can view its interface by opening a browser. The default URL is
http://localhost:6006, but you can configure this by setting
portoptions in the
How to Run TensorFlow in the Cloud
The best reason to use TensorFlow for machine learning is that you can run your applications in the cloud. More specifically, you can deploy TensorFlow programs to the Machine Learning (ML) Engine, which Google makes available as part of the Google Cloud Platform (GCP). This deployment process consists of seven steps:
- Create a Google Cloud Platform project.
When you work with the GCP, a project serves as the central container of configuration settings and source files. You can create a new project by visiting the Google Cloud platform, clicking Select a Project, and clicking the plus button in the Select dialog box. You can choose your project’s name, but the GCP sets the project’s ID, which is unique among all GCP projects.
- Enable access to the ML Engine.
Every new GCP project can access a number of Google’s capabilities, including the Datastore and Cloud Storage. But by default, GCP projects can’t deploy applications to the ML Engine. To enable access, open the menu in the upper-left of the project page, select APIs & Services, and then click Library. Click the link entitled Google Cloud Machine Learning Engine and then click the ENABLE button.
- Install the Cloud Software Development Kit (SDK).
You can access the GCP from a command line by installing Google’s Cloud SDK. To download this, click the appropriate link for your operating system. When the installation is complete, you’ll be able to access the SDK by running
gcloudcommands on a command line.
- Upload training/prediction data to Cloud Storage.
The ML Engine can access your training/prediction data only if you upload it to Google’s Cloud Storage. You can interact with Cloud Storage from a command line through the
gsutilutility provided by the Cloud SDK. Cloud Storage data is contained in directory-like structures called buckets, and when you upload a file to a bucket, the data structure is called an object.
- Add a setup.py module in your application’s package.
To make a Python application accessible to the ML Engine, you need to structure it as a package. Each package needs to have a
setup.pymodule in the top-level directory. This module needs to provide code for
setuptools.setup, which provides configuration information to the ML Engine.
- Launch a training job for the ML Engine.
To train your model in the cloud, you need to launch a training job by running
gcloud ml-engine jobs submit trainingwith the appropriate options. Options include
--package-path, which identifies the location of the package,
--module-name, which provides the name of the Python module, and
–job-dir, which tells the ML Engine where to store output. When training is complete, the ML Engine will produce a
SavedModelcontaining the trained results.
- Launch a prediction job for the ML Engine.
After you obtain a
SavedModel, you can use the ML Engine to perform prediction by running
gcloud ml-engine jobs submit predictionwith the appropriate options. Options include
--input-paths, which identifies the location of the project’s input files,
--data-format, which tells the ML Engine how the input data is formatted, and
--output-path, which identifies where the prediction output should be stored.