分享

【原创】机器学习从零开始系列连载(7)——人工神经网络-Neural Network

 新用户8173JS52 2020-10-27

神经网络在维基百科上的定义是:

NN is a network inspired by biological neural networks (the central nervous systems of animals, in particular the brain) which are used to estimate or approximate functions that can depend on a large number of inputs that are generally unknown.(from wikipedia)


神经元

神经元是神经网络和SVM这类模型的基础模型和来源,它是一个具有如下结构的线性模型:



其输出模式为:



示意图如下:


神经网络的常用结构

神经网络由一系列神经元组成,典型的神经网络结构如下:


其中最左边是输入层,包含若干输入神经元,最右边是输出层,包含若干输出神经元,介于输入层和输出层的所有层都叫隐藏层,由于神经元的作用,任何权重的微小变化都会导致输出的微小变化,即这种变化是平滑的。
神经元的各种组合方式得到性质不一的神经网络结构 :


前馈神经网络


反向传播神经网络


循环神经网络


卷积神经网络


自编码器


Google DeepMind 记忆神经网络(用于AlphaGo)

一个简单的神经网络例子

假设随机变量 , 使用3层神经网络拟合该分布:
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import random
import math
import keras
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation
def gd(x,m,s):
left=1/(math.sqrt(2*math.pi)*s)
right=math.exp(-math.pow(x-m,2)/(2*math.pow(s,2)))
return left*right
def pt(x, y1, y2):
if len(x) != len(y1) or len(x) != len(y2):
print 'input error.'
return
plt.figure(num=1, figsize=(20, 6))
plt.title('NN fitting Gaussian distribution', size=14)
plt.xlabel('x', size=14)
plt.ylabel('y', size=14)
plt.plot(x, y1, color='b', linestyle='--', label='Gaussian distribution')
plt.plot(x, y2, color='r', linestyle='-', label='NN fitting')
plt.legend(loc='upper left')
plt.savefig('ann.png', format='png')
def ann(train_d, train_l, prd_d):
if len(train_d) == 0 or len(train_d) != len(train_l):
print 'training data error.'
return
model = Sequential()
model.add(Dense(30, input_dim=1))
model.add(Activation('relu'))
model.add(Dense(30))
model.add(Activation('relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mse',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(train_d,train_l,batch_size=250, nb_epoch=50, validation_split=0.2)
p = model.predict(prd_d,batch_size=250)
return p
if __name__ == '__main__':
x = np.linspace(-5, 5, 10000)
idx = random.sample(x, 900)
train_d = []
train_l = []
for i in idx:
train_d.append(x[i])
train_l.append(gd(x[i],0,1))
y1 = []
y2 = []
for i in x:
y1.append(gd(i,0,1))
y2 = ann(np.array(train_d).reshape(len(train_d), 1), np.array(train_l), np.array(x).reshape(len(x), 1))
pt(x, y1, y2.tolist())
















1.机器学习原来这么有趣!【第一章】

2.机器学习原来这么有趣!【第二章】:用机器学习制作超级马里奥的关卡

3.机器学习原来这么有趣!【第三章(上)】:深度学习和卷积神经网络

4.机器学习从零开始系列连载(1)——基本概念

5.机器学习从零开始系列连载(2)——线性回归

6.机器学习从零开始系列连载(3)——支持向量机

7.机器学习从零开始系列连载(4)——逻辑回归

8.机器学习从零开始系列连载(5)——Bagging and Boosting框架

9.机器学习从零开始系列连载(6)—— Additive Tree 模型

扫描个人微信号,

拉你进机器学习大牛群。

福利满满,名额已不多…

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多