所看书籍为人民邮电出版社的深度学习入门,原作者[日]斋藤康毅
# _*_ coding:UTF-8 _*_
# 文件名:neural_network_01.py
# 开发工具:PyCharm
import numpy as np
import matplotlib.pylab as plt
"""
输入层 中间层 输出层
x_1
↗ ↘
↗ ↘
x1 ------→ x_2-----→↘output
↘ ↗ ↗
↗↘ ↗
x2 ↘ ↗
↓ ↘ ↗
↓--------→ x_3
第0层 第1层 第2层
中间层(也称为隐藏层),其中的神经元一般是肉眼看不到的,而输入层和输出层可以
激活函数对输入信息进行处理并输出结果
一般激活函数会用sigmoid函数来实现
h(x) = 1/(1+e的-x次方)
激活函数必须使用非线性函数,如果使用线性函数,那么层数的多少就没有意义了,
比如h(x) = cx 如果是三层神经网络,那就是c*c*c*x即c三次方x,可以简化为一层神经网络
ReLU函数
当输入大于0时,输出原值,如果小于等于0则返回0
"""
def step_function(x):
"""
阶跃函数,会在某个点突然上升
:rtype list[int]
将大于0的数变为 1
"""
return np.array(x > 0, dtype=np.int)
x = np.arange(-5.0, 5.0, 0.1) # 以0.1间距生成-5~5之间数
y = step_function(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()
def sigmoid(x):
"""
sigmoid函数
:param x: list[int]
:return: list[int]
"""
return 1 / (1 + np.exp(-x))
y = sigmoid(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()
def relu(x):
"""
ReLU函数
:param x: list[int]
:return: list[int]
"""
return np.maximum(0, x)
y = relu(x)
plt.plot(x, y)
plt.ylim(-1, 5)
plt.show()