本文目录
- 多维度特征的数据集
- 多层神经网络的计算图
- 实现糖尿病预测:
- 代码:
- 结果:
- 补充知识:
- 练习:尝试不同的激活函数
- tips:
- 代码:
- 结果:
- 学习资料
- 系列文章索引
在机器学习和数据库中处理数据的方式略有不同。在机器学习里面,拿到数据表之后,把内容分成两部分,一部分作为输入x,另一部分作为输入y。如果训练是从数据库读数据,就把x读出来构成一个矩阵,把y字段读出来构成一个矩阵,就把输入的数据集准备好了。
如下图:Anaconda的安装目录下已经给我们准备好了一些数据集,gz是linux下非常流行的压缩格式。
模型采用一层线性函数self.linear = torch.nn.Linear(8, 1),函数的输入的特征维度为8,输出的维度为1,如下图:
中间隐层越多,中间步骤越多,神经元越多,学习能力越强。与此同时,也会学会更多噪声。
import numpy as np
import torch
import matplotlib.pyplot as plt
#1.Prepare Dataset
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:, [-1]])
#2. Define Model
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
#3.Construct Loss and Optimizer
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
#4. Train Cycle
epoch_list=[]
loss_list=[]
for epoch in range(100):
# Forward
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
# Backward
optimizer.zero_grad()
loss.backward()
# Update
optimizer.step()
epoch_list.append(epoch+1)
loss_list.append(loss.item())
# 画图
plt.plot(epoch_list,loss_list)
plt.xlabel("epoch")
plt.ylabel("'loss")
plt.grid()
plt.show()
查看参数
# 参数说明
# 第一层的参数:
layer1_weight = model.linear1.weight.data
layer1_bias = model.linear1.bias.data
print("layer1_weight", layer1_weight)
print("layer1_weight.shape", layer1_weight.shape)
print("layer1_bias", layer1_bias)
print("layer1_bias.shape", layer1_bias.shape)
更改评估指标
import numpy as np
import torch
import matplotlib.pyplot as plt
# prepare dataset
xy = np.loadtxt('diabetes.csv', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1]) # 第一个‘:’是指读取所有行,第二个‘:’是指从第一列开始,最后一列不要
print("input data.shape", x_data.shape)
y_data = torch.from_numpy(xy[:, [-1]]) # [-1] 最后得到的是个矩阵
# print(x_data.shape)
# design model using class
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 2)
self.linear4 = torch.nn.Linear(2, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x)) # y hat
x = self.sigmoid(self.linear4(x)) # y hat
return x
model = Model()
# construct loss and optimizer
# criterion = torch.nn.BCELoss(size_average = True)
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
# training cycle forward, backward, update
for epoch in range(100000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
# print(epoch, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch%100000 == 99999:
y_pred_label = torch.where(y_pred>=0.5,torch.tensor([1.0]),torch.tensor([0.0]))
acc = torch.eq(y_pred_label, y_data).sum().item()/y_data.size(0)
print("loss = ",loss.item(), "acc = ",acc)
采用不同的激活函数进行训练,并画出不同激活函数的损失曲线进行比较。
- 查看激活函数及导数图像:https://dashee87.github.io/data%20science/deep%20learning/visualising-activation-functions-in-neural-networks/
- 查看pytorch提供的激活函数:https://pytorch.org/docs/stable/nn.html#non-linear-activations-weighted-sum-nonlinearity
注意:(激活函数种类很多,采用RuLU激活函数时需要注意)
现在非常流行使用RuLU激活函数,但是RuLU存在的问题是当激活函数的输入为小于0时,激活函数的梯度就变为0,不会继续更新维度,所以采用RuLU需要注意。
一般如果做分类,采用RuLU激活函数都是在前面的层数,最后一层激活函数不要使用RuLU,一般会采用sigmoid的。
数据集diabetes.csv.gz需要在anaconda中找到放在代码目录下。
激活函数最后一层统一采用sigmoid。
import numpy as np
import torch
import matplotlib.pyplot as plt
#1.Prepare Dataset
xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:, [-1]])
#2. Define Model
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.activate = torch.nn.ReLU()
def forward(self, x):
x = self.activate(self.linear1(x))
x = self.activate(self.linear2(x))
x = F.sigmoid(self.linear3(x))
return x
model = Model()
#3.Construct Loss and Optimizer
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
#4. Train Cycle
epoch_list=[]
loss_relu=[]
for epoch in range(100):
# Forward
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
# Backward
optimizer.zero_grad()
loss.backward()
# Update
optimizer.step()
epoch_list.append(epoch+1)
loss_relu.append(loss.item())
# 画图
plt.plot(epoch_list,loss_relu)
plt.xlabel("epoch")
plt.ylabel("'loss")
plt.grid()
plt.show()
教程指路:【《PyTorch深度学习实践》完结合集】 https://www.bilibili.com/video/BV1Y7411d7Ys?share_source=copy_web&vd_source=3d4224b4fa4af57813fe954f52f8fbe7
- 线性模型 Linear Model
- 梯度下降 Gradient Descent
- 反向传播 Back Propagation
- 用PyTorch实现线性回归 Linear Regression with Pytorch
- 逻辑斯蒂回归 Logistic Regression
- 多维度输入 Multiple Dimension Input
- 加载数据集Dataset and Dataloader
- 用Softmax和CrossEntroyLoss解决多分类问题(Minst数据集)
- CNN基础篇——卷积神经网络跑Minst数据集
- CNN高级篇——实现复杂网络
- RNN基础篇——实现RNN
- RNN高级篇—实现分类