首页 > 其他分享 >PyTorch学习笔记

PyTorch学习笔记

时间:2022-10-10 21:47:26浏览次数:44  
标签:rand tensor torch 张量 笔记 学习 PyTorch print Size

#########################################################

有关PyTorch一些学习笔记,目前笔记并不全面,只是针对性记录一些对应地铁预测中运用的的原理,函数,方法(有些没有使用的函数也会纪录,但并不详细)。

###########################################################

一、 张量模块

1. 张量(Tensor):PyTorch最基本的操作对象。张量是基于标量、向量和矩阵概念的延伸。可以把标量视为0维张量,向量视为1维张量,矩阵视为2维张量。

2. 创建张量的方法:

    • torch.Tensor()
#从python数组构建
a = [[1, 2, 3],[4 ,5 ,6]]
x = torch.Tensor(a)
print(x)
#tensor([[1., 2., 3.],
#       [4., 5., 6.]])

3. 创建各种形式的随机数的方法

1) torch.rand()

#返回一个张量,服从区间为[0,1)的均匀分布

tensor1 = torch.rand(4)
tensor2 = torch.rand(2, 3)
print(tensor1, tensor2)
# tensor([0.7638, 0.3919, 0.9474, 0.6846]) 
# tensor([[0.3425, 0.0689, 0.6304],
#         [0.5676, 0.8049, 0.3459]])

2) torch.randn()

#返回张量服从标准正态分布(均值为0,方差为1)

tensor1 = torch.randn(5)
tensor2 = torch.randn(2, 4)
print(tensor1, tensor2)
# tensor([ 0.4315, -0.3812, 0.9554, -0.8051, -0.9421]) 
# tensor([[-0.6991, 0.0359, 1.2298, -0.1711],
#         [ 1.0056, 0.5772, 1.4460, -0.5936]])

3) torch.normal()

#torch.normal()的第一种用法为torch.normal(means, std, out=None)返回一个张量

tensor = torch.normal(mean=torch.arange(1., 11.), std= torch.arange(1, 0, -0.1))
print(tensor)
# tensor([0.0605, 2.5965, 3.3046, 4.2056, 5.0117, 6.7848, 6.3024, 7.9845, 9.4306, 9.7881])

#torch.normal()的第二种用法为torch.normal(mean, std, size*, out=None),共享均值和方程

tensor = torch.normal(2, 3, size=(1, 4))
print(tensor)
# tensor([[ 4.7555, -2.5026, -1.6333, -0.9256]])

4) torch.linspace()

tensor = torch.linspace(1, 10, steps=5)
print(tensor)
# tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])

5) torch.manual_seed()

torch.manual_seed(1)
temp1 = torch.rand(5)
print(temp1)  # tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
torch.manual_seed(1)
temp2 = torch.rand(5)
print(temp2)  # tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
temp3 = torch.rand(5)
print(temp3)  # tensor([0.7999, 0.3971, 0.7544, 0.5695, 0.4388])

6) torch.ones()、torch.zeros()、torch.eye()

#ones全为1,zeros全为0,eye是对角线为1,其余为0

tensor1 = torch.zeros(2, 3)
tensor2 = torch.ones(2, 3)
tensor3 = torch.eye(3)
print(tensor1, tensor2, tensor3)
# tensor([[0., 0., 0.],
#         [0., 0., 0.]]) 
#         tensor([[1., 1., 1.],
#         [1., 1., 1.]]) 
#         tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.]])

4. 张量基本操作

1) 改变张量的形状

x = torch.rand(3, 2)
print(x.shape)  # torch.Size([3, 2])
y = x.view(2, 3)
print(y.shape)  # torch.Size([2,3])

2) 增加和删除维度

# 增加维度
a = torch.rand(3, 4)
b = torch.unsqueeze(a, 0)
c = a.unsqueeze(0)
print(b.shape)  # torch.Size([1, 3, 4])
print(c.shape)  # torch.Size([1, 3, 4])
# 删除维度
a = torch.rand(1, 1, 3, 4)
b = torch.squeeze(a)
c = a.squeeze(1)
print(b.shape)  # torch.Size([3, 4])
print(c.shape)  # torch.Size([1, 3, 4])

3) 交换维度

a = torch.rand(1, 3, 28, 32)  # torch.Size([1, 3, 28, 32]
# 第一种方法
b = a.transpose(1, 3).transpose(1, 2)  # torch.Size([1, 28, 32, 3])
print(b.shape)

# 第二种方法
c = a.permute(0, 2, 3, 1)
print(c.shape)  # torch.Size([1, 28, 32, 3])

4) 拼接和分割

# torch.cat()拼接方法的代码如下:
a = torch.rand(1, 2)
b = torch.rand(1, 2)
c = torch.rand(1, 2)
output1 = torch.cat([a, b, c], dim=0)  # dim=0为按列拼接
print(output1.shape)  # torch.Size([3, 2])
output2 = torch.cat([a, b, c], dim=1)  # dim=1为按行拼接
print(output2.shape)  # torch.Size([1, 6])
# torch.split()分割方法的代码如下:
a = torch.rand(3, 4)
output1 = torch.split(a, 2)
print(output1)
# (tensor([[0.2540, 0.1353, 0.4933, 0.0357],
#         [0.3998, 0.7569, 0.4552, 0.5319]]), tensor([[0.3846, 0.5187, 0.4397, 0.0126]]))

output2 = torch.split(a, [2, 2], dim=1)
print(output2)
# (tensor([[0.2540, 0.1353],
#         [0.3998, 0.7569],
#         [0.3846, 0.5187]]), tensor([[0.4933, 0.0357],
#         [0.4552, 0.5319],
#         [0.4397, 0.0126]]))

5) 堆叠和分解

# torch.stack()堆叠方法的代码如下:
a = torch.rand(1, 2)
b = torch.rand(1, 2)
c = torch.rand(1, 2)
output1 = torch.stack([a, b, c], dim=0)  # dim=0为按列拼接
print(output1.shape)  # torch.Size([3, 1, 2])
output2 = torch.stack([a, b, c], dim=1)  # dim=1为按行拼接
print(output2.shape)  # torch.Size([1, 3, 2])
# torch.chunk()分解方法的代码如下:
a = torch.rand(3, 4)
output1 = torch.chunk(a, 2, dim=0)
print(output1)
# (tensor([[0.1943, 0.1760, 0.3022, 0.0746],
#         [0.5819, 0.7897, 0.2581, 0.0709]]), tensor([[0.2137, 0.5694, 0.1406, 0.0052]]))

output2 = torch.chunk(a, 2, dim=1)
print(output2)
# (tensor([[0.1943, 0.1760],
#         [0.5819, 0.7897],
#         [0.2137, 0.5694]]), tensor([[0.3022, 0.0746],
#         [0.2581, 0.0709],
#         [0.1406, 0.0052]]))

6) 索引和切片

x = torch.rand(2, 3, 4)
print(x[1].shape)  # torch.Size([3, 4])

y = x[1, 0:2, :]
print(y.shape)  # torch.Size([2, 4])

z = x[:, 0, ::2]
print(z.shape)  # torch.Size([2, 2])

7) 一些基本数学运算

# 所有元素的和
a = torch.rand(4, 3)
b = torch.sum(a)
print(b)  # tensor(6.4069)
# 所有元素的乘积
a = torch.rand(4, 3)
b = torch.prod(a)
print(b)  # tensor(2.0311e-05)
# 求所有元素的平均数
a = torch.rand(4, 3)
b = torch.mean(a)
print(b)  # tensor(0.4836)
# 求方差
a = torch.rand(4, 3)
b = torch.var(a)
print(b)  # tensor(0.0740)
# 求最大值
a = torch.rand(4, 3)
b = torch.max(a)
print(b)  # tensor(0.8765)
# 求最小值
a = torch.rand(4,3)
b = torch.min(a)
print(b)  # tensor(0.0397)

8) 向量运算和矩阵运算

包括点乘、叉乘、内积、外积

# 向量的点乘,a和b必须为一维
a = torch.Tensor([1, 2, 3])
b = torch.Tensor([1, 1, 1])
output = torch.dot(a, b)
print(output)  # 等价于 1*1+2*1+3*1,tensor(6.)
# 向量的叉乘
a = torch.Tensor([1, 2, 3])
b = torch.Tensor([1, 1, 1])
output = torch.multiply(a, b)
print(output)  # tensor([1., 2., 3.])
# 矩阵的内积
a = torch.Tensor([1, 2, 3])
b = torch.Tensor([1, 1, 1])
output = torch.inner(a, b)
print(output)  # tensor(6.)
# 矩阵的外积:矩阵乘法
a = torch.Tensor([[1, 2, 3], [4, 5, 6]])
b = torch.Tensor([[1, 1], [2, 2], [3, 3]])
output = torch.matmul(a, b)
print(output)
# tensor([[14., 14.],
#         [32., 32.]])

5. numpy转张量

a = np.ones([1, 2])
b = torch.from_numpy(a)  # 进行转换
print(a, b)  # [[1. 1.]] tensor([[1., 1.]], dtype=torch.float64)

a += 2
print(a, b)  # [[3. 3.]] tensor([[3., 3.]], dtype=torch.float64)
b += 2  # 在a改变后,b也已经改变
print(a, b)  # [[5. 5.]] tensor([[5., 5.]], dtype=torch.float64)

6. Cuda张量与CPU张量

  在深度学习过程中,GPU能起到加速作用。但是pytorch中的张量默认存放在CPU设备中,如果GPU可用,可以将张量转移到GPU中。两种方法如下:

x = torch.rand(2, 4)
print(x.device)  # cpu

# 第一种方法
x = x.cuda()
print(x.device)  # cuda:0

# 第二种方法
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
    x = x.to(device)
    print(x.device)  #cuda:0

# 转化为cpu
x = x.cpu()
print(x.device)  # cpu

二、 数据模块:Dataset和Dataloader

Dataset和DataLoader都是用来帮助我们加载数据集的两个重要工具类。Dataset用来构造支持索引的数据集。在训练时需要在全部样本中拿出小批量数据参与每次的训练,因此我们需要使用DataLoader,即DataLoader是用来在Dataset里取出一组数据(mini-batch)供训练时快速使用的。 

 

标签:rand,tensor,torch,张量,笔记,学习,PyTorch,print,Size
From: https://www.cnblogs.com/zc-dn/p/16777476.html

相关文章

  • LeetCode算法笔记 350. 两个数组的交集 II
    importjunit.framework.TestCase;importjava.util.Arrays;importjava.util.HashMap;publicclassLeetCode03extendsTestCase{/***350.两个数组......
  • JDBC学习
    title:JDBC学习excerpt:tags:[Java,MySQL]categories:[学习,Java][学习,MySQL]index_img:https://picture-store-repository.oss-cn-hangzhou.aliyuncs.com/......
  • HTML学习
    title:HTML学习excerpt:重塑!tags:[狂神,HTML,表单]categories:[学习,网站运维]index_img:https://picture-store-repository.oss-cn-hangzhou.aliyuncs.com/......
  • 《EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural
    论文题目:《EPSANet:AnEfficientPyramidSqueezeAttentionBlockonConvolutionalNeuralNetwork》 论文作者:HuZhang,KekeZu,JianLuetal.论文发表年份:2021.......
  • 推荐一个浏览器插件,一键采集付费课程到印象笔记
    前言前天上线的时候,看到同事在看文章,于是喵了一眼,看看他在看啥文章,没想到是某个机构的付费课程。我:付费课程还能在印象笔记看,怎么做到的?他:我买了课程然后用插件采集过来的......
  • 数据库设计学习
    select*fromwtpartmaster;select*fromwtpartmasterwhereida2a2=611205;select*fromwtpartwhereIDA3MASTERREFERENCE=611205;select*fromwtpart......
  • C++ Primer Plus学习笔记之预备知识
    前言个人觉得学习编程最有效的方法是阅读专业的书籍,通过阅读专业书籍可以构建更加系统化的知识体系。一直以来都很想深入学习一下C++,将其作为自己的主力开发语言。现在为......
  • Typora基本语法学习
    Markdown学习二级标题三级标题四级标题字体hello,word!hello,word!hello,word!hello,word!引用引用内容> +空格分割线(三个-号:---)(三个号:***)也是分割线,......
  • 程序员修炼之道:从小工到专家阅读笔记3
    注重实效的途径2.1重复的危害   1.DRY:系统中的每一项知识都是必须具有单一、无歧义、权威的表示   DRY:Donotrepeatyourself   2.文档与代码:你撰写文......
  • 【学习笔记】Maven
    Maven什么是Maven?maven是项目架构管理工具,用来管理项目,如自动导入jar包等。Maven下载和配置下载官网:Maven–WelcometoApacheMaven下载完成后解压到自己的环......