分享

TensorFlow ML cookbook 第一章1、2节 TensorFlow如何工作

levycui 2017-9-12 17:13:42 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 7532
本帖最后由 levycui 于 2017-9-12 17:15 编辑
问题导读:
1、如何理解TensorFlow工作原理?
2、使用TensorFlow应该准备哪些环境?
3、TensorFlow算法的流程有哪些?
4、TensorFlow如何运行?



上一篇:TensorFlow ML cookbook:前言

TensorFlow入门
在本章中,我们将介绍基本内容,以了解TensorFlow的工作原理,以及如何访问本书的数据和其他资源。 在本章结束,您应该了解以下内容:
  • TensorFlow如何工作
  • 声明变量和传感器
  • 使用占位符和变量
  • 使用矩阵
  • 宣布运作
  • 实现激活功能
  • 使用数据源
  • 其他资源

介绍
Google的TensorFlow引擎具有独特的解决问题的方法。 这种独特的方式使我们能够非常有效地解决机器学习问题。 机器学习在几乎所有的生活和工作领域都被使用,但是一些更着名的领域是计算机视觉,语音识别,语言翻译和医疗保健。 我们将介绍基本步骤,以了解TensorFlow如何运行,并最终在本书后面的生成代码技术中加以构建。这些基本原则很重要,便于了解本书其余部分的内容。

TensorFlow的工作原理

首先,TensorFlow中的计算可能看起来不是很复杂。 原因是:由于TensorFlow处理计算、开发复杂的算法比较容易。该内容将引导我们完成TensorFlow算法的伪代码。

准备好
目前,在Linux,Mac和Windows上都支持TensorFlow。本书的代码已经在Linux系统上创建并运行,但也可以在任何其他系统上运行。该书的代码可以在GitHub上访问https://github.com/nfmcclure/tensorflow_ cookbookTensorFlow。在本书中,我们将仅关注TensorFlow的Python库包装器,尽管TensorFlow的大部分原始核心代码都是用C ++编写的。本书将使用Python 3.4+(https://www.python.org)和TensorFlow 0.12(https://www.tensorflow.org)。 TensorFlow在官方GitHub网站上提供了一个1.0.0 alpha版本,本书中的代码已经被审查与该版本兼容。虽然TensorFlow可以在CPU上运行,但是如果在GPU上处理,大多数算法运行速度更快,并且在支持Nvidia Compute Capability v4.0 +(推荐使用v5.1)的显卡上。 TensorFlow的热门GPU是Nvidia Tesla架构和具有至少4 GB视频RAM的Pascal架构。要在GPU上运行,您还需要下载并安装Nvidia Cuda Toolkit以及v 5.x +(https://developer.nvidia.com/cuda-downloads)。一些内容将依赖于当前安装的Python软件包:Scipy,Numpy和Scikit-Learn。这些软件包也都包含在Anaconda软件包(https://www.continuum.io/downloads)中。

如何做
这里我们将介绍TensorFlow算法的一般流程。 大多数内容将遵循这个大纲:
1.导入或生成数据集:我们所有的机器学习算法都将依赖于数据集。 在本书中,我们将生成数据或使用外部数据集源。 有时最好依靠生成的数据,因为我们只想知道预期的结果。 大多数情况下,我们将访问给定食谱的公共数据集,有关访问这些信息的详细信息,请参见本章第8节。

2.转换和归一化数据:通常,输入数据集不会以TensorFlow的形式出现,因此我们需要将TensorFlow转换为接受的格式。 数据通常不在我们的算法期望的正确维度或类型中。 我们必须转换我们的数据才能使用它。 大多数算法也期望归一化数据,我们也将在这里做。 TensorFlow具有内置函数,可以为您规范数据,如下所示:
[mw_shl_code=python,true]data = tf.nn.batch_norm_with_global_normalization(...)[/mw_shl_code]

3.将分类数据集转化为训练,测试和验证集:我们一般希望在我们接受过训练的不同集上测试我们的算法。 此外,许多算法需要超参数调整,因此我们放置一个验证集来确定最佳的超参数集。

4.设置算法参数(超参数):我们的算法通常具有一组在整个过程中保持不变的参数。 例如,这可以是我们选择的迭代次数,学习率或其他固定参数。 将这些一起初始化是很好的形式,因此读者或用户可以轻松找到它们,如下所示:
[mw_shl_code=python,true]learning_rate = 0.01
batch_size = 100
iterations = 1000 [/mw_shl_code]

5.初始化变量和占位符:TensorFlow取决于知道它可以和无法修改。 TensorFlow将在优化期间修改/调整变量和权重/偏差,以最大限度地减少损失函数。 为了实现这一点,我们通过占位符来提供数据。 我们需要初始化这两个变量和占位符的大小和类型,以便TensorFlow知道会有什么期望。 TensorFlow还需要知道要预期的数据类型:对于本书的大部分,我们将使用float32。 TensorFlow还提供了float64和float16。 请注意,用于精度的字节越多,算法越慢,但使用的结果越少,精度越低。 请参阅以下代码:
[mw_shl_code=python,true]a_var = tf.constant(42)
x_input = tf.placeholder(tf.float32, [None, input_size])
y_input = tf.placeholder(tf.float32, [None, num_classes]) [/mw_shl_code]

6.定义模型结构:在我们拥有数据并初始化了变量和占位符后,我们必须定义模型。 这是通过构建计算图来完成的。 TensorFlow选择哪些操作和值必须是变量和占位符来达到我们的模型结果。 我们在第2章“TensorFlow方法”中的计算图操作中的计算图更详细地讨论了TensorFlow配方。 我们这个例子的模型将是一个线性模型:
[mw_shl_code=python,true]y_pred = tf.add(tf.mul(x_input, weight_matrix), b_matrix) [/mw_shl_code]

7.声明损失函数:定义模型后,我们必须能够评估输出。 这是我们声明损失函数的地方。 损失函数非常重要,因为它告诉我们我们的预测与实际值有多远。 不同类型的损失函数将在第2章“TensorFlow方法”中的“实施反向传播配方”中进行了详细的探讨。
[mw_shl_code=python,true]loss = tf.reduce_mean(tf.square(y_actual – y_pred))[/mw_shl_code]

8.初始化和训练模型:现在我们有了一切,我们需要创建一个图表的实例,通过占位符提供数据,并让TensorFlow更改变量以更好地预测我们的训练数据。 这是初始化计算图的一种方法:
[mw_shl_code=python,true]with tf.Session(graph=graph) as session:
...
session.run(...)
...
Note that we can also initiate our graph with:
session = tf.Session(graph=graph)
session.run(…) [/mw_shl_code]

9.评估模型:一旦我们建立并训练了模型,我们应该通过一些指定的标准对新数据模型做个评估。 我们对训练集和测试集进行评估,这些评估将使我们看到该model是否underfit或overfit。 我们将在以后的内容中解决这些问题。

10.调整超参数:大多数情况下,我们将根据模型性能返回并更改一些超参数。 然后我们用不同的超参数重复前面的步骤,并对验证集进行评估。

11.部署/预测新的成果:了解如何对新的,不可见的数据进行预测也很重要。 一旦我们接受model,我们可以使用我们所有的模型,去训练新的数据。

如何运行
在TensorFlow中,我们必须先建立数据,变量,占位符和模型,然后再告诉程序来训练和更改变量以改进预测。 TensorFlow通过计算图完成了这一点。 这些计算图是没有递归的有向图,这允许计算并行。 我们为TensorFlow创建一个损失函数以最小化。 TensorFlow通过修改计算图中的变量来实现。 Tensorflow知道如何修改变量,因为它跟踪模型中的计算,并自动计算每个变量的渐变。 因此,我们可以看到进行更改以及尝试不同的数据源有多么容易。

继续阅读
一个好的开始是浏览Tensorflow Python API部分的官方文档,网址为https://www.tensorflow.org/api_docs/python/
还有教程:https://www.tensorflow.org/tutorials/


原文:
Getting Started with TensorFlow

In this chapter, we will cover basic recipes in order to understand how TensorFlow works and how to access data for this book and additional resources. By the end of the chapter, you should have knowledge of the following:
  • How TensorFlow Works
  • Declaring Variables and Tensors
  • Using Placeholders and Variables
  • Working with Matrices
  • Declaring Operations
  • Implementing Activation Functions
  • Working with Data Sources
  • Additional Resources

Introduction
Google's TensorFlow engine has a unique way of solving problems. This unique way allows us to solve machine learning problems very efficiently. Machine learning is used in almost all areas of life and work, but some of the more famous areas are computer vision, speech recognition, language translations, and healthcare. We will cover the basic steps to understand how TensorFlow operates and eventually build up to production code techniques later in the book. These fundamentals are important in order to understand the recipes in the rest of this book.
How TensorFlow Works
At first, computation in TensorFlow may seem needlessly complicated. But there is a reason for it: because of how TensorFlow treats computation, developing more complicated algorithms is relatively easy. This recipe will guide us through the pseudocode of a TensorFlow algorithm.

Getting ready
Currently, TensorFlow is supported on Linux, Mac, and Windows. The code for this book has been created and run on a Linux system, but should run on any other system as well. The code for the book is available on GitHub at https://github.com/nfmcclure/tensorflow_ cookbookTensorFlow. Throughout this book, we will only concern ourselves with the Python library wrapper of TensorFlow, although most of the original core code for TensorFlow is written in C++. This book will use Python 3.4+ (https://www.python.org) and TensorFlow 0.12 (https://www.tensorflow.org). TensorFlow has a 1.0.0 alpha version available on the official GitHub site, and the code in this book has been reviewed to be compatible with that version as well. While TensorFlow can run on the CPU, most algorithms run faster if processed on the GPU, and it is supported on graphics cards with Nvidia Compute Capability v4.0+ (v5.1 recommended). Popular GPUs for TensorFlow are Nvidia Tesla architectures and Pascal architectures with at least 4 GB of video RAM. To run on a GPU, you will also need to download and install the Nvidia Cuda Toolkit and also v 5.x + (https://developer.nvidia.com/ cuda-downloads). Some of the recipes will rely on a current installation of the Python packages: Scipy, Numpy, and Scikit-Learn. These accompanying packages are also all included in the Anaconda package (https://www.continuum.io/downloads).

How to do it…
Here we will introduce the general flow of TensorFlow algorithms. Most recipes will follow this outline:
1.Import or generate datasets: All of our machine-learning algorithms will depend on datasets. In this book, we will either generate data or use an outside source of datasets. Sometimes it is better to rely on generated data because we will just want to know the expected outcome. Most of the time, we will access public datasets for the given recipe and the details on accessing these are given in section 8 of this chapter.

2.Transform and normalize data: Normally, input datasets do not come in the shape TensorFlow would expect so we need to transform TensorFlow them to the accepted shape. The data is usually not in the correct dimension or type that our algorithms expect. We will have to transform our data before we can use it. Most algorithms also expect normalized data and we will do this here as well. TensorFlow has built-in functions that can normalize the data for you as follows:
data = tf.nn.batch_norm_with_global_normalization(...)

3.Partition datasets into train, test, and validation sets: We generally want to test our algorithms on different sets that we have trained on. Also, many algorithms require hyperparameter tuning, so we set aside a validation set for determining the best set of hyperparameters.

4.Set algorithm parameters (hyperparameters): Our algorithms usually have a set of parameters that we hold constant throughout the procedure. For example, this can be the number of iterations, the learning rate, or other fixed parameters of our choosing. It is considered good form to initialize these together so the reader or user can easily find them, as follows:
learning_rate = 0.01
batch_size = 100
iterations = 1000

5.Initialize variables and placeholders: TensorFlow depends on knowing what it can and cannot modify. TensorFlow will modify/adjust the variables and weight/bias during optimization to minimize a loss function. To accomplish this, we feed in data through placeholders. We need to initialize both of these variables and placeholders with size and type, so that TensorFlow knows what to expect. TensorFlow also needs to know the type of data to expect: for most of this book, we will use float32. TensorFlow also provides float64 and float16. Note that the more bytes used for precision results in slower algorithms, but the less we use results in less precision. See the following code:
a_var = tf.constant(42)
x_input = tf.placeholder(tf.float32, [None, input_size])
y_input = tf.placeholder(tf.float32, [None, num_classes])

6.Define the model structure: After we have the data, and have initialized our variables and placeholders, we have to define the model. This is done by building a computational graph. TensorFlow chooses what operations and values must be the variables and placeholders to arrive at our model outcomes. We talk more in depth about computational graphs in the Operations in a Computational Graph TensorFlow recipe in Chapter 2, The TensorFlow Way. Our model for this example will be a linear model:
y_pred = tf.add(tf.mul(x_input, weight_matrix), b_matrix)

7.Declare the loss functions: After defining the model, we must be able to evaluate the output. This is where we declare the loss function. The loss function is very important as it tells us how far off our predictions are from the actual values. The different types of loss functions are explored in greater detail, in the Implementing Back Propagation recipe in Chapter 2, The TensorFlow Way:
loss = tf.reduce_mean(tf.square(y_actual – y_pred))

8.Initialize and train the model: Now that we have everything in place, we need to create an instance of our graph, feed in the data through the placeholders, and let TensorFlow change the variables to better predict our training data. Here is one way to initialize the computational graph:
with tf.Session(graph=graph) as session:
...
session.run(...)
...
Note that we can also initiate our graph with:
session = tf.Session(graph=graph)
session.run(…)

9.Evaluate the model: Once we have built and trained the model, we should evaluate the model by looking at how well it does with new data through some specified criteria. We evaluate on the train and test set and these evaluations will allow us to see if the model is underfit or overfit. We will address these in later recipes.

10.Tune hyperparameters: Most of the time, we will want to go back and change some of the hyperparamters, based on the model performance. We then repeat the previous steps with different hyperparameters and evaluate the model on the validation set.

11.Deploy/predict new outcomes: It is also important to know how to make predictions on new, unseen, data. We can do this with all of our models, once we have them trained.

How it works…
In TensorFlow, we have to set up the data, variables, placeholders, and model before we tell the program to train and change the variables to improve the predictions. TensorFlow accomplishes this through the computational graphs. These computational graphs are a directed graphs with no recursion, which allows for computational parallelism. We create a loss function for TensorFlow to minimize. TensorFlow accomplishes this by modifying the variables in the computational graph. Tensorflow knows how to modify the variables because it keeps track of the computations in the model and automatically computes the gradients for every variable. Because of this, we can see how easy it can be to make changes and try different data sources.

See also
A great place to start is to go through the official documentation of the Tensorflow Python API section at https://www.tensorflow.org/api_docs/python/
There are also tutorials available at: https://www.tensorflow.org/ tutorials/


已有(1)人评论

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条