Pytorch深度学习入门与实战教程2024版

吉太
• 阅读 61

学习资料1:https://pan.baidu.com/s/1Q56wzkCV5qpMUT6-xia1LA 提取码: y7m7 学习资料2:https://share.weiyun.com/nsgcbkza 密码:grban3

一、PyTorch环境的配置及安装 1.官网下载最新版Anaconda,完成后打开Anaconda Prompt,显示(base)即安装成功 2.conda create -n pytorch python=3.6建立一个命名为pytorch的环境,且环境python版本为3.6 3.conda activate pytorch激活并进入pytorch这个环境;linux:source activate pytorch 4.pip list来查看环境内安装了哪些包,可以发现并没有我们需要的pytorch 5.打开PyTorch官网,直接找到最新版pytorch指令conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch(无脑最新版就完事了。。。。老版本调了半天,最后还出问题了),打开pytorch环境,输入指令下载安装 6.检验是否安装成功。输入python,import torch不报错即pytorch安装成功。输入torch.cuda.is_available(),若返回True即机器显卡是可以被pytorch使用的(如失败,建议去英伟达官网下载更新驱动程序,并删除环境,使用各种最新版重新安装)。 7.linux服务器安装时出现环境安装不到conda/envs下,而在.conda下,进行如下操作

二、为什么选择Pytorch 简洁:PyTorch 的设计追求最少的封装,尽量避免重复造轮子。PyTorch的源码只有TensorFlow的十分之一左右,更少的抽象、更直观的设计使得PyTorch的源码十分易于阅读。 速度:PyTorch 的灵活性不以速度为代价,在许多评测中,PyTorch 的速度表现胜过 TensorFlow和Keras 等框架。 易用:PyTorch 是所有的框架中面向对象设计的最优雅的一个。PyTorch的面向对象的接口设计来源于Torch,而Torch的接口设计以灵活易用而著称。 生态丰富:PyTorch 提供了完整的文档,循序渐进的指南,此外,相关社区还在逐渐壮大。 步骤1:创建数据 Tensors张量是一种特殊的数据结构,它和数组还有矩阵十分相似。在Pytorch中,Tensors可以在gpu或其他专用硬件上运行来加速计算之外,其他用法类似Numpy。 import torch import numpy as np #直接从数据创建 data = [[1, 2], [3, 4]] x_data = torch.tensor(data) x_data.shape #全为1 x_ones = torch.ones_like(x_data) # retains the properties of x_data print(f"Ones Tensor: \n {x_ones} \n") #全为0 x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data print(f"Random Tensor: \n {x_rand} \n") #查看tensor类型 tensor = torch.rand(3, 4) print(f"Shape of tensor: {tensor.shape}“) print(f"Datatype of tensor: {tensor.dtype}”) print(f"Device tensor is stored on: {tensor.device}")

步骤2:自动梯度计算 在Pytorch中可以使用tensor进行计算,并最终可以从计算得到的tensor计算损失,并进行梯度信息。在Pytorch中主要关注正向传播的计算即可。 #x = torch.ones(2, 2, requires_grad=True) x = torch.tensor([[1, 2], [3, 4]], dtype=float, requires_grad=True) print(x) y = x + 2 print(y) print(y.grad_fn) # y就多了一个AddBackward z = y * y * 3 out = z.mean() print(z) # z多了MulBackward print(out) # out多了MeanBackward #计算公式:out = 0.25 ((x+2) * (x+2) * 3) out.backward() print(x.grad)

步骤3:拟合曲线 接下来我们将尝试使用Pytorch拟合一条曲线,我们首先的创建待你和的参数,并加载待训练的数据。 #需要计算得到的参数 w = torch.ones(1, requires_grad=True) b = torch.ones(1, requires_grad=True) #数据 x_tensor = torch.from_numpy(x) y_tensor = torch.from_numpy(y) #目标模型 #y = wx + b 定义损失 def mse(label, pred): diff = label - pred return torch.sqrt((diff ** 2).mean()) pred = x_tensor * w + b loss = mse(y_tensor, pred) #执行20次参数更新 for _ in range(20):

重新定义一下,梯度清空

w = w.clone().detach().requires_grad_(True) b = b.clone().detach().requires_grad_(True)

正向传播

pred = x_tensor * w + b

计算损失

loss = mse(y_tensor, pred) print(loss)

计算梯度

loss.backward()

点赞
收藏
评论区
推荐文章
赵颜 赵颜
5个月前
首个基于Transformer的分割检测+视觉大模型视频课程(23年新课+源码+课件)
学习资料地址1:https://pan.baidu.com/s/1mpYHRFi68lzNuA8neYI15w提取码:pwjd学习资料地址2:https://share.weiyun.com/tnVNHGMD密码:3fj7iy自动驾驶是高安全型应用,需要高
赵嬷嬷 赵嬷嬷
4个月前
[升级16章+电子书]SpringBoot+Vue3 项目实战,打造企业级在线办公系统
学习地址1:https://pan.baidu.com/s/1gx9YoT3asP0fRdlwnBzXIQ提取码:ftyi学习地址2:https://share.weiyun.com/jVSDdcBU密码:cruqf9SpringBootVue3项目实战
赵嬷嬷 赵嬷嬷
4个月前
[完结13章]一课掌握Java并发编程精髓
资料地址1:https://pan.baidu.com/s/1AcAiXR8afHlpMbdIGVkx3w提取码:7skv资料地址2:https://share.weiyun.com/VtbcAU8C密码:gmqctfJava并发编程从入门到进阶多场景实战
荀勗 荀勗
4个月前
[第2季]OpenGL-自主高性能三维GIS平台架构与实现
学习资料地址1:https://pan.baidu.com/s/13XjYIpnTsXteItmKx5NIxg提取码:c821学习资料地址2:https://share.weiyun.com/I2HekLPO密码:b36r5sOpenGL没有提供着色器编译
鲍二家的 鲍二家的
1个月前
Pytorch深度学习入门与实战(2024版)
参考资料1:https://pan.baidu.com/s/11CD2dcDaZMCMVoOW99e3A提取码:8akb参考资料2:https://share.weiyun.com/nsgcbkza密码:grban3一、什么是pytorchPyTorch是
双寿 双寿
1个月前
Pytorch深度学习入门与实战2024
学习资料1:https://pan.baidu.com/s/1rcWuaQfVEoVozj6AuaBdvA提取码:zo4u学习资料2:https://share.weiyun.com/nsgcbkza密码:grban3一、Pytorch功能PyTorch是
双寿 双寿
1个月前
C++数据开放平台实战,手把手教你做工业级项目[完结22章]
学习资料1:https://pan.baidu.com/s/15uqLwUVJxq87xlOh1cuAQ提取码:fxnk学习资料2:https://share.weiyun.com/Av8n5sen密码:rda6fw本文给大家讲解几个C的项目课程:一、
吉太 吉太
1个月前
C++数据开放平台实战,手把手教你做工业级项目(完结)
学习资料1:https://pan.baidu.com/s/1nV9Mk9J1oiOrqzVypoXTug提取码:ur4d学习资料2:https://share.weiyun.com/Av8n5sen密码:rda6fw近几年,C已经成为助力程序员走出内
吉太 吉太
1个月前
[24年新版48章]Three.js可视化系统课程WebGL
学习资料1:https://pan.baidu.com/s/17c91OQcsIyeXvEOrlfGsKg提取码:svpc学习资料2:https://share.weiyun.com/QL6JqNUT密码:hi8h6mThree.js最早由西班牙的Rica
鲍二家的 鲍二家的
4星期前
[完结17章]SpringBoot3+Vue3 开发高并发秒杀抢购系统
学习地址1:https://pan.baidu.com/s/1DRZXkQeGkrPwhVTd2ko00g提取码:gpwn学习地址2:https://share.weiyun.com/ysK13sR2密码:74m96t众所周知,作为开发新手,入行、实习、转