PyTorch
以及如何使用PyTorch搭建模型。Logistic
回归、深度学习的优化方法、多层全连接神经网络、卷积神经网络、循环神经网络、以及生成对抗网络,最后通过实战了解深度学习前沿的研究成果。https://hulin.blog.csdn.net/article/details/107733777
https://hulin.blog.csdn.net/article/details/107746239
PyTorch
基础入手,介绍PyTorch
的处理对象、运算操作、自动求导、以及数据处理方法,接着从线性模型开始进入机器学习的内容,然后由Logistic
回归引入分类问题,介绍多层全连接神经网络、反向传播算法、各种基于梯度的优化算法、数据预处理和训练技巧,最后用PyTorch
实现多层全连接神经网络。https://hulin.blog.csdn.net/article/details/107797807
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.autograd.variable import *
x_train = np.array(
[[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], [9.779], [6.128], [7.59], [2.167], [7.042], [10.791], [5.313],
[7.997], [3.1]], dtype=np.float32)
y_train = np.array(
[[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], [3.366], [2.596], [2.53], [1.221], [2.827], [3.465], [1.65],
[2.904], [1.3]], dtype=np.float32)
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(1, 1) # input and output is 1 dimension
def forward(self, x):
out = self.linear(x)
return out
if torch.cuda.is_available():
model = LinearRegression().cuda()
else:
model = LinearRegression()
criterion = nn.MSELoss() # 使用均方差作为优化函数
optimizer = optim.SGD(model.parameters(), lr=1e-3) # 梯度下降进行优化
# 开始训练模型
num_epochs = 1000 # 迭代次数
for epoch in range(num_epochs): # 将数据变成Variable并放入计算图
if torch.cuda.is_available():
inputs = Variable(x_train).cuda()
target = Variable(y_train).cuda()
else:
inputs = Variable(x_train)
target = Variable(y_train)
# forward
out = model(inputs) # 得到网络前向传播得到的结果
loss = criterion(out, target) # 得到损失函数
# backward
# 每次做反向传播之前都要归零梯度,不然梯度会累加起来,造成结果不收敛
optimizer.zero_grad()
loss.backward() # 做反向传播和更新参数
optimizer.step()
# 在训练的过程中,各一段时间将损失函数的值打印出来,确保误差越来越小
if (epoch + 1) % 20 == 0:
# print('Epoch[{} / {}],loss:{:.6f}'.format(epoch + 1, num_epochs, loss.data[0]))
print('Epoch[{} / {}]'.format(epoch + 1, num_epochs))
print(loss.data)
# 预测结果
model.eval() # 将模型变成测试模式
predict = model(Variable(x_train)) # 将测试数据放入网络做前向传播得到结果
predict = predict.data.numpy()
plt.plot(x_train.numpy(), y_train.numpy(), 'ro', label='Original Data')
plt.plot(x_train.numpy(), predict, label='Fitting Line')
plt.show()
import torch
from torch.autograd.variable import Variable
import torch.nn as nn
import torch.optim as optim
def make_features(x):
"""
Builds features i.e a matrix with columns [x,x^2,x^3]
:param x:
:return:
"""
x = x.unsqueeze(1)
# torch.cat()实现Tensor的拼接
return torch.cat([x ** i for i in range(1, 4)], 1)
# unsqueeze(1) 是将原来的tensor大小由3变成(3,1)
w_target = torch.FloatTensor([0.5, 3, 2.4]).unsqueeze(1)
b_target = torch.FloatTensor([0.9])
def f(x):
"""
Approximated function
每次输入一个x,得到一个y的真实函数
:param x:
:return:
"""
return x.mm(w_target) + b_target[0] # x.mm(w_target) 做矩阵乘法
# 在进行训练的时候需要采样一些点,可以随机生成一些数来得到每次的训练集
def get_batch(batch_size=32):
"""
Build a batch i.e. (x,f(x)) pair.
:param batch_size:
:return:
"""
random = torch.randn(batch_size)
x = make_features(random)
y = f(x)
if torch.cuda.is_available():
return Variable(x).cuda(), Variable(y).cuda()
else:
return Variable(x), Variable(y)
# define model
class poly_model(nn.Module):
def __init__(self):
super(poly_model, self).__init__()
self.poly = nn.Linear(3, 1) # 输入3维,输出1维
def forward(self, x):
out = self.poly(x)
return out
if torch.cuda.is_available():
model = poly_model().cuda()
else:
model = poly_model()
criterion = nn.MSELoss() # 损失函数
optimizer = optim.SGD(model.parameters(), lr=1e-3) # 优化器
# 使用均方误差来衡量模型的好坏,使用随机梯度下降来优化模型
epoch = 0
while True:
# Get Data
batch_x, batch_y = get_batch()
# Forward Pass
output = model(batch_x)
loss = criterion(output, batch_y)
print_loss = loss.data
# Reset gradients
optimizer.zero_grad()
# Backward Pass
loss.backward()
# update parameters
optimizer.step()
epoch += 1
# 取出的32个点的均方误差小于0.001
if print_loss < 1e-3:
break
odds
)是指该事件发生的概率和不发生的概率的比值,比如一个事件发生的概率是P,那么该事件发生的几率是p/(1-p)
,该事件的对数几率或logit函数是:import matplotlib.pyplot as plt
# 每个数据点是一行,每行中前面两个数据表示x坐标和y坐标,最后一个数据表示其类别
with open('data.txt', 'r') as f:
data_list = f.readlines()
data_list = [i.split('\n')[0] for i in data_list]
data_list = [i.split(',') for i in data_list]
data = [(float(i[0]), float(i[1]), float(i[2])) for i in data_list]
x0 = list(filter(lambda x: x[-1] == 0.0, data))
x1 = list(filter(lambda x: x[-1] == 1.0, data))
plot_x0_0 = [i[0] for i in x0]
plot_x0_1 = [i[1] for i in x0]
plot_x1_0 = [i[0] for i in x1]
plot_x1_1 = [i[1] for i in x1]
plt.plot(plot_x0_0, plot_x0_1, 'ro', label='x_0')
plt.plot(plot_x1_0, plot_x1_1, 'bo', label='x_1')
plt.legend(loc='best')
plt.show()
import torch
from torch.autograd.variable import Variable
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
# 每个数据点是一行,每行中前面两个数据表示x坐标和y坐标,最后一个数据表示其类别
with open('data.txt', 'r') as f:
data_list = f.readlines()
data_list = [i.split('\n')[0] for i in data_list]
data_list = [i.split(',') for i in data_list]
data = [(float(i[0]), float(i[1]), float(i[2])) for i in data_list]
# 分类
x0 = list(filter(lambda x: x[-1] == 0.0, data))
x1 = list(filter(lambda x: x[-1] == 1.0, data))
plot_x0_0 = [i[0] for i in x0]
plot_x0_1 = [i[1] for i in x0]
plot_x1_0 = [i[0] for i in x1]
plot_x1_1 = [i[1] for i in x1]
plt.plot(plot_x0_0, plot_x0_1, 'ro', label='x_0')
plt.plot(plot_x1_0, plot_x1_1, 'bo', label='x_1')
plt.legend(loc='best')
x_data = [i[0] for i in data]
y_data = [i[1] for i in data]
x_data=torch.tensor(x_data)
y_data=torch.tensor(y_data)
# 定义Logistic回归模型
class LogisticRegression(nn.Module):
def __init__(self):
super(LogisticRegression, self).__init__()
self.lr = nn.Linear(2, 1)
self.sm = nn.Sigmoid()
def forward(self, x):
x = self.lr(x)
x = self.sm(x)
return x
logistic_model = LogisticRegression()
if torch.cuda.is_available():
logistic_model.cuda()
criterion = nn.BCELoss() # 二分类的损失函数
# 随机梯度下降优化函数
optimizer = torch.optim.SGD(logistic_model.parameters(), lr=1e-3, momentum=0.9)
# 训练模型
num_epochs = 50000
for epoch in range(num_epochs):
if torch.cuda.is_available():
x = Variable(x_data).cuda()
y = Variable(y_data).cuda()
else:
x = Variable(x_data)
y = Variable(y_data)
# forward
out = logistic_model(x)
loss = criterion(out, y)
print_loss = loss.data[0]
# 判断输出结果,如果大于0.5就等于1,小于0.5就等于0,通过这个来计算模型分类的准确率
mask = out.ge(0.5).float()
correct = (mask == y).sum()
acc = correct.data[0] / x.size(0)
# backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 1000 == 0:
print('*' * 10)
print('epoch {}'.format(epoch + 1))
print('loss is {:.4f}'.format(print_loss))
print('acc is {:.4f}'.format(acc))
https://hulin.blog.csdn.net/article/details/107816060
f(x,y,z)=(x+y)*z
,当然可以直接求出这个函数的微分,但是这里要使用链式法则,令q=x+y,那么f=q*z,对于这两个算式,分别求出它们的微分。q=x+y
, f=qz
,通过计算图可以将这个过程表达出来。https://hulin.blog.csdn.net/article/details/107815873
https://hulin.blog.csdn.net/article/details/107824845
https://hulin.blog.csdn.net/article/details/107825032