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 aTensor
, 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 callrun
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
, andpredict
.
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 thetf.summary
package. For example, you can create a summary operation for a single value by callingtf.summary.scalar
. You can create a summary operation for a series of values by callingtf.summary.histogram
. You can combine operations together by calling a function liketf.summary.merge_all
. - 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 aFileWriter
by callingtf.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.
TheFileWriter
class doesn’t have a simple print method. Instead, you need to call the FileWriter’sadd_summary
method to print summary data to a file. This method writes the event file to the directory specified in theFileWriter
constructor. After you print the data, it’s a good idea to call the FileWriter’sclose
method to destroy the instance. - Launch TensorBoard.
After you install TensorFlow, thetensorboard
utility appears in the top-level scripts directory. You can launch the utility by executing thetensorboard
command and setting thelogdir
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 executingtensorboard –logdir=output
on 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 ishttp://localhost:6006
, but you can configure this by settinghost
andport
options in thetensorboard
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:
- 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 runninggcloud
commands 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 thegsutil
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. - 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 asetup.py
module in the top-level directory. This module needs to provide code forsetuptools.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 runninggcloud 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 aSavedModel
containing the trained results. - Launch a prediction job for the ML Engine.
After you obtain aSavedModel
, you can use the ML Engine to perform prediction by runninggcloud 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.
Be the first to comment