python实现神经网络_python实现简易的 神经网络算法(Nerual Networks) 原理及示例

1. 关于非线性转化方程(non-linear transformation function)

sigmoid函数(S 曲线)用来作为activation function:

20170615115459.png

1.1 双曲函数(tanh)

Sinh_cosh_tanh.svg_.png

双曲函数基本定义 来源于wiki

20170615115757.png

这个计算线类似于简单的三角函数sin cos tan

timg.jpg

1.2  逻辑函数(logistic function)

Logistic-curve.svg_.png

2. 实现一个简单的神经网络算法

#!/usr/bin/python

#coding:utf-8

import numpy as np #这是实现一个简易版的神经网络实现

#直接调用一个简单的双曲函数

def tanh(x):

return np.tanh(x)

#求tanh导数function

def tanh_deriv(x):

return 1.0 - np.tanh(x)*np.tanh(x)

#定义S型函数

def logistic(x):

return 1/(1 + np.exp(-x))

#求logistic导数

def logistic_derivative(x):

return logistic(x)*(1-logistic(x))

class NeuralNetwork:

#layers 指定神经网络层数和神经元个数 最少有两层 input and output 如[10,10,3]

#activation指定一个选择用tanh 默认使用tanh 还可以使用logistic

def __init__(self, layers, activation='tanh'):

"""

:param layers: A list containing the number of units in each layer.

Should be at least two values

:param activation: The activation function to be used. Can be

"logistic" or "tanh"

"""

#选取用户选取的函数类型

if activation == 'logistic':

self.activation = logistic

self.activation_deriv = logistic_derivative

elif activation == 'tanh':

self.activation = tanh

self.activation_deriv = tanh_deriv

#定义weight装权重(weight) - 也就是之前图解的连线

self.weights = []

#得到神经网络层数 排除第一层和最后一层循环

for i in range(1, len(layers) - 1):

#对i层 与 i-1层 与第一层 进行权重(weight)连线赋值

self.weights.append((2*np.random.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)

#对i层 与 i+1层 与最后一层 进行权重(weight)连线赋值

self.weights.append((2*np.random.random((layers[i] + 1, layers[i + 1]))-1)*0.25)

#训练神经网络fit函数

# X 是训练集 一个二维矩阵

# y 对应class label 分类标记 是 两个分类 还是 0-9 10个分类 定义

# learning_rate 是学习率 数值大的话 步子大

# epochs 神经网络最多执行次数 每次样本随机抽取 全部执行运算较多 有一个数来控制次数

# (注意:这里是实现一个简易版的神经网络的实现 而之前的图解中停止条件应该是多个条件控制)

def fit(self, X, y, learning_rate=0.2, epochs=10000):

X = np.atleast_2d(X) #最少是一个二维的数组

temp = np.ones([X.shape[0], X.shape[1]+1]) #ones初始化一个矩阵 参数是传入行数和列数+1 初始化的值全是1

temp[:, 0:-1] = X # adding the bias unit to the input layer :取所有的行 :列取第一列和除了最后一列

X = temp

y = np.array(y) #数据类型转换为 np科学计算数组格式

#更新神经网络

for k in range(epochs): #循环次数循环

i = np.random.randint(X.shape[0]) #随机抽取一行

a = [X[i]] # 随机从x中抽取一个实例

#从输入层正向更新计算神经元中的值

for l in range(len(self.weights)): #going forward network, for each layer

a.append(self.activation(np.dot(a[l], self.weights[l]))) #Computer the node value for each layer (O_i) using activation function

#根据真实的class lable 与预测 class lable 结果 做减法 output 计算error

error = y[i] - a[-1] #Computer the error at the top layer

#根据当前最后一层神经元的值进行反向更新

deltas = [error * self.activation_deriv(a[-1])] #For output layer, Err calculation (delta is updated error)

# 开始反向更新

#Staring backprobagation

for l in range(len(a) - 2, 0, -1): # we need to begin at the second to last layer 从最后一层开始到0层 每一次往回退一次

#Compute the updated error (i,e, deltas) for each node going from top layer to input layer

deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_deriv(a[l])) #更新隐藏层error更新 之前图解公式的Errj

deltas.reverse() #顺序颠倒

#根据公式更新权重(weight)

for i in range(len(self.weights)):

layer = np.atleast_2d(a[i])

delta = np.atleast_2d(deltas[i])

self.weights[i] += learning_rate * layer.T.dot(delta)

#预测结果 跟上面的正向流程类似计算出输出层的值 不需要保存每一层的值

# 得数是0 到 1 有时候是 -1 到 1 以0.5为界限

def predict(self, x):

x = np.array(x)

temp = np.ones(x.shape[0]+1)

temp[0:-1] = x

a = temp

for l in range(0, len(self.weights)):

a = self.activation(np.dot(a, self.weights[l]))

return a

例子1:

简单非线性关系数据集测试(XOR): 两个数值不一样为 1 否则为 0 (因为XOR异或运算是非线性运算所以用这个举例)

X: Y

0 0 0

0 1 1

1 0 1

1 1 0

from NeuralNetwork import NeuralNetwork

import numpy as np

# 2 2 1 第一层是输入层二维数据 2个神经元 第二层隐藏层 2 个神经元 第三层输出层 得到结果 calss label 一个作为输出层

nn = NeuralNetwork([2, 2, 1], 'tanh')

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

y = np.array([0, 1, 1, 0])

nn.fit(X, y)

for i in [[0, 0], [0, 1], [1, 0], [1, 1]]:

print(i, nn.predict(i))

例子2

每个图片8×8的像素64

识别数字:0,1,2,3,4,5,6,7,8,9

#!/usr/bin/python

# -*- coding:utf-8 -*-

# 每个图片8x8 识别数字:0,1,2,3,4,5,6,7,8,9

import numpy as np

#加载手写阿拉伯数字数据集

from sklearn.datasets import load_digits

#对结果衡量的包

from sklearn.metrics import confusion_matrix, classification_report

#用于转化二维数字类型

from sklearn.preprocessing import LabelBinarizer

from NeuralNetwork import NeuralNetwork

#交叉运算 数据集 做拆分 训练集 测试集两部分

from sklearn.cross_validation import train_test_split

digits = load_digits()

X = digits.data #特征量

y = digits.target # class label

X -= X.min() # normalize the values to bring them into the range 0-1 把所有的值转化到 0 - 1之间

X /= X.max()

# 64 输入层和特征向量维度是一样的 10 输出层和 class label 一样 hidden layer = 100 有一定的灵活性 这个设计的比输入层多一些

nn = NeuralNetwork([64, 100, 10], 'logistic')

#对数据类型转化为 0 1 形式 每种组合 是 sklearn的要求

X_train, X_test, y_train, y_test = train_test_split(X, y)

labels_train = LabelBinarizer().fit_transform(y_train)

labels_test = LabelBinarizer().fit_transform(y_test)

print "start fitting"

#训练集特征向量 和 class label传入

nn.fit(X_train, labels_train, epochs=3000)

predictions = []

for i in range(X_test.shape[0]): #测试集每一行循环

o = nn.predict(X_test[i]) #预测标签是多少

predictions.append(np.argmax(o)) #结果0-1之间的值 选一个概率对应的整数

#y_test是预测 class label predictions 是真实 class label

print confusion_matrix(y_test, predictions) # 绘制准确率图标

print classification_report(y_test, predictions) # 输出准确率

输出

20170615135047.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值