TensorFlow For Dummies
Book image
Explore Book Buy On Amazon
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 Tensor class, 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 minimize method, 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 run method. If you call run with 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 minimize method.
  • Variables. Unlike tensors and placeholders, a variable is an instance of the Variable class. 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 Estimator class, 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: train, evaluate, and predict.

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:

  1. 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.summary package. 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 tf.summary.merge_all.
  2. 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.
  3. Create a FileWriter.
    Before you can print the summary data to a file, you need to create a FileWriter by calling tf.summary.FileWriter. This constructor accepts many arguments, but the only required argument is the name of the directory to contain the summary data.
  4. Print the summary data.
    The FileWriter class doesn’t have a simple print method. Instead, you need to call the FileWriter’s add_summary method to print summary data to a file. This method writes the event file to the directory specified in the FileWriter constructor. After you print the data, it’s a good idea to call the FileWriter’s close method to destroy the instance.
  5. Launch TensorBoard.
    After you install TensorFlow, the tensorboard utility appears in the top-level scripts directory. You can launch the utility by executing the tensorboard command and setting the logdir option 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=output on a command line.
  6. 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 host and port options in the tensorboard command.

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:

  1. 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.
  2. 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.
  3. 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 gcloud commands on a command line.
  4. 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 gsutil utility 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.
  5. 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.py module in the top-level directory. This module needs to provide code for setuptools.setup, which provides configuration information to the ML Engine.
  6. 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 training with 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 SavedModel containing the trained results.
  7. 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 prediction with 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.

About This Article

This article is from the book:

About the book author:

Matthew Scarpino has been a programmer and engineer for more than 20 years. He has worked extensively with machine learning applications, especially those involving financial analysis, cognitive modeling, and image recognition. Matthew is a Google Certified Data Engineer and blogs about TensorFlow at tfblog.com.

This article can be found in the category: