DEEP LEARNING · 04
认识人工神经网络、激活函数、网络搭建与参数初始化。
- 神经网络
- 激活函数
神经网络基础
1.神经网络概念

神经网络:⼀种模仿⽣物神经⽹络结构和功能的计算模型,由神经元构成(加权求和+激活函数)
神经网络的构成:输入层 | 隐藏层 | 输出层
全连接神经网络:第N层的每个神经元和第N-1层的所有神经元相连,同一层神经元之间没有连接
每一个神经元内部:加权求和 + 激活函数
2.激活函数
激活函数用于:
对每层的输出数据进行变换, 进而为整个网络注入了非线性因素
2.1 sigmoid

sigmoid 把值映射到0,1 适合在输出层做二分类
值域 0
1 00.25作用 输出层 二分类
2.2 tanh

值域 -1
1 01作用 隐藏层 浅层
2.3Relu

值域 0~+∞ 0~1
作用 隐藏层 深层/浅层
训练优先用relu
2.4SoftMax
将内容转换为概率,所有概率求和==1
作用 输出层 多分类
3.搭建神经网络
3.1 搭建流程
1.创建一个类,继承nn.Module
Class net(torch.nn.Module)
2.定义一个init方法
def init(self):
1.调用父类init方法
super(Net,self)——init——()
2.搭建神经网络
2.1 搭建隐藏层 – 任意个
self.fc1 = torch.nn.Linear(3,3) # (输入层神经元数量,本层神经元数量)
self.fc1 = torch.nn.Linear(3,5) #(上一层神经元数量,本层神经元数量)
2.2 搭建输出层 – 一个
self.out = torch.nn.Linear(5,2)(上一层神经元数量,输出数量)
2.3初始化参数
2.3.1 初始化权重
torch.nn.init.kaiming_uniform_(self.fc1.weight)
torch.nn.init.xavier_normal(self.fc2.weight)
kaiming_normal – ReLu使用
kaiming_uniform
xavier_uniform_ – 除了ReLu都使用
xavier_normal_
2.3.2 初始化偏置
torch.nn.init.ones_(self.fc1.bias)
torch.nn.init.zeros_(self.fc2.bias)
zeros_/ones
3.定义一个forward方法
def forward(self,x)
3.1 训练神经元(加权求和+激活函数)
#隐藏层
x1 =torch.relu.(self.fc1(x)) 加权平均输入的x,再用relu激活
x2 =torch.relu(self.fc2(x2)) 加权平均上一层结果x1,再用rulu激活
#输出层
result = torch.sigmod(self.out(x2)) 直接将x2处理后输出
return result
import torch.nn
import torchsummary as summary
# 1.搭建神经网络
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
# 搭建每一层神经网络
self.fc1 = torch.nn.Linear(3, 3)
self.fc2 = torch.nn.Linear(3, 2)
self.out = torch.nn.Linear(2, 2)
# 初始化参数
# 初始化权重
torch.nn.init.xavier_uniform_(self.fc1.weight)
torch.nn.init.kaiming_uniform_(self.fc2.weight)
# 初始化偏执
torch.nn.init.ones_(self.fc1.bias)
torch.nn.init.ones_(self.fc2.bias)
def forward(self, x): #正向传播
# 加权求和+激活函数
x1 = torch.relu(self.fc1(x))
x2 = torch.relu(self.fc2(x1))
output = torch.sigmoid(self.out(x2))
return output
if __name__ == '__main__':
#模型预测
# 1.方法实例化
model =Net()
# 2.编造数据
data = torch.randn(2, 3)
# 3.预测
output = model(data)
# 4.查看模型参数w和b
summary.summary(model, (3,),batch_size=2)
# 5.查看模型具体参数
for name, param in model.named_parameters():
print(name, param)
3.2初始化参数
初始化参数:
全0 torch.nn.init.zeros_(self.xxx)
全1 torch.nn.init.ones_(self.xxx)
指定 torch.nn.init.constant_(self.xxx)
随机 torch.nn.init.uniform_()
均匀 torch.nn.init.normal_()
kaiming 凯明均匀,凯明随机
xavier 均匀,随机
# 1.参数初始化指的是初始化神经网络中每个层的权重和偏执
# 2.我们当前只关注每个层的权重,不关心偏置
# 3.需要创建一个隐藏层+设置隐藏层的权重
import torch
# 1.创建隐藏层
fc1 = torch.nn.Linear(3,5)
# 神经网络的权重都是随机初始化生成的
print(fc1.weight)
# 也可以修改权重
# torch.nn.init_.方法(层)
# 1.均匀分布(0,1)
torch.nn.init.uniform_(fc1.weight)
print(fc1.weight)
# 2.正态分布(-3,3)
torch.nn.init.normal_(fc1.weight)
# 3.全0--掌握
torch.nn.init.zeros_(fc1.weight)
# 4.全1
torch.nn.init.ones_(fc1.weight)
# 5.固定
torch.nn.init.constant_(fc1.weight, 1.0)
# 6.kaiming-均匀分布
torch.nn.init.kaiming_uniform_(fc1.weight)
#7. kaiming-随机分布--掌握
torch.nn.init.kaiming_normal_(fc1.weight)
# 8. xavier-均匀分布
torch.nn.init.xavier_uniform_(fc1.weight)
# 9. xavier-随机分布--掌握
torch.nn.init.xavier_normal_(fc1.weight)