神经网络
131
尔数据的另一种表示方法是使用二进制位对“是”或“否”进行编码:
0
表示
false
1
表示
true
目前已经有许多常见的处理布尔数据的函数。例如 ORANDNANDNOR NOT 等都
是布尔函数。这类函数接受两个布尔数据类型的输入,输出的结果也是布尔数据
true
false
这些方法已经通过数字逻辑门给电子行业带来了巨大的进步,并且可以组合在一起来
解决许多问题。但是如何构建布尔函数的模型呢?
以下所示是一个对
OR
函数进行建模的简单例子:
This is such a common occurrence that there already exist many functions that deal
with Boolean data. Functions such as
OR, AND, NAND, NOR, and NOT are all Boolean func‐
tions. They take in two inputs that are true or false and output something that is true
or false.
These have been used for great advances in the electronics community through digi‐
tal logic gates and can be composed together to solve many problems. But how would
we go about constructing something like this?
A simple example of modeling the
OR function would be the following:
OR a, b = min 1, a + b
Perceptrons
Perceptrons take the idea of Boolean logic even further to include more fuzzy logic.
They usually involve returning a value based on a threshold being met or not. Lets
say that you’re a teacher and you wish to assign pass/fail grades to your students at
the end of the quarter. Obviously you need to come up with a way of cutting off the
people who failed from the ones who didnt. This can be quite subjective but usually
follows a general procedure of:
def threshold(x):
if sum(weights * x) + b > 0.5:
return 1
else:
return 0
x is a vector of all the grades you collected the entire quarter and weights is a vector
of weightings. For instance, you might want to weight the final grade higher.
b is just
a freebie to the students for showing up.
Using such a simple formula we could traverse the optimal weightings by determin‐
ing a priori how many people wed like to fail. Let’s say we have 100 students and only
want to fail the bottom 10%. This goal is something we can actually code.
How to Construct Feed-Forward Neural Nets
There are many different kinds of neural networks, but this chapter will focus on
feed-forward networks and recurrent networks.
What makes neural networks special is their use of a hidden layer of weighted func‐
tions called neurons, with which you can effectively build a network that maps a lot
of other functions (Figure 8-2). Without a hidden layer of functions, neural networks
would be just a set of simple weighted functions.
Perceptrons | 131
感知器
感知器利用了布尔逻辑的思想,并更进一步,包含了更多的模糊逻辑。感知器通常需
要基于某些阈值的达到与否来决定返回值。例如,假设你是一名老师,你想要在本季
度末决定哪些学生可以通过,哪些学生不能通过。显然,你需要一种方法,把通过的
学生和没通过的学生分开。这可能是个相当主观的决定,但通常会遵循以下常规程序
This is such a common occurrence that there already exist many functions that deal
with Boolean data. Functions such as
OR, AND, NAND, NOR, and NOT are all Boolean func‐
tions. They take in two inputs that are true or false and output something that is true
or false.
These have been used for great advances in the electronics community through digi‐
tal logic gates and can be composed together to solve many problems. But how would
we go about constructing something like this?
A simple example of modeling the
OR function would be the following:
OR a, b = min 1, a + b
Perceptrons
Perceptrons take the idea of Boolean logic even further to include more fuzzy logic.
They usually involve returning a value based on a threshold being met or not. Lets
say that you’re a teacher and you wish to assign pass/fail grades to your students at
the end of the quarter. Obviously you need to come up with a way of cutting off the
people who failed from the ones who didnt. This can be quite subjective but usually
follows a general procedure of:
def threshold(x):
if sum(weights * x) + b > 0.5:
return 1
else:
return 0
x is a vector of all the grades you collected the entire quarter and weights is a vector
of weightings. For instance, you might want to weight the final grade higher.
b is just
a freebie to the students for showing up.
Using such a simple formula we could traverse the optimal weightings by determin‐
ing a priori how many people wed like to fail. Let’s say we have 100 students and only
want to fail the bottom 10%. This goal is something we can actually code.
How to Construct Feed-Forward Neural Nets
There are many different kinds of neural networks, but this chapter will focus on
feed-forward networks and recurrent networks.
What makes neural networks special is their use of a hidden layer of weighted func‐
tions called neurons, with which you can effectively build a network that maps a lot
of other functions (Figure 8-2). Without a hidden layer of functions, neural networks
would be just a set of simple weighted functions.
Perceptrons | 131
在这段代码中,变量
x
是在整个季度收集的所有成绩的向量,weights 是一个权重向量。
例如,你可能想要给期末考试的成绩更高的权重。而此处的
b
只是因为学生按时出勤
而赠送的分数。
使用这样一个简单的公式,可以先确定我们想要“不通过”的学生人数,来遍历所有
权重值,从而获取最优的权重。假设我们有
100
名学生,只想让
10
%的学生“不通过”
我们可以通过编写程序来实现这个目标。
如何构建前馈神经网络
神经网络有许多不同的种类,但本章将重点关注前馈网络和循环网络。
神经网络之所以很特别,是因为它们使用了一层隐藏的加权函数,这些函数叫作神
经元。在这个隐藏层中,你可以有效地构建一个使用了许多其他函数的网络(见图
8-2
)。如果没有隐藏层的这些函数,神经网络只是一组简单的加权函数罢了。
输入层 隐藏层 输出层
8-2:前馈网络
神经网络可以用每层神经元的数量表示。例如,如果输入层中有
20
个神经元,一个
隐藏层中有
10
个神经元,在输出层中有
5
个,那将是一个
20-10-5
网络。如果有多个
隐藏层,那么我们可以将其表示为
20-7-7-5
(中间两个
7
,代表两个具有
7
个神经元
的隐藏层)这样的形式。
总之,神经网络包括以下几个部分:
输入层
隐藏层(可能有多个)
神经元
输出层
训练算法
接下来,逐个介绍这些组成部分以及它们的工作原理。
输入层
如图
8-3
所示,输入层是神经网络的入口点,是模型设置的输入数据的地方。在这一
层中没有神经元,因为它的主要目的是作为隐藏层的导入渠道。输入类型是很重要的,
因为神经网络只对两种类型可用:对称型和标准型。
132
8
输入层 隐藏层 输出层
8-3:神经网络的输入层
当对神经网络进行训练时,有两类数据:观察和输入。以异或(也称为
XOR
)为一
个简单例子,它的真值表如表
8-1
所示。
8-1XOR 真值表
A B XOR(A,B)
0 0 0
0 1 1
1 0 1
1 1 0
表示异或的另一种方法是查看维恩图(见图
8-4
)。给定两组数据,阴影区域显示这
两组数据的
XOR
区域。注意,中间白色区域是空集。
8-4:维恩图中的 XOR 函数(图片来源:Wikimedia
在这个例子中有四个观察值和两个输入值,它们可能是
true
或者
false
。然而,神经
网络不能直接处理
true
或者
false
这样的数据,所以关键是如何对输入数据进行编码。
我们需要将它们转换为标准输入或者对称输入。

Get Python 机器学习实践:测试驱动的开发方法 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.