当前位置: 首页 > news >正文

化妆品网站模板免费下载外包网络推广公司

化妆品网站模板免费下载,外包网络推广公司,wordpress优秀站点,公司网址平台有哪些本章正式开始使用pytorch的接口来实现对应的numpy的学习的过程,来学习模型的实现,我们会介绍numpy是如何学习的,以及我们如何一步步的通过torch的接口来实现简单化的过程,优雅的展示我们的代码,已经我们的代码完成的事…

本章正式开始使用pytorch的接口来实现对应的numpy的学习的过程,来学习模型的实现,我们会介绍numpy是如何学习的,以及我们如何一步步的通过torch的接口来实现简单化的过程,优雅的展示我们的代码,已经我们的代码完成的事情

numpy的线性回归

在此之前,先看看现在的numpy实现的学习的过程是什么样的

#引入计算模块
import numpy as np
from sklearn.linear_model import LinearRegressionimport torch
import torch.optim as optim
import torch.nn as nn
from torchviz import make_dot用真实的数据来生成对应的分布点的数据
true_b = 1
true_w = 2
N = 100# Data Generation
np.random.seed(42)
x = np.random.rand(N, 1)
epsilon = (.1 * np.random.randn(N, 1))
y = true_b + true_w * x + epsilon
np.rand# Shuffles the indices
idx = np.arange(N)
np.random.shuffle(idx)# Uses first 80 random indices for train
train_idx = idx[:int(N*.8)]
# Uses the remaining indices for validation
val_idx = idx[int(N*.8):]# Generates train and validation sets
x_train, y_train = x[train_idx], y[train_idx]
x_val, y_val = x[val_idx], y[val_idx]np.random.seed(42)
b = np.random.randn(1)
w = np.random.randn(1)for _ in range(1000):# Step 1 - Computes our model's predicted output - forward passyhat = b + w * x_train# Step 2 - Computing the loss# We are using ALL data points, so this is BATCH gradient# descent. How wrong is our model? That's the error!error = (yhat - y_train)# It is a regression, so it computes mean squared error (MSE)loss = (error ** 2).mean()# Step 3 - Computes gradients for both "b" and "w" parametersb_grad = 2 * error.mean()w_grad = 2 * (x_train * error).mean()# Sets learning rate - this is "eta" ~ the "n" like Greek letterlr = 0.1# Step 4 - Updates parameters using gradients and # the learning rateb = b - lr * b_gradw = w - lr * w_grad#验证,通过线性的模型直接学习
linr = LinearRegression()
linr.fit(x_train, y_train)

如上一章所说,我们的5个步骤,就是准备数据,前向传递,计算损失,计算梯度,更新参数,循环往复

pytorch的取代

张量(通常指3维)但是这里除了标量全部都是张量,为了简化。

#如下是创建张量的例子
scalar = torch.tensor(3.14159)	#张量
vector = torch.tensor([1, 2, 3]) #一维
matrix = torch.ones((2, 3), dtype=torch.float) #二维
tensor = torch.randn((2, 3, 4), dtype=torch.float)	#三维

获取到张量的shape

shape将会是我们以后将会长期用到的东西

print(tensor.size(), tensor.shape)
torch.Size([2, 3, 4]) torch.Size([2, 3, 4])

view

我们可以使用view的接口来改变一个张量的shape,注意view并不会创建新的张量。

# We get a tensor with a different shape but it still is the SAME tensor
same_matrix = matrix.view(1, 6)
# If we change one of its elements...
same_matrix[0, 1] = 2.

创建新的tensor

使用这个可以使用new_tensor和clone

# We can use "new_tensor" method to REALLY copy it into a new one
different_matrix = matrix.new_tensor(matrix.view(1, 6))
# Now, if we change one of its elements...
different_matrix[0, 1] = 3.

使用clone.detach,为什么要用detach(涉及到后面的数据存放和计算的内容)

another_matrix = matrix.view(1, 6).clone().detach()

加载数据、设备、CUDA

我们需要从numpy的数据转成tensor张量

x_train_tensor = torch.as_tensor(x_train)
x_train.dtype, x_train_tensor.dtype

注意,这里变成了torch的张量,但是这里是还是共享的原始的内存,换句话说,改了还是会一起改

(dtype('float64'), torch.float64)

cuda的数据

cuda就是要使用GPU来存储和运算的数据,我们需要手动的将其放到GPU的内存中,用于加速计算的过程

#判断是否有GPU的数据可以使用的接口,如果存在那么设置device为GPU,否则CPU
device = 'cuda' if torch.cuda.is_available() else 'cpu'
#可以获取GPU的数量
n_cudas = torch.cudsa.device_count()
for i in range(n_cuda):print(torch.cuda.get_device_naem(i))

我的机器上的输出的结果

NVIDIA GeForce RTX 2060

将数据发送到GPU上

gpu_tensor = torch.as_tensor(x_train).to(device)
gpu_tensor[0]

输出

torch.cuda.FloatTensor

如果发送到了GPU上的数据,需要重新变成numpy的数组,需要使用CPU的变量

back_to_numpy = x_train_tensor.cpu().numpy()

创建参数,并且需要梯度计算

为什么要使用torch,很多的时候在于可以自行进行梯度计算,也可以使用到很多pytorch的使用的接口和用法

# 创建一个随机1以内的参数,并且需要梯度,类型是float
b = torch.randn(1, requires_grad=True, dtype=torch.float)
w = torch.randn(1, requires_grad=True, dtype=torch.float)
print(b, w)

创建数据并且发送到GPU,但是需要注意的是,这样会丢失梯度,因为这个是CPU的数据需要梯度,发送到GPU后又是新的数据,

b = torch.randn(1, requires_grad=True, dtype=torch.float).to(device)
w = torch.randn(1, requires_grad=True, dtype=torch.float).to(device)

好的方法是直接在GPU上创建变量

b = torch.randn(1, requires_grad=True, dtype=torch.float,device = device)
w = torch.randn(1, requires_grad=True, dtype=torch.float),device = device)

Autograd

自动求解梯度

backward

我们可以通过backward直接计算和进行backward的实现,注意b,w是我们创建的参数(需要梯度的那种)

# Step 1 - Computes our model's predicted output - forward pass
yhat = b + w * x_train_tensor# Step 2 - Computes the loss
# We are using ALL data points, so this is BATCH gradient descent
# How wrong is our model? That's the error! 
error = (yhat - y_train_tensor)
# It is a regression, so it computes mean squared error (MSE)
loss = (error ** 2).mean()# Step 3 - Computes gradients for both "b" and "w" parameters
# No more manual computation of gradients! 
# b_grad = 2 * error.mean()
# w_grad = 2 * (x_tensor * error).mean()
loss.backward()

需要说明的是,所有的参与到b,w的计算的都是需要梯度的参数,例如这里面的yhat,error,loss,都是通过w,b的计算得来的,我们都认为是传递了梯度的计算特性

需要特别注意的是,这里的梯度是累计的,因为为了后续的小批量的情况,所以每次更新完毕以后需要手动设置gard_zero_()函数

b.grad.zero_(), w.grad.zero_()

还需要特别注意的是,我们更新参数的时候,是不能直接更新的,需要使用

    with torch.no_grad():b -= lr * b.gradw -= lr * w.grad

这在停一下,除了前面的变量的配置不一样的地方,我们这里已经在改造我们的numpy代码了

	# 原来的numpy的梯度的计算需要手动计算# Step 3 - Computes gradients for both "b" and "w" parametersb_grad = 2 * error.mean()w_grad = 2 * (x_train * error).mean()#但是这里已经可以使用自动计算的方法loss.backward()#可以直接读取当前的梯度的值b.gradw.grad

动态计算图

可以使用动态计算图,直接看到当前的所有的梯度的变量的相互之间的关系,这里大家可以自己看,我就不放出来了

优化器

我们之前都要手动的执行backward,然后获取b,w的grad,然后手动的进行更新使用了learning rate,最后还需要zero_grad。我们可以通过优化器的方式将变量一开始就加入到优化器中

	optimizer = optim.SGD([b,w],lr = lr)#	....执行学习的步骤loss.backward()optimizer.step()#一次性更新所有的参数的变量optimizer.zero_grad()#一次性zero所有的变量的值

损失函数

实际上,我们的损失函数也有torch的封装,可以直接使用已经配置好的损失函数
loss_fn = nn.MSELoss(reduction = ‘mean’)

损失函数直接得到我们的yhat的结果和y_lable之间的损失的值

	#原文中的损失函数的部分error = (yhat - y_train_tensor)loss = (error**2).mean()#取代后loss_fn = nn.MSELoss(reduction='mean')loss = loss_fn(y_hat,y_train_tensor)此时的loss计算出来的结果和之前的是一模一样的

需要注意的是,如果此时需要回归的numpy,需要执行detach()操作,表示Loss函数不再参与grad的计算

模型

我们已经有了优化器(用于更新参数),损失函数(用于生成损失值),我们还可以定义自己的module模型(显然吗,我们还需要构建很多的我们自己的东西)

我们使用model函数来用现有的模型针对输入得到我们的输出函数,model函数对比forward函数,还会 前向和反向的钩子函数
我们声明一个继承与Module的自己的类

class ManualLinearRegression(nn.Module):def __init__(self):super().__init__()# To make "b" and "w" real parameters of the model,# we need to wrap them with nn.Parameterself.b = nn.Parameter(torch.randn(1,requires_grad=True, dtype=torch.float))self.w = nn.Parameter(torch.randn(1, requires_grad=True,dtype=torch.float))def forward(self, x):# Computes the outputs / predictionsreturn self.b + self.w * x

通过parameters的函数,我们可以得到一个module类的目前包含的参数

dummpy = ManualLinearRegression()
list(dummy.parameters)
[Parameter containing:tensor([0.3367], requires_grad=True), Parameter containing:tensor([0.1288], requires_grad=True)]

也可以state_dict来获取所有的参数的当前值,和parameters的区别在于state_dict通常用于加载和保存模型,而前面的通常用于展示优化器的包含的变量
注意,数据和模型需要在同一个设备

dummy = ManualLinearRegression().to(device)

阶段代码

经过我们的使用自己的类以后的代码可以优化如下

# Greek letter
lr = 0.1# Step 0 - Initializes parameters "b" and "w" randomly
torch.manual_seed(42)
# Now we can create a model and send it at once to the device
model = ManualLinearRegression().to(device)# Defines a SGD optimizer to update the parameters 
# (now retrieved directly from the model)
optimizer = optim.SGD(model.parameters(), lr=lr)# Defines a MSE loss function
loss_fn = nn.MSELoss(reduction='mean')# Defines number of epochs
n_epochs = 1000for epoch in range(n_epochs):model.train() # What is this?!?# Step 1 - Computes model's predicted output - forward pass# No more manual prediction,直接使yhat = model(x_train_tensor)# 一定注意这里使用的是model而不是forward# Step 2 - Computes the lossloss = loss_fn(yhat, y_train_tensor)# Step 3 - Computes gradients for both "b" and "w" parametersloss.backward()# Step 4 - Updates parameters using gradients and# the learning rateoptimizer.step()optimizer.zero_grad()# We can also inspect its parameters using its state_dict
print(model.state_dict())

model.train()

这个是配置了训练模式,训练模式可以有很多内容,例如我们常见的dropout的优化模式来减少过拟合的问题

嵌套模型

我们可以使用了已经有的模型来作为嵌套的模型

首先我们使用nn自带的Linear来取代我们手写的线性的Module,如下是一个具有一个权重w,和一个bias的线性模型,我们完全可以用这个来取代我们之前的手写的类

linear = nn.Linear(1,1)

当然我们可以嵌套这个到我们自己的类,这个和之前的基本上是完全等价的

class MyLinearRegression(nn.Module):def __init__(self):super().__init__()# Instead of our custom parameters, we use a Linear model# with single input and single outputself.linear = nn.Linear(1, 1)def forward(self, x):# Now it only takes a callself.linear(x)

序列模型

一个好的深度学习的模型,显然不只有一层,通常都会有很多隐藏层,将所有的封装子在一起,我们可以认为是一个序列模型

# Building the model from the figure above
model = nn.Sequential(nn.Linear(3, 5), nn.Linear(5, 1)).to(device)model.state_dict()

注意这里创建了一个序列类,序列类里面有两层,第一层是一个35的输出,第二层是一个51的模型,或者我们可以通过添加层的方式来增加带名字的层

# Building the model from the figure above
model = nn.Sequential()
model.add_module('layer1', nn.Linear(3, 5))
model.add_module('layer2', nn.Linear(5, 1))
model.to(device)

这里其实已经涉及到了深度学习的部分,pytorch的层只是很多例如
卷积层、池化层、填充层、非线性激活层、归一化层、循环层、transformer层、线性层、丢弃层、稀疏层、视觉层、数据平移(多GPU),展平层

http://www.tj-hxxt.cn/news/12687.html

相关文章:

  • 网站菜单导航怎么做优化设计答案五年级下册
  • 中秋节网页设计实训报告网站seo公司哪家好
  • 贵州网站制作公司5000人朋友圈推广多少钱
  • wordpress显示分类昆明seo推广外包
  • cgi--网站开发技术的雏形深企在线
  • 比较国内外政府门户网站建设特点免费seo网站推荐一下
  • 浙江省建设厅继续教育网站首页软文代写价格
  • 做天猫还是做网站推广google关键词
  • 天津建设工程信息网投标报名平台seo博客
  • 大连网站开发师青岛网站权重提升
  • 免费网站模板网杭州seo薪资水平
  • wordpress写书主题莆田百度快照优化
  • 做投票的网站赚钱嘛大连seo网站推广
  • 遂宁市网站建设外贸网站建设推广
  • 做电影网站会被捉吗外贸展示型网站建设公司
  • 个人网站设计论文参考文献重庆网络推广公司
  • 有名做网站公司个人如何推广app
  • 基于推荐算法的网站开发网站查询是否安全
  • 网站如何做熊掌号百度seo软件优化
  • 做课题查新网站深圳新闻最新事件
  • 天津市哪里有做网站的百度官方营销推广平台加载中
  • 可以做网站的app如何做网站平台
  • 视频播放网站怎么做百度推广渠道代理
  • 天津做网站比较好的公司策划网络营销方案
  • 国外做名片的网站怎么自己做一个网页
  • 哪里有网站制作建设成人职业技能培训有哪些项目
  • 向客户介绍网站建设的话术seo分析及优化建议
  • 网站版式分类seo入门讲解
  • 网站开发效率seo点击排名器
  • 怎么才能建立自己的网站啊网页设计网站