Tenflow基础教程15天。。。Recipe 1。。。Tensorflow概览

写在前面

Google的Tensorflow用一种独特的方式解决问题。这种独特的方式可以让我们非常便捷得解决机器学习问题。机器学习已经应用在几乎所有的工作和生活当中,但更耳熟能详的领域是计算机视觉,对话识别,语言翻译。这篇教程将从最基本步骤开始,理解Tensorflow的操作,然后自然上到生产所需的编程技术。教程将以一个recipe一个recipe的方式呈现。要理解高级的recipe,基础是非常重要。

Tensorflow是如何工作的

首先,Tensorflow的计算看起来没那么复杂。一个很重要的原因在于Tesorflow处理计算的方式,由此建立复杂的计算是相当简洁的。这个Recipe将指导我们完成TensorFlow算法的大概流程。

准备工作

目前,Tensorflow支持Lunix、Mac、Window。这片教程将在Windows上运行代码,通过在你的Windows上安装Anaconda,可以很轻松的安装Tesorflow,以及依赖的库。Tensorflow现在最新版本是1.12,2.0版本已经开始内测,本教程使用的1.10版本。
Tensorflow可以在CPU上运行,当然大多数算法在GPU上运行更快,比较流行的是,Nvidia Tesla架构和Pascal架构的显卡,与至少4G的显存。在GPU跑Tensorlow,你需要下载并安装 Nvidia Cuda Toolkit 5.0以上版本(https://developer.nvidia.com/cuda-downloads)

如何操作

下面的Tensorflow算法的概览,大多Recipe将遵循这样的方式。

  1. 插入或者生成数据集
    所有的机器学习算法都建立数据的基础上。在这篇教程中我们将生成数据,或者使用外源的数据集。
  2. 转换和归一化数据
    大多数情况下,输入的数据格式不符合Tensorflow的要求,所有我们要将数据转换成Tensorflow期望的格式。如果数据没有我们需要的维度和类型,我们必须变换数据,在输入至算法之前。在Tensorflow我们也需要相同的操作,Tensorflow有一个内置的函数用来归一化数据,如下:
    data = tf.nn.batch_norm_with_global_normalization(…)
  3. 将数据分成训练集、测试集和验证集
    通常我们要在不同于训练用的数据集,测试是算法。另外,很多算法是需要优化超参数,所以我们要设置验证集,用于获得最优的超参数。
  4. 设置算法参数
    我们的算法通常有一套在整个运算过程中保持不变的参数,例如迭代次数、学习率或者我们设置为固定的参数。一起初始化这些参数,是一种比较好的形式,因为读者或者用户可以很容易得找到他们。例如:
    learning_rate = 0.01
    batch_size = 100
    iterations = 1000
  5. 初始化Variable和placehoders
    使用Tesroflow取决于知道它能操做或不能操作什么。在优化或者最小化损失函数的过程,Tesorflow可以优化variable。要完成这个操作,我们必须将数据传入placehoder中。我们需要初始化Variable和placeholder的大小和类型,以使Tensorflow知道所期望结果的格式。在本教程中会多次提到,要让Tensoerflow知道数据类型,通常我们使用float32,Tensorflow还提供float64和float32. 注意我们使用越多的位数,算法的计算速度会越慢。例如:
    a_var = tf.constant(42)
    w = tf.Variable(tf.random_normal([None, 1]))
    x_input = tf.placeholder(tf.float32, [None, input_size])
    y_input = tf.placeholder(tf.float32, [None, num_classes])
  6. 定义模型结构
    在导入数据和初始化Variable、placeholder之后,下面要定义模型。这个是通过建立计算图来完成的。我们要把数据通过Variable和placeholder传入计算图(会在后面详细说明)
    下面是一个线性模型例子:
    y_pred = tf.add(tf.mul(x_input, weight_matrix), b_matrix)
  7. 声明损失函数
    在定义模型后,我们必须评估输出。在这一步,就需要定义损失函数。损失函数是非常重要的,用于评估预测值与实际值之间的差异。在后续会详细探讨损失函数。在Tesorflow中是这样定义损失函数的。
    loss = tf.reduce_mean(tf.square(y_actual – y_pred))
  8. 初始化和训练模型
    现在我们掌握了一切,下面需要创建计算图实例,将数据导入placeholder,让Tensorflow更新参数,以更好预测训练集。下面是初始化计算图的一种方式:
    with tf.Session(graph=graph) as session:

    session.run(…)

    也可以通过下面这种方式初始化:
    session = tf.Session(graph=graph)
    session.run(…)
  9. 评估模型
    一旦建立并训练模型,我们就应该在新数据集评估模型,并观察是否过拟合。
  10. 调试超参数
    多数情况下,基本模型表现,我们会回过头调试超参数。使用不同的超参数重复前面的步骤,并在验证集上评估模型。
  11. 部署/预测新的结果
    在新的,没有见过的数据上验证算法同样重要。我们对所有训练过的算法做相同操作。

如何起作用

我们必须先设置数据,Variable,placeholder和模型,在让程序训练和更改变量以改进预测之前。Tensorflow通过计算图来完成这些步骤。这些计算图是一个没有递归的有向图,允许计算并行。
我们创建损失函数,来获得最优化。Tesorflow通过改变计算图中的变量来完成这个步骤。Tensorflow知道如何优化变量,因为它跟踪模型中的计算过程并自动计算梯度。

同样参考

  1. 一个很好的起点是查看Tensorflow的官方文档Python API部分。
    https://www.tensorflow.org/api_docs/python/
  2. 还有一些教程在
    https://www.tensorflow.org/tutorials/
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In this book, you will learn how to efficiently use TensorFlow, Google's open source framework for deep learning. You will implement different deep learning networks such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Deep Q-learning Networks (DQNs), and Generative Adversarial Networks (GANs) with easy to follow independent recipes. Take the next step in implementing various common and not-so-common neural networks with Tensorflow 1.x About This Book Skill up and implement tricky neural networks using Google's TensorFlow 1.x An easy-to-follow guide that lets you explore reinforcement learning, GANs, autoencoders, multilayer perceptrons and more. Hands-on recipes to work with Tensorflow on desktop, mobile, and cloud environment Who This Book Is For This book is intended for data analysts, data scientists, machine learning practitioners and deep learning enthusiasts who want to perform deep learning tasks on a regular basis and are looking for a handy guide they can refer to. People who are slightly familiar with neural networks, and now want to gain expertise in working with different types of neural networks and datasets, will find this book quite useful. What You Will Learn Install TensorFlow and use it for CPU and GPU operations Implement DNNs and apply them to solve different AI-driven problems. Leverage different data sets such as MNIST, CIFAR-10, and Youtube8m with TensorFlow and learn how to access and use them in your code. Use TensorBoard to understand neural network architectures, optimize the learning process, and peek inside the neural network black box. Use different regression techniques for prediction and classification problems Build single and multilayer perceptrons in TensorFlow Implement CNN and RNN in TensorFlow, and use it to solve real-world use cases. Learn how restricted Boltzmann Machines can be used to recommend movies. Understand the implementation of Autoencoders and deep belief networks, and use them for emotion detection. Master the different reinforcement learning methods to implement game playing agents. GANs and their implementation using TensorFlow. In Detail Deep neural networks (DNNs) have achieved a lot of success in the field of computer vision, speech recognition, and natural language processing. The entire world is filled with excitement about how deep networks are revolutionizing artificial intelligence. This exciting recipe-based guide will take you from the realm of DNN theory to implementing them practically to solve the real-life problems in artificial intelligence domain. In this book, you will learn how to efficiently use TensorFlow, Google's open source framework for deep learning. You will implement different deep learning networks such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Deep Q-learnin... Read more...

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值