# ML神经网络

### 感知机与多层网络

$exmple$:

import numpy as np

# 阶跃函数
def unit_step(x):
y = x>=0
return y.astype(np.int)

# 逻辑运算
def perc_logic(x,w,theta):
y = x@w.T-theta
out = unit_step(y)
return out
x = np.array([[1,1],[0,1],[0,0],[1,0]])
# 逻辑与
w = np.array([1,1])
theta = 2
y = perc_logic(x,w,theta)

print(x.T)
print(y)

[[1 0 0 1]
[1 1 0 0]]
[1 0 0 0]


$w_i\gets w_i+\Delta w_i\\ \Delta w_i=\eta (y-\hat y)x_i$

# 感知机实现 逻辑与的学习
import numpy as np
# 阶跃函数
def unit_step(x):
y = x>=0
return y.astype(np.int)

# 创造数据集
def setdata(n):
x1 = np.random.randint(0,2,n)
x2 = np.random.randint(0,2,n)
y = np.logical_and(x1,x2)
data = [x1,x2,y]
return np.array(data).T

# 感知机
class prec:

def __init__(self,w,lr,expoch):
self.w = w
self.lr = lr
self.expoch = expoch

def forward(self,x):
y1 = x@self.w.T
h1 = unit_step(y1)
out = h1
return out

# 梯度更新
out = self.forward(x)

def run(self,x,y):
for step in range(self.expoch):

n = 1200
rate = 0.8
offset = int(n*rate)

data = setdata(n)
b = np.ones((n,1))
data =  np.hstack((data,b))

x_train,y_train,x_test,y_test = data[:offset,[0,1,-1]],data[:offset,2],data[offset:,[0,1,-1]],data[offset:,2]
y_train = y_train.reshape((offset,1))
y_test = y_test.reshape((n-offset,1))

init_w = np.zeros((1,3))
lr = 0.001
expoch = 1000

net = prec(init_w,lr,expoch)
net.run(x_train,y_train)

pre = unit_step(x_test@net.w.T)
acc = 1-np.sum(np.abs(y_test-pre))/len(y_test)

print(net.w)
print(acc)


### 误差逆传播(BP)算法

$\hat y_j^k = f(\beta_j - \theta_j)$

$E_k = \frac{1}{2}\sum_{j=1}^{l}(\hat y_j^k - y_j^k)^2$

$v \gets v + \Delta v$

$BP$ 算法在基于梯度下降(gradient descent) 策略更新时，以目标的负梯度方向对参数进行调整。例如，对于上述的 $E_k$ ,给定学习率 $\eta$ 时，有：
$\Delta w_{hj}=-\eta \frac{\partial E_k}{\partial w_{hj}}\\ \frac{\partial E_k}{\partial w_{hj}}=\frac{\partial E_k}{\partial \hat y_{j}^k} ·\frac{\partial \hat y_{j}^k}{\partial \beta_j}·\frac{\partial \beta_j}{\partial w_{hj}}\\ \frac{\partial \beta_j}{\partial w_{hj}}=b_h$

$f'(x)=f(x)·f(1-f(x))$

$g_i = -\frac{\partial E_k}{\partial \hat y_{j}^k} ·\frac{\partial \hat y_{j}^k}{\partial \beta_j}\\ =-(\hat y_j^k-y_j^k)f'(\beta_j-\theta_j)\\ =\hat y_j^k(1-\hat y_j^k)(y_j^k-\hat y_j^k)$

$\Delta w_{hj}=\eta g_ib_h$

$\Delta \theta_j = -\eta g_i\\ \Delta v_{ih} = \eta e_hx_i\\ \Delta \gamma_h=-\eta e_h\\$

$e_h = -\frac{\partial E_k}{\partial b_h}·\frac{\partial b_h}{\partial \alpha_h}\\ =b_h(1-b_h)\sum_{j=1}^{l}w_{hj}g_i$

$E = \frac{1}{m}\sum_{k=1}^{m}E_k$

• 早停

• 正则化

### 避免局部最小

• 以多组不同参数值初始化多个神经网络
• 使用“模拟退火”
• 使用随机梯度下降
• 遗传算法

### 其他常见神经网络

• $RBF$ 网络

• $ART$ 网络

• $SOM$ 网络

• 级联相关网络

• $Elman$ 网络

• $Boltzmann$

### 一个单隐层 $BP$ 算法的搭建

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')
%matplotlib inline
# 对中文的支持
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def sigmoid(x):
y = 1/(1+np.exp(-x))
return y

class SimpleNet:

def __init__(self,w,b,lr,expoch):

self.w = w
self.b = b
self.lr = lr
self.expoch = expoch

y1 = self.w[0].T@x+self.b[0]
h1 = sigmoid(y1)
y2 = self.w[1].T@h1+self.b[1]
h2 = sigmoid(y2)
out = h2
return out,h1

def loss(self,y,ylabel):

Ek = np.sum((y-ylabel)**2)/2

return Ek

gj = y*(1-y)*(ylabel-y)

eh = h1*(1-h1)*(self.w[1])@gj

def fit(self,x_train,y_train):
m = x_train.shape[-1]

for step in range(self.expoch):
ls = 0
for k in range(m):
x = np.array(x_train[:,k],ndmin=2).T
ylabel = np.array(y_train[:, k], ndmin=2).T
ls += self.loss(out,ylabel)

E = ls / m
if step%50==0:
print(E,out)

xm = 10 # 输入层样本数量
xd = 2 # 输入层样本属性（输入层神经元数量）
hq = 5 # 隐含层神经元数量
outl = 1 # 输出层神经元数量

x = np.ones((xd,xm))
y = np.ones((outl,xm))

w1 = np.zeros((xd,hq))
w2 = np.zeros((hq,outl))
w = [w1,w2]

b1 = np.zeros((hq,1))
b2 = np.zeros((outl,1))
b = [b1,b2]

net = SimpleNet(w,b,lr=0.01,expoch=100)
net.fit(x,y)


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客