pytorch学习
文章平均质量分 61
pytorch版本为1.1
HMTT
这个作者很懒,什么都没留下…
展开
-
PyTorch Lightning基础入门
pytorch_ligntning的一些基础操作原创 2023-06-28 19:44:36 · 2003 阅读 · 2 评论 -
pytorch学习21:Fashion MNIST实例
介绍Fashion MNIST数据集 是kaggle上提供的一个图像分类入门级的数据集,其中包含10个类别的70000个灰度图像。如图所示,这些图片显示的是每件衣服的低分辨率(28×28像素)数据集的下载和介绍:地址步骤载入数据使用pandas载入文件中的数据预处理数据将数据划分为X、Y并创建相应的DataLoader方便使用创建网络使用三层CNN网络定义超参、损失函数和优化器损失函数选择交叉熵优化器选择Adam开始训练测试结果全部代码impor原创 2021-10-13 14:58:44 · 246 阅读 · 0 评论 -
pytorch学习20:处理结构数据实例
简介在对训练数据进行预处理时,我们有时会遇到结构化数据。结构化数据,是高度组织和整齐格式化的数据,是可以放入表格和电子表格中的数据类型,可以理解为一张表格。本次使用adult.csv数据作为样例进行模型训练,数据连接如下:https://github.com/zergtant/pytorch-handbook/blob/master/chapter5/data/adult.csv数据结构如下图:数据预处理在处理数据之前,首先要读取数据。使用pandas对数据进行读取:#读入文件df = p原创 2021-10-09 22:21:12 · 733 阅读 · 0 评论 -
pytorch学习19:pytorch下tensorboard的使用
Tensorboard是tensorflow内置的一个可视化工具,它通过将tensorflow程序输出的日志文件的信息可视化使得tensorflow程序的理解、调试和优化更加简单高效。 Tensorboard的可视化依赖于tensorflow程序运行输出的日志文件,因而tensorboard和tensorflow程序在不同的进程中运行。 TensorBoard给我们提供了极其方便而强大的可视化环境。它可以帮助我们理解整个神经网络的学习过程、数据的分布、性能瓶颈等等。安装直接安装即可,使用pip inst原创 2021-10-09 16:34:06 · 1161 阅读 · 0 评论 -
pytorch学习18:模型微调Fine tuning和固定层向量导出hook
微调的概念当我们在进行特定任务时,时常会出现训练数据不够的情况,若从头开始训练一个模型,往往效果较差。为了解决这个问题,我们可以找一个别人已经训练好的现成模型,换成自己的数据,调整一下参数,再训练一遍,这个操作就是微调(fine-tune)为什么要微调对于数据集很小的情况,从头开始训练具有千万参数的大型神经网络是是不现实的。越大的模型对数据量的要求越大,且容易产生过拟合。这是如果我们仍然想使用大型神经网络来训练数据,就需要微调已经训练好的模型。微调可以降低训练成本。如果使用导出特征向量的方法进行迁原创 2021-09-29 10:48:47 · 407 阅读 · 0 评论 -
pytorch学习17:RNN——通过sin预测cos
描述使用RNN,实现通过sin(x)sin(x)sin(x)预测cos(x)cos(x)cos(x)。这是pytorch-handbook中的一个样例,具体网址如下:pytorch-handbook/chapter3/3.3-rnn实现代码import numpy as npimport torch.nn as nnimport torchimport torch.optim as optimimport matplotlib.pyplot as plt# 解决 plt 不输出图片的原创 2021-09-11 10:21:03 · 365 阅读 · 0 评论 -
pytorch学习16:RNN简单使用
基础参数说明基础网络创建参数RNN(input_size, hidden_size)input_size:输入数据X的特征值的数目,可视为nlp中词嵌入向量的维度。hidden_size:隐藏层的神经元数量,即每个时刻输出向量 hth_tht 的维度。网络输入参数说明rnn(input, h_0)input:输入数据,一般包含三个维度(seq_len, batch, input_size),其中 seq_len 表示序列长度,batch 表示批大小。h_0:初始隐状态。网络输出参原创 2021-09-01 16:56:19 · 309 阅读 · 0 评论 -
pytorch学习15:DataSet和DataLoader
pytorch在torch.utils.data 对常用数据加载进行了封装,可用很容易实现数据读取和批量加载。DataSetDataSet 是 pytorch 提供的用于包装数据的抽象类。可以通过继承并实现其中的抽象方法来自定义DataSet。自定义DataSet要继承并实现两个成员方法:__getitem__(self, idx) :该方法需要实现通过索引获得一条数据。__len__(self) :该方法需要返回数据集的长度。DataLoaderDataLoader提供了对DataSet原创 2021-08-26 20:51:52 · 635 阅读 · 0 评论 -
pytorch学习14:使用CIFAR10数据训练LetNet模型
工具方法和一些设置这里有一个帮助显示图片的方法和解决图片不显示问题的设置。### 一些工具方法import matplotlib.pyplot as pltimport numpy as np# 如果不加则会出现如下错误,且不会显示图片# Error #15: Initializing libiomp5md.dll...import osos.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'def imshow(img): """ 展示原创 2021-08-24 22:01:09 · 432 阅读 · 0 评论 -
pytorch学习13:实现LetNet和学习nn.Module相关基本操作
用pytorch实现经典的LeNet网络。网络结构图:模型建立代码LeNet包含如下层:二维卷积层([32,32]→[6,28,28])([32,32] \rightarrow [6,28,28])([32,32]→[6,28,28]),激活函数relurelurelu池化层(→[6,14,14])(\rightarrow [6,14,14])(→[6,14,14])二维卷积层(→[16,10,10])(\rightarrow [16,10,10])(→[16,10,10]),激活函数rel原创 2021-08-21 21:27:35 · 493 阅读 · 0 评论 -
pytorch学习12:自动求导
autograd.grad示例:使用损失函数为均方差原函数形式为:y=wx+by = wx+by=wx+bimport torch.nn.functional as Fimport torchx = torch.tensor(2)# 可以在初始化时声明需要参数# 即设置 requires_grad=True# 若此时不声明,则需要执行 w.requires_grad_() 声明w = torch.full([1], 2, requires_grad=True, dtype=torch.原创 2021-08-16 21:11:32 · 195 阅读 · 0 评论 -
pytorch学习11:where 和 gather
wherewhere语法:torch.where(condition, x, y)要求:condition、x、y矩阵的阶数(shape)必须相同,返回值也是相同阶数的句子condition:条件矩阵,当元素为 True 时,填入 x 中对应的元素,否则填入 y 中对应的元素x:condition中元素为 True 时将从 x 中选取元素y:condition中元素为 False 时将从 y 中选取元素示例一:c = max(a, b)import torcha = torch.ran原创 2021-08-06 12:27:36 · 326 阅读 · 0 评论 -
pytorch学习10:统计运算
范数 normimport torcha = torch.ones(8)b = torch.ones(2, 4)c = torch.ones(2, 3, 4)print("a:\n{}".format(a))print("b:\n{}".format(b))print("c:\n{}".format(c))# norm1 = sum(|x|)print("a.norm(1): {}, b.norm(1): {}, c.norm(1): {}".format( a.norm(1原创 2021-08-03 22:07:42 · 145 阅读 · 0 评论 -
pytorch学习09:矩阵基本运算
四则运算import torcha = torch.tensor([ [1,2], [3,4]])b = torch.tensor([ [10, 20]])# 加print("torch.all(torch.eq(a+b, torch.add(a,b))):", torch.all(torch.eq(a+b, torch.add(a,b))))print("a+b:\n{}\n".format(a+b))# 减print("torch.all(原创 2021-08-03 22:03:50 · 785 阅读 · 0 评论 -
pytorch学习08:拼接与拆分
catimport torcha1 = torch.rand(4, 3, 32, 32)a2 = torch.rand(5, 3, 32, 32)a3 = torch.rand(4, 1, 32, 32)a4 = torch.rand(4, 3, 16, 32)# 要求其他维度必须相同print("torch.cat([a1, a2], dim=0): ", torch.cat([a1, a2], dim=0).shape)print("torch.cat([a1, a3],原创 2021-07-30 22:13:26 · 1074 阅读 · 0 评论 -
pytorch学习07:Broadcast广播——自动扩展
pytorch在矩阵相加时,会自动根据矩阵的阶数进行自动扩展。在一定条件下,可用让阶数不同的矩阵相加。示例:import torcha1 = torch.tensor([ [0, 0, 0], [10, 10, 10], [20, 20, 20], [30, 30, 30]])b1 = torch.tensor([ [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]])b2 = torch原创 2021-07-30 22:09:02 · 469 阅读 · 1 评论 -
pytorch学习06:Tensor维度变换
view reshape 重塑import torcha = torch.rand(4, 1, 28, 28)print("a.shape:", a.shape)print("a.view(4, 28*28):", a.view(4, 28*28).shape)print("a.view(4*28, 28):", a.view(4*28, 28).shape)print("a.view(4, 28, 28):", a.view(4, 28, 28).shape)print("a.resha原创 2021-07-28 21:58:53 · 316 阅读 · 0 评论 -
pytorch学习05:索引和切片
索引import torcha = torch.rand(4,3,28, 28)print("a.shape: ", a.shape)print("a[0].shape: ", a[0].shape)print("a[0][0].shape: ", a[0][0].shape)print("a[0, 0].shape: ", a[0, 0].shape)print("a[0, 1, 2, 3]: ", a[0, 1, 2, 3])取前N个或后N个import torcha = t原创 2021-07-28 21:54:25 · 325 阅读 · 0 评论 -
pytorch学习04:Tensor的创建
用 numpy 创建 tensor:import numpy as npimport torcha_np = np.array([2,3.3])a_tensor = torch.from_numpy(a_np)print("a_tensor: ", a_tensor)用 list 创建 tensorimport torcha_list = [2,3.3]a_torch = torch.tensor(a_list)print("a_torch: ", a_torch)print(原创 2021-07-25 22:35:11 · 579 阅读 · 4 评论 -
pytorch学习03:张量数据类型和一些操作
张量数据类型Data typedtypeCPU tensorGPU tensor32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor64-bit floating pointtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor16-bit floa原创 2021-07-23 14:29:57 · 102 阅读 · 0 评论 -
pytorch学习02:手写数字识别
描述手写数字识别是一个深度学习中的经典案例,pytorch也提供了简单的**MNIST数据集(手写数字数据集)**加载方法,很适合初学者尝试。本次在各种教程的帮助下,实现了手写数字识别。介绍Utils实现了一些方便处理数据和展示数据的方法。加载数据使用pytorch自带的方法加载数据集,并作一定的数据处理,如更改数据格式和正则化。创建网络本次使用的网络的结构为三层全连接层,其中前两层的激活函数为 ReluReluRelu,最后一层的激活函数为 SoftmaxSoftmaxSoftmax。原创 2021-07-22 13:09:11 · 510 阅读 · 0 评论 -
pytorch学习01:梯度下降实现简单线性回归
描述梯度下降算法是深度学习的一个基础概念,基本思想是利用梯度来尝试寻找使损失函数得到最小值的参数,以达到接近真实未知函数的目的。本次利用python实现简单函数的梯度下降算法。目标函数形式为:y=wx+by=wx+by=wx+b损失函数形式为:Loss=∑(wx+b−y)2/N,N为x的数量Loss=\sum{(wx + b - y)^2}/N, N为x的数量Loss=∑(wx+b−y)2/N,N为x的数量梯度下降的目的:寻找使LossLossLoss函数值最小的 www 和 bbb 。梯原创 2021-07-20 22:40:31 · 220 阅读 · 2 评论