《TensorFlow Machine Learning Cookbook》--1.1

TensorFlow如何工作

首先,TensorFlow中的计算看起来不是很复杂,这是因为,TensorFlow处理计算以及对待开发复杂算法的方法相对来说比较简单.下面我们将浏览TensorFlow算法的伪代码.

准备工作

目前,TensorFlow支持Linux,Mac及Windows操作系统,本书中的代码是在Linux系统上创建和运行的,它也能在其他系统上运行.书中的代码可以在https://github.com/nfmcclure/tensorflow_cookbookTensorFlow下载.整个这本书,我们只关心TensorFlow的Python包装接口,尽管它的核心代码是用C++写的.我们使用的Python版本是3.4+,TensorFlow版本是0.12.目前(本书出版时)TensorFlow在github上已提供了1.0.0 a版本,书中的代码已重新同步以便兼容此版本.TensorFlow可以运行在CPU上,不过多数算法在GPU上运行时会更快.它支持兼容Nvidia Compute Capability V4.0+(推荐V5.1)的显卡.适用于TensorFlow的GPU有Nvidia的Tesla和Pascal架构且RAM不少于4GB的等等显卡.需要下载安装Nvidia Cuda Toolkit(版本是V5.x+,https://developer.nvidia.com/cuda-download).这些"秘诀"中的一部分内容会依赖Python库:Scipy,Numpy及Scikit-Learn.这些库当然也都可以在Anaconda包中找到(https://www.continnum.io/downloads).

如何做...


这里我们来介绍TensorFlow算法的工作流程,大多数"秘诀"都遵从以下步骤:
1.导入或者生成数据集:我们所有的机器学习算法都要依赖于数据集,本书中,我们要么自己生成数据集,要么使用外部的数据源.有时候,使用生成的数据集更好一点,因为我们只是想验证预期的结果.大多数时候,我们会访问特定的数据集,本章的第8节会详细说明如何访问.
2.转换并正则化数据:一般的输入数据在"形状"(shape)上并不适合TensorFlow,因此我们需要把它们转换成TensorFlow能够接收的形状.数据在维度或者类型上并不适配算法,就要在使用它们之前进行转换.很多算法期望的数据是正则化过的,那这也要我们做这样的处理.TensorFlow有内置函数来将数据进行正则化:

    data = tf.nn.batch_norm_with_global_normalization(...)

3.将数据集分为训练集,测试集和验证集:通常情况下,我们会用不同的数据集来测试训练好的算法;另外,不少算法需要进行参数(hyperparameters)微调,这也需要留出验证集来确定最佳的参数.

4.设置算法参数(超参数):算法一般会有一系列的参数需要在整个过程中保持不变,例如:迭代的次数,学习率,或者我们选择的其他固定的参数.把这些放在一起进行初始化是比较好的方式,这样读者或者用户很容易查看它们.例如:

    learning_rate = 0.01
    batch_size = 100
    iterations = 1000


5.初始化变量和占位符(placeholder):TensorFlow需要知道什么可以修改,什么不可以,它在进行优化使得损失函数最小化的过程中会调整修改变量,或者权重和偏置(weight/bias).而要达成这些目的,我们需要通过给占位符喂入(feed)数据,也需要用特定的大小及类型(size and tyep)来初始化变量和占位符,以便TensorFlow知道干什么.TensorFlow需要知道能接收的数据类型:本书中使用的是float32类型.它还提供了float64和float16.需要注意的是,字节多可以提高精度但会让算法运行变慢,字节少会导致精度低.看下面代码:

    a_var = tf.constant(42)
    x_input = tf.placeholder(tf.float32, [None, input_size])
    y_input = tf.placeholder(tf.float32, [None, num_classes])


6.定义模型结构:在有了数据集,并且初始化了变量和占位符后,需要定义模型.这是通过构建计算图(computational graph)来完成的.TensorFlow会选择一定类型的操作和数据来得到模型的输出.对计算图的更深层的讲解会在第2章中相关部分进行.作为例子,下面给出一个线性模型:

y_pred = tf.add(tf.multiple(x_input, weight_matrix), b_matrix) #原书中是tf.mul.TensorFlow1.2版本此API更新为tf.multiply


7.声明损失函数(loss function):定义模型后,需要能够评估输出结果.这就是我们声明损失函数的地方.损失函数很重要,它可以告诉我们预测结果和实际值之间相差多远.不同类型的损失函数的更为详细的介绍会在第2章相关部分进行:

    loss = tf.reduce_mean(tf.square(y_actual - y_pred))


8.初始化和训练模型:现在所有东西都各就各位了,我们需要创建一个图(graph)的实例,给占位符(placeholder)喂入(feed)数据,让TensorFlow更新变量的值来预测训练数据.下面给出一种初始化计算图的方式:

    with tf.Session(graph=graph) as session:
        session.run(...)

另外我们也可以用下述方式初始化图:

    session = tf.Session(graph=graph)
    session.run(...)


9.评估模型:一旦我们构建和训练了模型,就要通过查看模型在特定类型的新数据集上工作的好不好来评估它.可以在训练集和测试集上进行评估,而通过这些评估工作可以知道模型是过拟合还是欠拟合.后面的章节将会讲到这些.

10.微调参数集(hyperparameters):很多情况下,需要回过头来根据模型的表现来调整其参数.我们用不同的参数来重复前面的步骤,且在验证集上再次进行评估.

11.布署/预测新数据:了解模型在新的,没有使用过的数据上预测的如何,很重要.模型一旦训练过,我们就可以进行这项工作.

如何工作...

用TensorFlow,我们可以设置数据,变量,占位符,定义模型; 然后让程序训练,更改变量值以便提高预测性能.它是通过计算图来完成这些的,这些计算图是直接式而不是递归式的,这样就可以并行计算.我们创建损失函数来让TensorFlow最小化,它通过修改计算图中的变量来达到此目的.TensorFlow知道如何修改变量,因为它会追踪模型中的计算过程,并且自动地计算每个变量的梯度.正是因为如此,我们可以看到它很容易地更新变量且能处理不同的数据源.

也可以看...


*开始学习TensorFlow的一个好的地方其Python API的官方文档:https://www.tensorflow.org/api_docs/python/
*以下网址有可用的教程:https://www.tensorflow.org/tutorials/
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Explore machine learning concepts using the latest numerical computing library — TensorFlow — with the help of this comprehensive cookbook About This Book Your quick guide to implementing TensorFlow in your day-to-day machine learning activities Learn advanced techniques that bring more accuracy and speed to machine learning Upgrade your knowledge to the second generation of machine learning with this guide on TensorFlow Who This Book Is For This book is ideal for data scientists who are familiar with C++ or Python and perform machine learning activities on a day-to-day basis. Intermediate and advanced machine learning implementers who need a quick guide they can easily navigate will find it useful. What You Will Learn Become familiar with the basics of the TensorFlow machine learning library Get to know Linear Regression techniques with TensorFlow Learn SVMs with hands-on recipes Implement neural networks and improve predictions Apply NLP and sentiment analysis to your data Master CNN and RNN through practical recipes Take TensorFlow into production In Detail TensorFlow is an open source software library for Machine Intelligence. The independent recipes in this book will teach you how to use TensorFlow for complex data computations and will let you dig deeper and gain more insights into your data than ever before. You’ll work through recipes on training models, model evaluation, sentiment analysis, regression analysis, clustering analysis, artificial neural networks, and deep learning – each using Google’s machine learning library TensorFlow. This guide starts with the fundamentals of the TensorFlow library which includes variables, matrices, and various data sources. Moving ahead, you will get hands-on experience with Linear Regression techniques with TensorFlow. The next chapters cover important high-level concepts such as neural networks, CNN, RNN, and NLP. Once you are familiar and comfortable with the TensorFlow ecosystem, the last chapter will show you how to take it to production. Style and approach This book takes a recipe-based approach where every topic is explicated with the help of a real-world example.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值