卷积神经网络玩游戏?
【前言】直接上代码是最有效的学习方式。
这篇教程通过由一段简短的 python 代码实现的非常简单的实例来讲解 BP 反向传播算法。
代码如下:
X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ])
y = np.array([[0,1,1,0]]).T
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,1)) - 1
for j in xrange(60000):
l1 = 1/(1+np.exp(-(np.dot(X,syn0))))
l2 = 1/(1+np.exp(-(np.dot(l1,syn1))))
l2_delta = (y - l2)*(l2*(1-l2))
l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1))
syn1 += l1.T.dot(l2_delta)
syn0 += X.T.dot(l1_delta)
当然,上述程序可能过于简练了。下面我会将其简要分解成几个部分进行探讨。
2 层神经网络:
import numpy as np
# sigmoid function
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
# input dataset
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
# output dataset
y = np.array([[0,0,1,1]]).T
# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)
# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1
for iter in xrange(10000):
# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))
# how much did we miss?
l1_error = y - l1
# multiply how much we missed by the
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)
# update weights
syn0 += np.dot(l0.T,l1_delta)
print "Output After Training:"
print l1
Output After Training:
[[ 0.00966449]
[ 0.00786506]
[ 0.99358898]
[ 0.99211957]]
变量 定义说明
X 输入数据集,形式为矩阵,每 1 行代表 1 个训练样本。
y 输出数据集,形式为矩阵,每 1 行代表 1 个训练样本。
l0 网络第 1 层,即网络输入层。
l1 网络第 2 层,常称作隐藏层。
syn0 第一层权值,突触 0 ,连接 l0 层与 l1 层。
* 逐元素相乘,故两等长向量相乘等同于其对等元素分别相乘,结果为同等长度的向量。
- 元素相减,故两等长向量相减等同于其对等元素分别相减,结果为同等长度的向量。
x.dot(y) 若 x 和 y 为向量,则进行点积操作;若均为矩阵,则进行矩阵相乘操作;若其中之一为矩阵,则进行向量与矩阵相乘操作。
正如在“训练后结果输出”中看到的,程序正确执行!在描述具体过程之前,我建议读者事先去尝试理解并运行下代码,对算法程序的工作方式有一个直观的感受。最好能够在 ipython notebook 中原封不动地跑通以上程序(或者你想自己写个脚本也行,但我还是强烈推荐 notebook )。下面是对理解程序有帮助的几个关键地方:
对比 l1 层在首次迭代和最后一次迭代时的状态。
仔细察看 “nonlin” 函数,正是它将一个概率值作为输出提供给我们。
仔细观察在迭代过程中,l1_error 是如何变化的。
将第 36 行中的表达式拆开来分析,大部分秘密武器就在这里面。
仔细理解第 39 行代码,网络中所有操作都是在为这步运算做准备。有一款软件做的不错 阿卡索口语秀 推荐给大家