Cheatsheet in TensorFlow

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 minimizemethod.
  • 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: 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.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 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 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 FileWriter constructor. After you print the data, it’s a good idea to call the FileWriter’s closemethod to destroy the instance.

5. 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.

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 tensorboardcommand.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值