Tensorflow 简明教程

TensorFlow - Quick Guide

TensorFlow - Introduction

TensorFlow 是 Google 团队设计的一个软件库或框架,以最简单的形式实现机器学习和深度学习概念。它将优化技术的计算代数相结合,以方便计算许多数学表达式。

TensorFlow 的官方网站如下 −

tensorflow homepage

现在让我们考虑 TensorFlow 的以下重要特性 −

  1. TensorFlow 提供了一项功能,该功能借助称为张量的多维数组轻松定义、优化和计算数学表达式。

  2. 它提供深度神经网络和机器学习技术的编程支持。

  3. TensorFlow 提供了一种高度可扩展的计算功能,能够处理各种数据集。

  4. TensorFlow 使用 GPU 计算,并自动化管理。它还包含一项独特的优化功能,可优化同一内存和所使用的数据。

TensorFlow 具有完善的文档,并包含许多机器学习库。它为这些库提供了一些重要的功能和方法。

TensorFlow 也称为“Google”产品。它包含多种机器学习和深度学习算法。TensorFlow 可以训练和运行深度神经网络,用于手写数字分类、图像识别、词嵌入和创建各种序列模型。

TensorFlow - Installation

要安装 TensorFlow,系统中必须先安装“Python”。通常认为 3.4+ 版本的 Python 最适合于开始 TensorFlow 安装。

请考虑以下步骤在 Windows 操作系统中安装 TensorFlow。

Step 1 − 验证已安装的 Python 版本。

python version installed

Step 2 − 用户可以选择任何一种机制来在系统中安装 TensorFlow。我们推荐使用“pip”和“Anaconda”。Pip 是用于执行和安装 Python 模块的命令。

在安装 TensorFlow 之前,我们需要在系统中安装 Anaconda 框架。

install anaconda

安装成功后,请通过“conda”命令在命令提示符中执行检查。命令的执行如下所示 −

conda command execution

Step 3 − 执行以下命令以初始化 TensorFlow 安装 −

conda create --name tensorflow python = 3.5
installation of tensorflow

它将下载 TensorFlow 设置所需的必要程序包。

Step 4 − 环境设置成功后,重要的是激活 TensorFlow 模块。

activate tensorflow
environmental setup

Step 5 − 在系统中使用 pip 安装“Tensorflow”。用于安装的命令如下所示 −

pip install tensorflow

并且,

pip install tensorflow-gpu
pip to install
pip to install tensorflow

安装成功后,重要的是了解 TensorFlow 的示例程序执行。

以下示例有助于我们了解 TensorFlow 中的基本程序创建“Hello World”。

hello world example

第一版程序实现的代码如下所示 −

>> activate tensorflow
>> python (activating python shell)
>> import tensorflow as tf
>> hello = tf.constant(‘Hello, Tensorflow!’)
>> sess = tf.Session()
>> print(sess.run(hello))

Understanding Artificial Intelligence

人工智能包括机器和特定计算机系统对人类智能进行模拟的过程。人工智能的示例包括学习、推理和自我纠正。人工智能的应用包括语音识别、专家系统、图像识别和机器视觉。

机器学习是人工智能的一个分支,它处理能够学习任何新数据和数据模式的系统和算法。

让我们关注以下提到的韦恩图来理解机器学习和深度学习概念。

venn diagram

机器学习包括机器学习的一部分,深度学习是机器学习的一部分。遵循机器学习概念的程序的能力是可以改善其观测数据执行。数据转换的主要动机是改进其知识,以便在未来实现更好的结果,为特定系统提供更接近所需输出的输出。机器学习包括“模式识别”,它包括在数据中识别模式的能力。

这些模式应该过训练以所需的方式显示输出。

机器学习可以用两种不同的方式进行训练−

  1. Supervised training

  2. Unsupervised training

Supervised Learning

监督式学习或监督式训练包括将训练集作为输入提供给系统的过程,其中每个示例都带有所需输出值的标签。通过最大程度减小特定损失函数(它表示输出错误相对于所需的输出系统)来执行此类型的训练。

在完成训练后,将通过训练集中的不相交示例(也称为验证集)来测量每个模型的准确性。

supervised learning

“监督式学习”的最佳示例是一堆包含附加信息的图片。在这里,用户可以训练模型来识别新图片。

Unsupervised Learning

在无监督学习或无监督训练中,包括未通过系统标记他们所属类的训练示例。系统寻找具有共同特征的数据,并根据内部知识特征对其进行更改。这种类型的学习算法基本上用于聚类问题。

“无监督学习”的最佳示例是一堆没有包含信息的图片,用户使用分类和聚类对模型进行训练。这种类型的训练算法是基于假设的,因为没有提供信息。

unsupervised learning

TensorFlow - Mathematical Foundations

在 TensorFlow 中创建基本应用程序之前,了解 TensorFlow 所需的数学概念非常重要。数学被认为是任何机器学习算法的核心。借助数学的核心概念,可以定义特定机器学习算法的解决方案。

Vector

连续或离散的一组数字被定义为向量。机器学习算法处理固定长度的向量以生成更好的输出。

机器学习算法处理多维数据,因此向量起着至关重要的作用。

vector

向量模型的图片表示如下所示 −

vector model

Scalar

标量可以定义为一维向量。标量是仅包含大小且没有方向的标量。对于标量,我们只关心大小。

标量的示例包括儿童的体重和身高参数。

Matrix

矩阵可以定义为多维数组,这些数组按行和列的格式排列。矩阵的大小由行长度和列长度定义。下图显示了任何指定矩阵的表示。

multi dimensional arrays

考虑上面提到的具有“m”行和“n”列的矩阵,矩阵表示将指定为“m*n 矩阵”,该矩阵也定义了矩阵的长度。

Mathematical Computations

在本部分,我们将了解 TensorFlow 中的不同数学计算。

Addition of matrices

如果矩阵具有相同的维数,则可以对两个或更多个矩阵进行加法。加法意味着按给定位置对每个元素进行加法。

考虑以下示例以了解矩阵加法如何工作 −

示例:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B=\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A+B=\begin{bmatrix}1+5 & 2+6 \\3+7 & 4+8 \end{bmatrix}=\begin{bmatrix}6 & 8 \\10 & 12 \end{bmatrix}

Subtraction of matrices

矩阵的减法操作方式与两个矩阵的加法类似。只要维数相等,用户就可以减去两个矩阵。

示例:A-\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B-\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A-B-\begin{bmatrix}1-5 & 2-6 \\3-7 & 4-8 \end{bmatrix}-\begin{bmatrix}-4 & -4 \\-4 & -4 \end{bmatrix}

Multiplication of matrices

对于两个矩阵 A m*n 和 B p*q 可以相乘, n 应等于 p 。结果矩阵为 −

C m*q

A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B=\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}

c_{11}=\begin{bmatrix}1 & 2 \end{bmatrix}\begin{bmatrix}5 \\7 \end{bmatrix}=1\times5+2\times7=19\:c_{12}=\begin{bmatrix}1 & 2 \end{bmatrix}\begin{bmatrix}6 \\8 \end{bmatrix}=1\times6+2\times8=22

c_{21}=\begin{bmatrix}3 & 4 \end{bmatrix}\begin{bmatrix}5 \\7 \end{bmatrix}=3\times5+4\times7=43\:c_{22}=\begin{bmatrix}3 & 4 \end{bmatrix}\begin{bmatrix}6 \\8 \end{bmatrix}=3\times6+4\times8=50

C=\begin{bmatrix}c_{11} & c_{12} \\c_{21} & c_{22} \end{bmatrix}=\begin{bmatrix}19 & 22 \\43 & 50 \end{bmatrix}

Transpose of matrix

矩阵 A 的转置,m*n 通常表示为 AT(转置)n*m;可通过将列向量转置为行向量获得。

示例:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}:然后 A^{T}\begin{bmatrix}1 & 3 \\2 & 4 \end{bmatrix}

Dot product of vectors

任何维度为 n 的向量可表示为矩阵 v = R^n*1。

v_{1}=\begin{bmatrix}v_{11} \\v_{12} \\\cdot\\\cdot\\\cdot\\v_{1n}\end{bmatrix}v_{2}=\begin{bmatrix}v_{21} \\v_{22} \\\cdot\\\cdot\\\cdot\\v_{2n}\end{bmatrix}

两个向量的点积是对应分量的乘积之和 - 相同维度上的分量,其表示形式为

v_{1}\cdot v_{2}=v_1 Tv_{2}=v_2 Tv_{1}=v_{11}v_{21}v_{12}v_{22}\cdot\cdot+v_{1n}v_{2n}=\displaystyle\sum\limits_{k=1}^n v_{1k}v_{2k}

下面提到了向量的点积示例 -

示例:v_{1}=\begin{bmatrix}1 \\2 \\3\end{bmatrix}v_{2}=\begin{bmatrix}3 \\5 \\-1\end{bmatrix}v_{1}\cdot v_{2}=v_1^Tv_{2}=1\times3+2\times5-3\times1=10

Machine Learning and Deep Learning

人工智能是近年来最热门的趋势之一。机器学习和深度学习构成人工智能。下面显示的维恩图解释了机器学习和深度学习之间的关系 −

venn diagram

Machine Learning

机器学习是让计算机按照设计和编程的算法执行操作的科学艺术。许多研究人员认为,机器学习是实现达到人类水平的 AI 的最佳途径。机器学习包括以下类型的模式

  1. Supervised learning pattern

  2. Unsupervised learning pattern

Deep Learning

深度学习是机器学习的一个子领域,其中涉及的算法受到称为人工神经网络的大脑结构和功能的启发。

当今深度学习的所有价值都是通过监督式学习或从标记数据和算法中学习而获得的。

深度学习中的每个算法都会经历相同的过程。它包括输入的非线性变换层次结构,可用于生成统计模型作为输出。

考虑定义机器学习过程的以下步骤

  1. 识别相关数据集并准备它们以进行分析。

  2. 选择要使用的算法类型

  3. 基于所使用的算法构建分析模型。

  4. 在测试数据集上对模型进行训练,根据需要对其进行修改。

  5. 运行模型生成测试分数。

Difference between Machine Learning and Deep learning

在本节中,我们将了解机器学习和深度学习之间的差异。

Amount of data

机器学习处理大量数据。它对于处理少量数据也有用。另一方面,如果数据量快速增长,深度学习将有效。下图显示了机器学习和深度学习与数据量之间如何运作 −

amount of data

Hardware Dependencies

与传统机器学习算法不同,深度学习算法被设计为高度依赖于高端机器。深度学习算法执行许多矩阵乘法运算,需要大量的硬件支持。

Feature Engineering

特征工程是将领域知识放入指定特征中以降低数据复杂性并使模式对学习算法可见的过程。

示例 − 传统机器学习模式专注于像素和其他用于特征工程过程的属性。深度学习算法专注于数据中的高级特征。它减少了为每个新问题开发新特征提取器的任务。

Problem Solving Approach

传统机器学习算法遵循标准程序来解决问题。它将问题分解为部分,解决其中每个部分并将它们组合起来以获得所需的结果。深度学习专注于端到端解决问题,而不是将问题分解为各个部分。

Execution Time

执行时间是训练算法所需的时间量。深度学习需要很多时间来训练,因为它包含许多参数,耗时比平时长。相比之下,机器学习算法所需的执行时间较少。

Interpretability

可解释性是比较机器学习和深度学习算法的主要因素。主要原因是深度学习在工业中使用之前仍然会被再三考虑。

Applications of Machine Learning and Deep Learning

在本节中,我们将了解机器学习和深度学习的不同应用。

  1. 用于面部识别和通过指纹考勤或通过车牌识别车辆的计算机视觉。

  2. 来自搜索引擎的信息检索,例如图片搜索的文本搜索。

  3. 带有指定目标识别的自动化电子邮件营销。

  4. 基于肿瘤的癌症医学诊断或任何慢性病的异常识别。

  5. 针对照片标记等应用的自然语言处理。用于解释此场景的最佳范例用于 Facebook。

  6. Online Advertising.

  1. 随着业界对数据科学和机器学习的使用趋势不断增强,对于每个组织而言,在业务中引进机器学习将变得重要。

  2. 深度学习比机器学习重要得多。深度学习被证明是表现最先进技术的最佳方法之一。

  3. 机器学习和深度学习将在研究和学术领域中证明是有益的。

Conclusion

本文概述了机器学习和深度学习,并提供了图示,展示了差异,同时重点介绍了未来趋势。许多 AI 应用程序主要利用机器学习算法来实现自助服务、提高代理生产力,并提高工作流的可靠性。机器学习和深度学习算法对许多企业和行业领导者来说是一个激动人心的前景。

TensorFlow - Basics

在本章中,我们将学习 TensorFlow 的基础知识。我们将首先了解张量的基础数据结构。

Tensor Data Structure

在 TensorFlow 语言中,张量用作基础数据结构。张量表示任意流图(称为数据流图)中的连接边缘。张量被定义为数组或列表的多维形式。

张量由以下三个参数标识:

Rank

张量中描述的维度单位称为秩。它标识张量的维度数量。张量的秩可以描述为已定义张量的阶次或 n 维。

Shape

行数和列数共同定义了张量的形状。

Type

类型描述了分配给张量元素的数据类型。

用户需要考虑以下活动来构建张量:

  1. Build an n-dimensional array

  2. Convert the n-dimensional array.

tensor data structure

Various Dimensions of TensorFlow

TensorFlow 包含多个维度。这些维度简述如下:

One dimensional Tensor

一维张量是包含同一数据类型的一组值的常规数组结构。

Declaration

>>> import numpy as np
>>> tensor_1d = np.array([1.3, 1, 4.0, 23.99])
>>> print tensor_1d

带有输出的实施在以下屏幕截图中显示:

one dimensional tensor

元素的索引与 Python 列表相同。第一个元素从索引 0 开始;要通过索引打印值,您只需要提及索引号。

>>> print tensor_1d[0]
1.3
>>> print tensor_1d[2]
4.0
declaration

Two dimensional Tensors

数组序列用于创建“二维张量”。

二维张量的创建如下所述 −

two dimensional tensors

以下是创建二维数组的完整语法 −

>>> import numpy as np
>>> tensor_2d = np.array([(1,2,3,4),(4,5,6,7),(8,9,10,11),(12,13,14,15)])
>>> print(tensor_2d)
[[ 1 2 3 4]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
>>>

可以用行号和指定为索引号的列号来追踪二维张量的特定元素。

>>> tensor_2d[3][2]
14
two dimensional tensors tracked

Tensor Handling and Manipulations

在本节中,我们将学习张量处理和操作。

首先,我们考虑以下代码 −

import tensorflow as tf
import numpy as np

matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype = 'int32')
matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype = 'int32')

print (matrix1)
print (matrix2)

matrix1 = tf.constant(matrix1)
matrix2 = tf.constant(matrix2)
matrix_product = tf.matmul(matrix1, matrix2)
matrix_sum = tf.add(matrix1,matrix2)
matrix_3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype = 'float32')
print (matrix_3)

matrix_det = tf.matrix_determinant(matrix_3)
with tf.Session() as sess:
   result1 = sess.run(matrix_product)
   result2 = sess.run(matrix_sum)
   result3 = sess.run(matrix_det)

print (result1)
print (result2)
print (result3)

Output

上述代码将生成以下输出 −

tensor handling and manipulations

Explanation

我们在上述源代码中生成了多维数组。现在,重要的是要理解我们创建了管理张量的图形和会话,并生成了适当的输出。借助图形,我们有指定张量之间数学计算的输出。

TensorFlow - Convolutional Neural Networks

在了解机器学习概念后,我们现在可以将重点转移到深度学习概念中。深度学习是机器学习的一部分内容,被认为是研究人员在近几十年中迈出的关键一步。深度学习实施的示例包括图像识别和语音识别的应用。

以下是两种重要的深度神经网络类型 -

  1. Convolutional Neural Networks

  2. Recurrent Neural Networks

在本章中,我们将重点关注 CNN,也就是卷积神经网络。

Convolutional Neural Networks

卷积神经网络设计用于通过多个数组层处理数据。此类神经网络用于图像识别或面部识别等应用。CNN 和任何其他普通神经网络的主要区别在于,CNN 将输入作为二维数组,并且直接在图像上进行操作,而不是专注于其他神经网络关注的特征提取。

CNN 的优势方法包括解决识别问题。诸如 Google 和 Facebook 等顶级公司已投资于识别项目的研发中,以更快的速度完成活动。

卷积神经网络采用了三个基本思想 -

  1. Local respective fields

  2. Convolution

  3. Pooling

让我们详细理解这些想法。

CNN 利用输入数据内存在的空间关联。神经网络的每个并发层都会连接一些输入神经元。这个特定区域被称为局部感受视野。局部感受视野专注于隐藏神经元。隐藏神经元在提到的视野内处理输入数据,而不了解超出特定边界的变化。

以下是有关生成局部感受视野的图表表示 −

convolutional neural networks

如果观察上面的表示,每个连接都会学习一个隐藏神经元的权重,该权重与从一层到另一层的移动相关。在这里,单个神经元会不时地进行移动。这个过程称为“卷积”。

从输入层到隐含特征图的连接映射被定义为“共享权重”,而包含的偏差被称为“共享偏差”。

CNN 或卷积神经网络使用池化层,这些层是紧接 CNN 声明之后放置的层。它将用户输入作为来自卷积网络的特征图,并准备一个浓缩特征图。池化层有助于创建包含前一层神经元的层。

TensorFlow Implementation of CNN

在本节中,我们将了解 TensorFlow 的 CNN 实现。要求整个网络执行和适当尺寸的步骤如下所示:

Step 1 − 包括 TensorFlow 和数据集模块的必要模块,这些模块需要计算 CNN 模型。

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

Step 2 − 声明一个名为 run_cnn() 的函数,其中包含各种参数和优化变量以及数据占位符的声明。这些优化变量将声明训练模式。

def run_cnn():
   mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
   learning_rate = 0.0001
   epochs = 10
   batch_size = 50

Step 3 − 在此步骤中,我们将声明具有输入参数的训练数据占位符 - 28 x 28 像素 = 784。这是来自 mnist.train.nextbatch() 的扁平化图像数据。

我们可以根据需要对张量进行整形。第一个值 (-1) 告诉函数根据传递给它的数据量动态整形该维度。中间两个维度设置为图像大小(即 28 x 28)。

x = tf.placeholder(tf.float32, [None, 784])
x_shaped = tf.reshape(x, [-1, 28, 28, 1])
y = tf.placeholder(tf.float32, [None, 10])

Step 4 − 现在,创建一些卷积层非常重要 −

layer1 = create_new_conv_layer(x_shaped, 1, 32, [5, 5], [2, 2], name = 'layer1')
layer2 = create_new_conv_layer(layer1, 32, 64, [5, 5], [2, 2], name = 'layer2')

Step 5 − 在具有 28 x 28 维度的两层步长 2 池化后,让我们压平输出,使其准备好进行完全连接的输出阶段,使其变为 14 x 14 或至少 7 x 7 x,y 坐标,但具有 64 个输出通道。为了使用“密集”层创建完全连接,新形状需要为 [-1, 7 x 7 x 64]。我们可以为此层设置一些权重和偏差值,然后使用 ReLU 激活。

flattened = tf.reshape(layer2, [-1, 7 * 7 * 64])

wd1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1000], stddev = 0.03), name = 'wd1')
bd1 = tf.Variable(tf.truncated_normal([1000], stddev = 0.01), name = 'bd1')

dense_layer1 = tf.matmul(flattened, wd1) + bd1
dense_layer1 = tf.nn.relu(dense_layer1)

Step 6 − 具有特定 softmax 激活的另一层,带有所需的优化器定义了准确性评估,它会设置初始化运算符。

wd2 = tf.Variable(tf.truncated_normal([1000, 10], stddev = 0.03), name = 'wd2')
bd2 = tf.Variable(tf.truncated_normal([10], stddev = 0.01), name = 'bd2')

dense_layer2 = tf.matmul(dense_layer1, wd2) + bd2
y_ = tf.nn.softmax(dense_layer2)

cross_entropy = tf.reduce_mean(
   tf.nn.softmax_cross_entropy_with_logits(logits = dense_layer2, labels = y))

optimiser = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

init_op = tf.global_variables_initializer()

Step 7 − 我们应该设置记录变量。这会添加一个摘要来存储数据的准确性。

tf.summary.scalar('accuracy', accuracy)
   merged = tf.summary.merge_all()
   writer = tf.summary.FileWriter('E:\TensorFlowProject')

   with tf.Session() as sess:
      sess.run(init_op)
      total_batch = int(len(mnist.train.labels) / batch_size)

      for epoch in range(epochs):
         avg_cost = 0
      for i in range(total_batch):
         batch_x, batch_y = mnist.train.next_batch(batch_size = batch_size)
            _, c = sess.run([optimiser, cross_entropy], feed_dict = {
            x:batch_x, y: batch_y})
            avg_cost += c / total_batch
         test_acc = sess.run(accuracy, feed_dict = {x: mnist.test.images, y:
            mnist.test.labels})
            summary = sess.run(merged, feed_dict = {x: mnist.test.images, y:
            mnist.test.labels})
         writer.add_summary(summary, epoch)

   print("\nTraining complete!")
   writer.add_graph(sess.graph)
   print(sess.run(accuracy, feed_dict = {x: mnist.test.images, y:
      mnist.test.labels}))

def create_new_conv_layer(
   input_data, num_input_channels, num_filters,filter_shape, pool_shape, name):

   conv_filt_shape = [
      filter_shape[0], filter_shape[1], num_input_channels, num_filters]

   weights = tf.Variable(
      tf.truncated_normal(conv_filt_shape, stddev = 0.03), name = name+'_W')
   bias = tf.Variable(tf.truncated_normal([num_filters]), name = name+'_b')

#Out layer defines the output
   out_layer =
      tf.nn.conv2d(input_data, weights, [1, 1, 1, 1], padding = 'SAME')

   out_layer += bias
   out_layer = tf.nn.relu(out_layer)
   ksize = [1, pool_shape[0], pool_shape[1], 1]
   strides = [1, 2, 2, 1]
   out_layer = tf.nn.max_pool(
      out_layer, ksize = ksize, strides = strides, padding = 'SAME')

   return out_layer

if __name__ == "__main__":
run_cnn()

以下是以上代码生成的输出 −

See @{tf.nn.softmax_cross_entropy_with_logits_v2}.

2018-09-19 17:22:58.802268: I
T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140]
Your CPU supports instructions that this TensorFlow binary was not compiled to
use: AVX2

2018-09-19 17:25:41.522845: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 1003520000 exceeds 10% of system memory.

2018-09-19 17:25:44.630941: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 501760000 exceeds 10% of system memory.

Epoch: 1 cost = 0.676 test accuracy: 0.940

2018-09-19 17:26:51.987554: W
T:\src\github\tensorflow\tensorflow\core\framework\allocator.cc:101] Allocation
of 1003520000 exceeds 10% of system memory.

TensorFlow - Recurrent Neural Networks

递归神经网络是一种面向深度学习的算法,它遵循顺序方法。在神经网络中,我们总是假设每个输入和输出独立于所有其他层。这些类型的神经网络被称为递归网络,因为它们以顺序方式执行数学计算。

考虑以下训练递归神经网络的步骤 −

Step 1 − 从数据集中输入一个特定示例。

Step 2 − 网络将采用一个示例,并使用随机初始化的变量计算一些计算。

Step 3 − 然后计算预期的结果。

Step 4 − 用产生的实际结果与期望值进行比较将产生一个误差。

Step 5 − 为了追踪误差,它通过变量也调整的相同路径传播。

Step 6 − 从 1 到 5 的步骤重复,直到我们确信宣告的变量以获得输出被正确定义。

Step 7 − 通过应用这些变量来获得新的看不见的输入,做出一个系统的预测。

递归神经网络的示意图方法如下所述 −

recurrent neural networks

Recurrent Neural Network Implementation with TensorFlow

在本节中,我们将学习如何用 TensorFlow 实现递归神经网络。

Step 1 − TensorFlow 包含用于实现递归神经网络模块的各种库。

#Import necessary modules
from __future__ import print_function

import tensorflow as tf
from tensorflow.contrib import rnn
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

如上所述,这些库有助于定义输入数据,这形成了递归神经网络实现的主要部分。

Step 2 − 我们的主要动机是使用循环神经网络对图像进行分类,其中我们把每行图像看成像素序列。MNIST 的图像形状具体定义为 28*28 px。现在我们将处理前面提到的每个样本的 28 个 28 步骤的序列。我们将定义输入参数以便完成序列模式。

n_input = 28 # MNIST data input with img shape 28*28
n_steps = 28
n_hidden = 128
n_classes = 10

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes]
weights = {
   'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
   'out': tf.Variable(tf.random_normal([n_classes]))
}

Step 3 − 使用 RNN 中定义的函数计算结果以获得最佳结果。此处,将每个数据形状与当前输入形状进行比较,并对结果进行计算以保持准确率。

def RNN(x, weights, biases):
   x = tf.unstack(x, n_steps, 1)

   # Define a lstm cell with tensorflow
   lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)

   # Get lstm cell output
   outputs, states = rnn.static_rnn(lstm_cell, x, dtype = tf.float32)

   # Linear activation, using rnn inner loop last output
   return tf.matmul(outputs[-1], weights['out']) + biases['out']

pred = RNN(x, weights, biases)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred, labels = y))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.global_variables_initializer()

Step 4 − 在此步骤中,我们将启动图表以获取计算结果。这也将帮助计算出测试结果的准确率。

with tf.Session() as sess:
   sess.run(init)
   step = 1
   # Keep training until reach max iterations

   while step * batch_size < training_iters:
      batch_x, batch_y = mnist.train.next_batch(batch_size)
      batch_x = batch_x.reshape((batch_size, n_steps, n_input))
      sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

      if step % display_step == 0:
         # Calculate batch accuracy
         acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})

         # Calculate batch loss
         loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})

         print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
            "{:.6f}".format(loss) + ", Training Accuracy= " + \
            "{:.5f}".format(acc))
      step += 1
   print("Optimization Finished!")
      test_len = 128
   test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))

   test_label = mnist.test.labels[:test_len]
   print("Testing Accuracy:", \
      sess.run(accuracy, feed_dict={x: test_data, y: test_label}))

下面的屏幕截图给出了输出结果 −

recurrent neural networks implementation output
recurrent neural networks implementation output transflow

TensorFlow - TensorBoard Visualization

TensorFlow 包含一个名为 TensorBoard 的可视化工具。它用于分析数据流程图,还用于了解机器学习模型。TensorBoard 的重要功能包括从垂直方向上查看关于参数和任何图表的详细信息的不同类型的统计信息。

深度神经网络包括多达 36000 个节点。TensorBoard 帮助以高级块折叠这些节点并突出显示相同结构。这将允许对图进行更好的分析,重点关注计算图的主要部分。TensorBoard 可视化被认为是极具交互性的,用户可以在其中平移、缩放和展开节点以显示详细信息。

以下示意图表示展示了 TensorBoard 可视化的完整工作原理 −

tensorboard visualization

该算法将节点折叠成高级块并突出显示特定组,其中包含相同结构,从而分离出高阶节点。这样创建的 TensorBoard 是有用的,并且被认为与调整机器学习模型同样重要。此可视化工具旨在用于包含汇总信息和需要显示的详细信息的配置日志文件。

让我们借助以下代码重点关注 TensorBoard 可视化演示示例 −

import tensorflow as tf

# Constants creation for TensorBoard visualization
a = tf.constant(10,name = "a")
b = tf.constant(90,name = "b")
y = tf.Variable(a+b*2,name = 'y')
model = tf.initialize_all_variables() #Creation of model

with tf.Session() as session:
   merged = tf.merge_all_summaries()
   writer = tf.train.SummaryWriter("/tmp/tensorflowlogs",session.graph)
   session.run(model)
   print(session.run(y))

下表显示了用于节点表示的 TensorBoard 可视化的各种符号 −

node representation

TensorFlow - Word Embedding

词嵌入是将离散对象(如单词)映射到向量和实数的概念。这对机器学习的输入至关重要。此概念包括将离散输入对象有效转换为有用向量中的标准函数。

单词嵌入输入的样本说明如下所示 −

blue: (0.01359, 0.00075997, 0.24608, ..., -0.2524, 1.0048, 0.06259)
blues: (0.01396, 0.11887, -0.48963, ..., 0.033483, -0.10007, 0.1158)
orange: (-0.24776, -0.12359, 0.20986, ..., 0.079717, 0.23865, -0.014213)
oranges: (-0.35609, 0.21854, 0.080944, ..., -0.35413, 0.38511, -0.070976)

Word2vec

Word2vec 是用于无监督单词嵌入技术的最常用方法。它以给定的输入单词利用跳字序列预测单词上下文的这样一种方式对模型进行训练。

TensorFlow 通过使用多线程概念和高级抽象,以逐渐增加的精细化和优化级别实现了这种模型的多种方式。

import os
import math
import numpy as np
import tensorflow as tf

from tensorflow.contrib.tensorboard.plugins import projector
batch_size = 64
embedding_dimension = 5
negative_samples = 8
LOG_DIR = "logs/word2vec_intro"

digit_to_word_map = {
   1: "One",
   2: "Two",
   3: "Three",
   4: "Four",
   5: "Five",
   6: "Six",
   7: "Seven",
   8: "Eight",
   9: "Nine"}
sentences = []

# Create two kinds of sentences - sequences of odd and even digits.
   for i in range(10000):
   rand_odd_ints = np.random.choice(range(1, 10, 2), 3)
      sentences.append(" ".join([digit_to_word_map[r] for r in rand_odd_ints]))
   rand_even_ints = np.random.choice(range(2, 10, 2), 3)
      sentences.append(" ".join([digit_to_word_map[r] for r in rand_even_ints]))

# Map words to indices
word2index_map = {}
index = 0

for sent in sentences:
   for word in sent.lower().split():

   if word not in word2index_map:
      word2index_map[word] = index
      index += 1
index2word_map = {index: word for word, index in word2index_map.items()}

vocabulary_size = len(index2word_map)

# Generate skip-gram pairs
skip_gram_pairs = []

for sent in sentences:
   tokenized_sent = sent.lower().split()

   for i in range(1, len(tokenized_sent)-1):
      word_context_pair = [[word2index_map[tokenized_sent[i-1]],
         word2index_map[tokenized_sent[i+1]]], word2index_map[tokenized_sent[i]]]

      skip_gram_pairs.append([word_context_pair[1], word_context_pair[0][0]])
      skip_gram_pairs.append([word_context_pair[1], word_context_pair[0][1]])

def get_skipgram_batch(batch_size):
   instance_indices = list(range(len(skip_gram_pairs)))
      np.random.shuffle(instance_indices)
   batch = instance_indices[:batch_size]
   x = [skip_gram_pairs[i][0] for i in batch]
   y = [[skip_gram_pairs[i][1]] for i in batch]
   return x, y

# batch example
x_batch, y_batch = get_skipgram_batch(8)
x_batch
y_batch
[index2word_map[word] for word in x_batch] [index2word_map[word[0]] for word in y_batch]

# Input data, labels train_inputs = tf.placeholder(tf.int32, shape = [batch_size])
   train_labels = tf.placeholder(tf.int32, shape = [batch_size, 1])

# Embedding lookup table currently only implemented in CPU with
   tf.name_scope("embeddings"):
   embeddings = tf.Variable(
      tf.random_uniform([vocabulary_size, embedding_dimension], -1.0, 1.0),
         name = 'embedding')
   # This is essentialy a lookup table
   embed = tf.nn.embedding_lookup(embeddings, train_inputs)

# Create variables for the NCE loss
nce_weights = tf.Variable(
   tf.truncated_normal([vocabulary_size, embedding_dimension], stddev = 1.0 /
      math.sqrt(embedding_dimension)))

nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

loss = tf.reduce_mean(
   tf.nn.nce_loss(weights = nce_weights, biases = nce_biases, inputs = embed,
   labels = train_labels,num_sampled = negative_samples,
   num_classes = vocabulary_size)) tf.summary.scalar("NCE_loss", loss)

# Learning rate decay
global_step = tf.Variable(0, trainable = False)
   learningRate = tf.train.exponential_decay(learning_rate = 0.1,
   global_step = global_step, decay_steps = 1000, decay_rate = 0.95, staircase = True)

train_step = tf.train.GradientDescentOptimizer(learningRate).minimize(loss)
   merged = tf.summary.merge_all()
with tf.Session() as sess:
   train_writer = tf.summary.FileWriter(LOG_DIR,
      graph = tf.get_default_graph())
   saver = tf.train.Saver()

   with open(os.path.join(LOG_DIR, 'metadata.tsv'), "w") as metadata:
      metadata.write('Name\tClass\n') for k, v in index2word_map.items():
      metadata.write('%s\t%d\n' % (v, k))

   config = projector.ProjectorConfig()
   embedding = config.embeddings.add() embedding.tensor_name = embeddings.name

   # Link this tensor to its metadata file (e.g. labels).
   embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')
      projector.visualize_embeddings(train_writer, config)

   tf.global_variables_initializer().run()

   for step in range(1000):
      x_batch, y_batch = get_skipgram_batch(batch_size) summary, _ = sess.run(
         [merged, train_step], feed_dict = {train_inputs: x_batch, train_labels: y_batch})
      train_writer.add_summary(summary, step)

      if step % 100 == 0:
         saver.save(sess, os.path.join(LOG_DIR, "w2v_model.ckpt"), step)
         loss_value = sess.run(loss, feed_dict = {
            train_inputs: x_batch, train_labels: y_batch})
         print("Loss at %d: %.5f" % (step, loss_value))

   # Normalize embeddings before using
   norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims = True))
   normalized_embeddings = embeddings /
      norm normalized_embeddings_matrix = sess.run(normalized_embeddings)

ref_word = normalized_embeddings_matrix[word2index_map["one"]]

cosine_dists = np.dot(normalized_embeddings_matrix, ref_word)
ff = np.argsort(cosine_dists)[::-1][1:10] for f in ff: print(index2word_map[f])
print(cosine_dists[f])

Output

以上代码会生成以下输出 −

word2vec

TensorFlow - Single Layer Perceptron

要理解单层感知器,重要的是理解人工智能神经网络 (ANN)。人工智能神经网络是一种信息处理系统,其机制源于生物神经回路的功能。一个人工神经网络拥有许多彼此之间相连的处理单元。以下为人工神经网络的示意图 −

schematic representation

该图表表明隐藏单元与外部层进行通信。而输入和输出单元仅通过网络的隐藏层进行通信。

节点的连接模式、层总数和输入与输出之间的节点级别以及每层神经元的数量定义了神经网络的架构。

有两种类型的架构。这些类型关注以下的人工神经网络功能 −

  1. Single Layer Perceptron

  2. Multi-Layer Perceptron

Single Layer Perceptron

单层感知器是创建的第一种提出的神经模型。神经元的局部存储器的内容包含一个权向量。单层感知器的计算在输入向量的总和计算上进行,每个向量值都乘以权向量的相应元素。输出中显示的值将是激活函数的输入。

single layer perceptron

让我们关注于使用 TensorFlow 对图像分类问题实现单层感知器。通过表示“逻辑回归”来说明单层感知器是最好的示例。

logistic regression

现在,让我们考虑训练逻辑回归的以下基本步骤 −

  1. 在训练开始时,使用随机值对权重进行初始化。

  2. 对于训练集的每个元素,都会计算所需的输出与实际输出之间的差值的误差。计算出来的误差用于调整权重。

  3. 重复此流程,直到在整个训练集上产生的误差不大于指定的阈值,或者直到达到最大迭代次数。

用于评估逻辑回归的完整代码如下所述 −

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

import tensorflow as tf
import matplotlib.pyplot as plt

# Parameters
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

# tf Graph Input
x = tf.placeholder("float", [None, 784]) # mnist data image of shape 28*28 = 784
y = tf.placeholder("float", [None, 10]) # 0-9 digits recognition => 10 classes

# Create model
# Set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Construct model
activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

# Minimize error using cross entropy
cross_entropy = y*tf.log(activation)
cost = tf.reduce_mean\ (-tf.reduce_sum\ (cross_entropy,reduction_indices = 1))

optimizer = tf.train.\ GradientDescentOptimizer(learning_rate).minimize(cost)

#Plot settings
avg_set = []
epoch_set = []

# Initializing the variables init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
   sess.run(init)

   # Training cycle
   for epoch in range(training_epochs):
      avg_cost = 0.
      total_batch = int(mnist.train.num_examples/batch_size)

      # Loop over all batches
      for i in range(total_batch):
         batch_xs, batch_ys = \ mnist.train.next_batch(batch_size)
         # Fit training using batch data sess.run(optimizer, \ feed_dict = {
            x: batch_xs, y: batch_ys})
         # Compute average loss avg_cost += sess.run(cost, \ feed_dict = {
            x: batch_xs, \ y: batch_ys})/total_batch
      # Display logs per epoch step
      if epoch % display_step == 0:
         print ("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))
            avg_set.append(avg_cost) epoch_set.append(epoch+1)
   print ("Training phase finished")

   plt.plot(epoch_set,avg_set, 'o', label = 'Logistic Regression Training phase')
   plt.ylabel('cost')
   plt.xlabel('epoch')
   plt.legend()
   plt.show()

   # Test model
   correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))

   # Calculate accuracy
   accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print
      ("Model accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

Output

以上代码会生成以下输出 −

evaluation of logistic regression

逻辑回归被认为是一种预测性分析。逻辑回归用于描述数据并解释一个因变量和一个或多个名义变量或自变量之间的关系。

independent variables

TensorFlow - Linear Regression

在本章中,我们将重点关注使用 TensorFlow 实现线性回归的基本示例。逻辑回归或线性回归是一种监督机器学习方法,用于对有序离散类别进行分类。我们在本章中的目标是构建一个模型,用户可以通过该模型来预测预测变量和一个或多个自变量之间的关系。

这两个变量之间的关系被认为是线性的。如果 y 是因变量,x 被视为自变量,那么两个变量的线性回归关系将如下方程式所示 −

Y = Ax+b

我们将设计一个线性回归算法。这将使我们理解以下两个重要概念 −

  1. Cost Function

  2. Gradient descent algorithms

线性回归的示意图如下所示 −

schematic representation linear regression

线性回归方程的图形视图如下:

graphical schematic representation

Steps to design an algorithm for linear regression

现在我们将了解有关设计线性回归算法的帮助步骤。

Step 1

对于绘制线性回归模块非常重要的模块是导入。我们开始导入 Python 库 NumPy 和 Matplotlib。

import numpy as np
import matplotlib.pyplot as plt

Step 2

为逻辑回归定义必要的系数数量。

number_of_points = 500
x_point = []
y_point = []
a = 0.22
b = 0.78

Step 3

对变量进行迭代以生成 300 个围绕回归方程的随机点 −

Y = 0.22x+0.78

for i in range(number_of_points):
   x = np.random.normal(0.0,0.5)
   y = a*x + b +np.random.normal(0.0,0.1) x_point.append([x])
   y_point.append([y])

Step 4

使用 Matplotlib 查看生成点。

fplt.plot(x_point,y_point, 'o', label = 'Input Data') plt.legend() plt.show()

逻辑回归的完整代码如下 −

import numpy as np
import matplotlib.pyplot as plt

number_of_points = 500
x_point = []
y_point = []
a = 0.22
b = 0.78

for i in range(number_of_points):
   x = np.random.normal(0.0,0.5)
   y = a*x + b +np.random.normal(0.0,0.1) x_point.append([x])
   y_point.append([y])

plt.plot(x_point,y_point, 'o', label = 'Input Data') plt.legend()
plt.show()

输入点数被认为是输入数据。

code for logistic regression

TensorFlow - TFLearn And Its Installation

TFLearn 可定义为用于 TensorFlow 框架的模块化和透明的深度学习环节。TFLearn 的主要目的为 TensorFlow 提供高级的 API 以实现新实验。

考虑 TFLearn 的以下重要特质 −

  1. TFLearn 易于使用和理解。

  2. 其中包括轻松构建高度模块化网络层、优化程序和各种嵌入式指标的概念。

  3. 其中包括 TensorFlow 工作系统完全透明。

  4. 其中包括训练的内置张量功能,该张量可接受多个输入、输出和优化程序。

  5. 其中包括轻松且美观的图形可视化。

  6. 图形可视化包括权重、梯度和激活的各种细节。

执行以下命令安装 TFLearn −

pip install tflearn

执行以上代码后,将生成以下输出 −

install tflearn

下图显示了 TFLearn 与 Random Forest 分类程序的实现 −

from __future__ import division, print_function, absolute_import

#TFLearn module implementation
import tflearn
from tflearn.estimators import RandomForestClassifier

# Data loading and pre-processing with respect to dataset
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot = False)

m = RandomForestClassifier(n_estimators = 100, max_nodes = 1000)
m.fit(X, Y, batch_size = 10000, display_step = 10)

print("Compute the accuracy on train data:")
print(m.evaluate(X, Y, tflearn.accuracy_op))

print("Compute the accuracy on test set:")
print(m.evaluate(testX, testY, tflearn.accuracy_op))

print("Digits for test images id 0 to 5:")
print(m.predict(testX[:5]))

print("True digits:")
print(testY[:5])

TensorFlow - CNN And RNN Difference

在此章节中,我们将专注于 CNN 和 RNN 之间的差异 −

CNN

RNN

适用于图片等空间数据。

RNN 适用于时间数据,也称为顺序数据。

CNN 被认为比 RNN 强大。

与 CNN 相比,RNN 具有更少的特征兼容性。

此网络采用固定大小输入并生成固定大小输出。

RNN 可以处理任意输入/输出长度。

CNN 是一种前馈人工神经网络,其变体为多层感知器,旨在使用最少的预处理量。

与前馈神经网络不同,RNN 可以使用其内部内存来处理任意输入序列。

CNN 使用神经元之间的连接模式。这是受动物视觉皮层的组织方式的启发,其单个神经元以这样的方式排列,以对平铺视觉领域的重叠区域做出反应。

循环神经网络使用时间序列信息 - 用户最后所说的内容会影响他/她接下来会所说的内容。

CNN 非常适合图像和视频处理。

RNN 非常适合文本和语音分析。

以下插图显示了 CNN 和 RNN 的示意图 -

schematic representation of cnn and rnn

TensorFlow - Keras

Keras 是一个紧凑、易于学习、高级的 Python 库,运行在 TensorFlow 框架之上。它的创建重点是理解深度学习技术,例如创建神经网络层,同时保持形状和数学细节的概念。框架的创建可以是以下两种类型 -

  1. Sequential API

  2. Functional API

考虑以下八个步骤在 Keras 中创建深度学习模型 -

  1. Loading the data

  2. Preprocess the loaded data

  3. Definition of model

  4. Compiling the model

  5. Fit the specified model

  6. Evaluate it

  7. Make the required predictions

  8. Save the model

我们将使用 Jupyter Notebook 执行和显示输出,如下所示 -

Step 1 - 首先实现加载数据和预处理加载的数据以执行深度学习模型。

import warnings
warnings.filterwarnings('ignore')

import numpy as np
np.random.seed(123) # for reproducibility

from keras.models import Sequential
from keras.layers import Flatten, MaxPool2D, Conv2D, Dense, Reshape, Dropout
from keras.utils import np_utils
Using TensorFlow backend.
from keras.datasets import mnist

# Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

此步骤可以定义为“导入库和模块”,这意味着所有库和模块都被导入为初始步骤。

Step 2 - 在此步骤中,我们将定义模型架构 -

model = Sequential()
model.add(Conv2D(32, 3, 3, activation = 'relu', input_shape = (28,28,1)))
model.add(Conv2D(32, 3, 3, activation = 'relu'))
model.add(MaxPool2D(pool_size = (2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation = 'softmax'))

Step 3 - 让我们现在编译指定模型-

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

Step 4 - 我们现在将使用训练数据拟合模型-

model.fit(X_train, Y_train, batch_size = 32, epochs = 10, verbose = 1)

创建的迭代输出如下-

Epoch 1/10 60000/60000 [==============================] - 65s -
loss: 0.2124 -
acc: 0.9345
Epoch 2/10 60000/60000 [==============================] - 62s -
loss: 0.0893 -
acc: 0.9740
Epoch 3/10 60000/60000 [==============================] - 58s -
loss: 0.0665 -
acc: 0.9802
Epoch 4/10 60000/60000 [==============================] - 62s -
loss: 0.0571 -
acc: 0.9830
Epoch 5/10 60000/60000 [==============================] - 62s -
loss: 0.0474 -
acc: 0.9855
Epoch 6/10 60000/60000 [==============================] - 59s -
loss: 0.0416 -
acc: 0.9871
Epoch 7/10 60000/60000 [==============================] - 61s -
loss: 0.0380 -
acc: 0.9877
Epoch 8/10 60000/60000 [==============================] - 63s -
loss: 0.0333 -
acc: 0.9895
Epoch 9/10 60000/60000 [==============================] - 64s -
loss: 0.0325 -
acc: 0.9898
Epoch 10/10 60000/60000 [==============================] - 60s -
loss: 0.0284 -
acc: 0.9910

TensorFlow - Distributed Computing

本章将重点介绍如何开始使用分布式 TensorFlow。目的是帮助开发人员理解不断重复出现的 TF 服务器等基本的分布式 TF 概念。我们将使用 Jupyter Notebook 来评估分布式 TensorFlow。下面提到了使用 TensorFlow 进行分布式计算的实现−

Step 1 − 导入分布式计算必需的重要模块−

import tensorflow as tf

Step 2 − 使用一个节点创建 TensorFlow 集群。让此节点负责一个名为“worker”且将在 localhost:2222 上操作一个任务的作业。

cluster_spec = tf.train.ClusterSpec({'worker' : ['localhost:2222']})
server = tf.train.Server(cluster_spec)
server.target

以上脚本会生成以下输出:

'grpc://localhost:2222'
The server is currently running.

Step 3 − 可以通过执行以下命令来计算具有相应会话的服务器配置 −

server.server_def

上述命令生成以下输出 −

cluster {
   job {
      name: "worker"
      tasks {
         value: "localhost:2222"
      }
   }
}
job_name: "worker"
protocol: "grpc"

Step 4 − 使用 TensorFlow 启动以执行引擎为服务器的会话。使用 TensorFlow 创建一个本地服务器并使用 lsof 查找服务器的位置。

sess = tf.Session(target = server.target)
server = tf.train.Server.create_local_server()

Step 5 − 查看此会话中可用的设备并关闭相应的会话。

devices = sess.list_devices()
for d in devices:
   print(d.name)
sess.close()

上述命令生成以下输出 −

/job:worker/replica:0/task:0/device:CPU:0

TensorFlow - Exporting

在这里,我们着重于 TensorFlow 中的 MetaGraph 形成。这将有助于我们理解 TensorFlow 中的导出模块。MetaGraph 包含基本信息,该信息对于在先前训练的图中训练、执行评估或运行推理是必需的。

以下是相同的代码片段-

def export_meta_graph(filename = None, collection_list = None, as_text = False):
   """this code writes `MetaGraphDef` to save_path/filename.

   Arguments:
   filename: Optional meta_graph filename including the path. collection_list:
      List of string keys to collect. as_text: If `True`,
      writes the meta_graph as an ASCII proto.

   Returns:
   A `MetaGraphDef` proto. """

下面提到典型用法模型之一-

# Build the model ...
with tf.Session() as sess:
   # Use the model ...
# Export the model to /tmp/my-model.meta.
meta_graph_def = tf.train.export_meta_graph(filename = '/tmp/my-model.meta')

TensorFlow - Multi-Layer Perceptron Learning

多层感知器定义了人工神经网络最复杂的架构。它基本上由多层感知器形成。

多层感知器学习的示意图如下所示-

multi layer perceptron

MLP 网络通常用于监督学习格式。MLP 网络的典型学习算法也称为反向传播算法。

现在,我们将重点放在 MLP 对图像分类问题的实施上。

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

import tensorflow as tf
import matplotlib.pyplot as plt

# Parameters
learning_rate = 0.001
training_epochs = 20
batch_size = 100
display_step = 1

# Network Parameters
n_hidden_1 = 256

# 1st layer num features
n_hidden_2 = 256 # 2nd layer num features
n_input = 784 # MNIST data input (img shape: 28*28) n_classes = 10
# MNIST total classes (0-9 digits)

# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

# weights layer 1
h = tf.Variable(tf.random_normal([n_input, n_hidden_1])) # bias layer 1
bias_layer_1 = tf.Variable(tf.random_normal([n_hidden_1]))
# layer 1 layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, h), bias_layer_1))

# weights layer 2
w = tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]))

# bias layer 2
bias_layer_2 = tf.Variable(tf.random_normal([n_hidden_2]))

# layer 2
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, w), bias_layer_2))

# weights output layer
output = tf.Variable(tf.random_normal([n_hidden_2, n_classes]))

# biar output layer
bias_output = tf.Variable(tf.random_normal([n_classes])) # output layer
output_layer = tf.matmul(layer_2, output) + bias_output

# cost function
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
   logits = output_layer, labels = y))

#cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(output_layer, y))
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)

# optimizer = tf.train.GradientDescentOptimizer(
   learning_rate = learning_rate).minimize(cost)

# Plot settings
avg_set = []
epoch_set = []

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
   sess.run(init)

   # Training cycle
   for epoch in range(training_epochs):
      avg_cost = 0.
      total_batch = int(mnist.train.num_examples / batch_size)

      # Loop over all batches
      for i in range(total_batch):
         batch_xs, batch_ys = mnist.train.next_batch(batch_size)
         # Fit training using batch data sess.run(optimizer, feed_dict = {
            x: batch_xs, y: batch_ys})
         # Compute average loss
         avg_cost += sess.run(cost, feed_dict = {x: batch_xs, y: batch_ys}) / total_batch
      # Display logs per epoch step
      if epoch % display_step == 0:
         print
         Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost)
      avg_set.append(avg_cost)
      epoch_set.append(epoch + 1)
   print
   "Training phase finished"

   plt.plot(epoch_set, avg_set, 'o', label = 'MLP Training phase')
   plt.ylabel('cost')
   plt.xlabel('epoch')
   plt.legend()
   plt.show()

   # Test model
   correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1))

   # Calculate accuracy
   accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
   print
   "Model Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})

上述代码行生成以下输出 -

implementation with mlp

TensorFlow - Hidden Layers of Perceptron

在本章中,我们将重点关注从称为 x 和 f(x) 的已知点集中学习的网络。一个隐藏层将构建这个简单的网络。

感知器隐藏层的说明代码如下所示-

#Importing the necessary modules
import tensorflow as tf
import numpy as np
import math, random
import matplotlib.pyplot as plt

np.random.seed(1000)
function_to_learn = lambda x: np.cos(x) + 0.1*np.random.randn(*x.shape)
layer_1_neurons = 10
NUM_points = 1000

#Training the parameters
batch_size = 100
NUM_EPOCHS = 1500

all_x = np.float32(np.random.uniform(-2*math.pi, 2*math.pi, (1, NUM_points))).T
   np.random.shuffle(all_x)

train_size = int(900)
#Training the first 700 points in the given set x_training = all_x[:train_size]
y_training = function_to_learn(x_training)

#Training the last 300 points in the given set x_validation = all_x[train_size:]
y_validation = function_to_learn(x_validation)

plt.figure(1)
plt.scatter(x_training, y_training, c = 'blue', label = 'train')
plt.scatter(x_validation, y_validation, c = 'pink', label = 'validation')
plt.legend()
plt.show()

X = tf.placeholder(tf.float32, [None, 1], name = "X")
Y = tf.placeholder(tf.float32, [None, 1], name = "Y")

#first layer
#Number of neurons = 10
w_h = tf.Variable(
   tf.random_uniform([1, layer_1_neurons],\ minval = -1, maxval = 1, dtype = tf.float32))
b_h = tf.Variable(tf.zeros([1, layer_1_neurons], dtype = tf.float32))
h = tf.nn.sigmoid(tf.matmul(X, w_h) + b_h)

#output layer
#Number of neurons = 10
w_o = tf.Variable(
   tf.random_uniform([layer_1_neurons, 1],\ minval = -1, maxval = 1, dtype = tf.float32))
b_o = tf.Variable(tf.zeros([1, 1], dtype = tf.float32))

#build the model
model = tf.matmul(h, w_o) + b_o

#minimize the cost function (model - Y)
train_op = tf.train.AdamOptimizer().minimize(tf.nn.l2_loss(model - Y))

#Start the Learning phase
sess = tf.Session() sess.run(tf.initialize_all_variables())

errors = []
for i in range(NUM_EPOCHS):
   for start, end in zip(range(0, len(x_training), batch_size),\
      range(batch_size, len(x_training), batch_size)):
      sess.run(train_op, feed_dict = {X: x_training[start:end],\ Y: y_training[start:end]})
   cost = sess.run(tf.nn.l2_loss(model - y_validation),\ feed_dict = {X:x_validation})
   errors.append(cost)

   if i%100 == 0:
      print("epoch %d, cost = %g" % (i, cost))

plt.plot(errors,label='MLP Function Approximation') plt.xlabel('epochs')
plt.ylabel('cost')
plt.legend()
plt.show()

Output

以下是函数层逼近的表示-

function layer approximation

这里两个数据以 W 的形状表示。这两个数据是:训练和验证,它们以不同的颜色表示,如图例部分所示。

distinct colors
mlp function approximation

TensorFlow - Optimizers

优化器是扩展类,其中包括用于训练特定模型的附加信息。优化器类使用给定的参数进行初始化,但重要的是要记住不需要张量。优化器用于提高训练特定模型的速度和性能。

TensorFlow 的基本优化器是-

tf.train.Optimizer

此类在 tensorflow/python/training/optimizer.py 的指定路径中定义。

以下是 Tensorflow 中的一些优化器-

  1. Stochastic Gradient descent

  2. 梯度裁剪的随机梯度下降

  3. Momentum

  4. Nesterov momentum

  5. Adagrad

  6. Adadelta

  7. RMSProp

  8. Adam

  9. Adamax

  10. SMORMS3

我们将专注于随机梯度下降。为其创建优化器的说明如下:

def sgd(cost, params, lr = np.float32(0.01)):
   g_params = tf.gradients(cost, params)
   updates = []

   for param, g_param in zip(params, g_params):
      updates.append(param.assign(param - lr*g_param))
   return updates

基本参数在特定函数内定义。在随后的章节中,我们将专注于具有优化器实现的梯度下降优化。

TensorFlow - XOR Implementation

在本章中,我们将了解使用 TensorFlow 的 XOR 实现。在开始使用 TensorFlow 中的 XOR 实现之前,让我们看看 XOR 表值。这将帮助我们理解加密和解密过程。

A

B

A XOR B

0

0

0

0

1

1

1

0

1

1

1

0

XOR 密码加密方法基本上用来加密难以用蛮力方法(即通过生成与适当密钥匹配的随机加密密钥来)破解的数据。

使用 XOR 密码实现的概念是定义一个 XOR 加密密钥,然后对指定字符串中的字符执行与该密钥的 XOR 操作,这是用户尝试进行加密的操作。现在,我们将专注于使用 TensorFlow 的 XOR 实现,如下所述:

#Declaring necessary modules
import tensorflow as tf
import numpy as np
"""
A simple numpy implementation of a XOR gate to understand the backpropagation
algorithm
"""

x = tf.placeholder(tf.float64,shape = [4,2],name = "x")
#declaring a place holder for input x
y = tf.placeholder(tf.float64,shape = [4,1],name = "y")
#declaring a place holder for desired output y

m = np.shape(x)[0]#number of training examples
n = np.shape(x)[1]#number of features
hidden_s = 2 #number of nodes in the hidden layer
l_r = 1#learning rate initialization

theta1 = tf.cast(tf.Variable(tf.random_normal([3,hidden_s]),name = "theta1"),tf.float64)
theta2 = tf.cast(tf.Variable(tf.random_normal([hidden_s+1,1]),name = "theta2"),tf.float64)

#conducting forward propagation
a1 = tf.concat([np.c_[np.ones(x.shape[0])],x],1)
#the weights of the first layer are multiplied by the input of the first layer

z1 = tf.matmul(a1,theta1)
#the input of the second layer is the output of the first layer, passed through the
   activation function and column of biases is added

a2 = tf.concat([np.c_[np.ones(x.shape[0])],tf.sigmoid(z1)],1)
#the input of the second layer is multiplied by the weights

z3 = tf.matmul(a2,theta2)
#the output is passed through the activation function to obtain the final probability

h3 = tf.sigmoid(z3)
cost_func = -tf.reduce_sum(y*tf.log(h3)+(1-y)*tf.log(1-h3),axis = 1)

#built in tensorflow optimizer that conducts gradient descent using specified
   learning rate to obtain theta values

optimiser = tf.train.GradientDescentOptimizer(learning_rate = l_r).minimize(cost_func)

#setting required X and Y values to perform XOR operation
X = [[0,0],[0,1],[1,0],[1,1]]
Y = [[0],[1],[1],[0]]

#initializing all variables, creating a session and running a tensorflow session
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

#running gradient descent for each iteration and printing the hypothesis
   obtained using the updated theta values
for i in range(100000):
   sess.run(optimiser, feed_dict = {x:X,y:Y})#setting place holder values using feed_dict
   if i%100==0:
      print("Epoch:",i)
      print("Hyp:",sess.run(h3,feed_dict = {x:X,y:Y}))

上面这一行代码生成的输出如以下截图所示 −

xor implementation using tensorflow

TensorFlow - Gradient Descent Optimization

梯度下降优化被认为是数据科学中的一个重要概念。

考虑下面所示的步骤来了解梯度下降优化的实现 −

Step 1

包含必需的模块并声明 x 和 y 变量,我们将通过它们来定义梯度下降优化。

import tensorflow as tf

x = tf.Variable(2, name = 'x', dtype = tf.float32)
log_x = tf.log(x)
log_x_squared = tf.square(log_x)

optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(log_x_squared)

Step 2

初始化必要的变量,并调用优化器为其定义并使用各自函数调用它。

init = tf.initialize_all_variables()

def optimize():
   with tf.Session() as session:
      session.run(init)
      print("starting at", "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))

      for step in range(10):
         session.run(train)
         print("step", step, "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
optimize()

上面这一行代码生成的输出如以下截图所示 −

initialize variables

我们可以看到必要的 epoch 和迭代如输出所示已经计算出来。

TensorFlow - Forming Graphs

偏微分方程 (PDE) 是一种包含多个自变量的未知函数偏导数的微分方程。关于偏微分方程,我们将专注于创建新的图形。

让我们假设有一个尺寸为 500*500 平方的池塘 −

N = 500

现在,我们将计算偏微分方程,并使用它形成相应的图形。考虑下面给出的计算图形的步骤。

Step 1 − 导入模拟库。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

Step 2 − 包含将二维阵列转换为卷积核和简化的二维卷积运算的函数。

def make_kernel(a):
   a = np.asarray(a)
   a = a.reshape(list(a.shape) + [1,1])
   return tf.constant(a, dtype=1)

def simple_conv(x, k):
   """A simplified 2D convolution operation"""
   x = tf.expand_dims(tf.expand_dims(x, 0), -1)
   y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding = 'SAME')
   return y[0, :, :, 0]

def laplace(x):
   """Compute the 2D laplacian of an array"""
   laplace_k = make_kernel([[0.5, 1.0, 0.5], [1.0, -6., 1.0], [0.5, 1.0, 0.5]])
   return simple_conv(x, laplace_k)

sess = tf.InteractiveSession()

Step 3 − 包含迭代次数并计算图形以相应地显示记录。

N = 500

# Initial Conditions -- some rain drops hit a pond

# Set everything to zero
u_init = np.zeros([N, N], dtype = np.float32)
ut_init = np.zeros([N, N], dtype = np.float32)

# Some rain drops hit a pond at random points
for n in range(100):
   a,b = np.random.randint(0, N, 2)
   u_init[a,b] = np.random.uniform()

plt.imshow(u_init)
plt.show()

# Parameters:
# eps -- time resolution
# damping -- wave damping
eps = tf.placeholder(tf.float32, shape = ())
damping = tf.placeholder(tf.float32, shape = ())

# Create variables for simulation state
U = tf.Variable(u_init)
Ut = tf.Variable(ut_init)

# Discretized PDE update rules
U_ = U + eps * Ut
Ut_ = Ut + eps * (laplace(U) - damping * Ut)

# Operation to update the state
step = tf.group(U.assign(U_), Ut.assign(Ut_))

# Initialize state to initial conditions
tf.initialize_all_variables().run()

# Run 1000 steps of PDE
for i in range(1000):
   # Step simulation
   step.run({eps: 0.03, damping: 0.04})

   # Visualize every 50 steps
   if i % 500 == 0:
      plt.imshow(U.eval())
      plt.show()

图形如下所示 −

forming graphs
graphs plotted

Image Recognition using TensorFlow

TensorFlow 包括图像识别的特殊功能,这些图像存储在特定文件夹中。与相对相同的图像相比,将容易为安全目的实现此逻辑。

图像识别代码实现的文件夹结构如下所示:

image recognition

dataset_image 包含需要加载的相关图像。我们将专注于图像识别以及其中定义的徽标。图像使用 “load_data.py” 脚本加载,它有助于记录其中的各种图像识别模块。

import pickle
from sklearn.model_selection import train_test_split
from scipy import misc

import numpy as np
import os

label = os.listdir("dataset_image")
label = label[1:]
dataset = []

for image_label in label:
   images = os.listdir("dataset_image/"+image_label)

   for image in images:
      img = misc.imread("dataset_image/"+image_label+"/"+image)
      img = misc.imresize(img, (64, 64))
      dataset.append((img,image_label))
X = []
Y = []

for input,image_label in dataset:
   X.append(input)
   Y.append(label.index(image_label))

X = np.array(X)
Y = np.array(Y)

X_train,y_train, = X,Y

data_set = (X_train,y_train)

save_label = open("int_to_word_out.pickle","wb")
pickle.dump(label, save_label)
save_label.close()

图像训练有助于存储指定文件夹中的可识别模式。

import numpy
import matplotlib.pyplot as plt

from keras.layers import Dropout
from keras.layers import Flatten
from keras.constraints import maxnorm
from keras.optimizers import SGD
from keras.layers import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K

import load_data
from keras.models import Sequential
from keras.layers import Dense

import keras
K.set_image_dim_ordering('tf')

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load data
(X_train,y_train) = load_data.data_set

# normalize inputs from 0-255 to 0.0-1.0
X_train = X_train.astype('float32')

#X_test = X_test.astype('float32')
X_train = X_train / 255.0

#X_test = X_test / 255.0
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)

#y_test = np_utils.to_categorical(y_test)
num_classes = y_train.shape[1]

# Create the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), padding = 'same',
   activation = 'relu', kernel_constraint = maxnorm(3)))

model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation = 'relu', padding = 'same',
   kernel_constraint = maxnorm(3)))

model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(512, activation = 'relu', kernel_constraint = maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = 'softmax'))

# Compile model
epochs = 10
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr = lrate, momentum = 0.9, decay = decay, nesterov = False)
model.compile(loss = 'categorical_crossentropy', optimizer = sgd, metrics = ['accuracy'])
print(model.summary())

#callbacks = [keras.callbacks.EarlyStopping(
   monitor = 'val_loss', min_delta = 0, patience = 0, verbose = 0, mode = 'auto')]
callbacks = [keras.callbacks.TensorBoard(log_dir='./logs',
   histogram_freq = 0, batch_size = 32, write_graph = True, write_grads = False,
   write_images = True, embeddings_freq = 0, embeddings_layer_names = None,
   embeddings_metadata = None)]

# Fit the model

model.fit(X_train, y_train, epochs = epochs,
   batch_size = 32,shuffle = True,callbacks = callbacks)

# Final evaluation of the model
scores = model.evaluate(X_train, y_train, verbose = 0)
print("Accuracy: %.2f%%" % (scores[1]*100))

# serialize model to JSONx
model_json = model.to_json()
with open("model_face.json", "w") as json_file:
   json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("model_face.h5")
print("Saved model to disk")

上面的代码行生成一个输出,如下所示:

recognizable patterns
recognizable patterns output

Recommendations for Neural Network Training

在本章中,我们将了解 TensorFlow 框架可以实现的神经网络训练的各个方面。

以下是可以评估的十条建议−

Back Propagation

反向传播是一种计算偏导数的简单方法,包括最适合神经网络的基本构成形式。

back propagation

Stochastic Gradient Descent

在随机梯度下降中, batch 是示例的总数,用户使用该示例在单个迭代中计算梯度。到目前为止,假定批次是整个数据集。最好的说明是在 Google 规模上工作;数据集通常包含数十亿甚至数百亿个示例。

stochastic gradient descent

Learning Rate Decay

learning rate decay

采用学习率是梯度下降优化最重要的特性之一。这对于 TensorFlow 实现至关重要。

Dropout

具有大量参数的深度神经网络构成了强大的机器学习系统。然而,过拟合是此类网络中的一个严重问题。

dropout

Max Pooling

最大池化是一种基于样本的离散化过程。目的是对输入表示进行下采样,从而通过所需的假设来减小维度。

max pooling

Long Short Term Memory (LSTM)

LSTM 控制关于在指定的神经元中应采用哪些输入的决策。它包括控制决定应该计算什么以及应该生成什么输出。

long short term memory