- 📢博客主页:盾山狂热粉的博客_CSDN博客-C、C++语言,机器视觉领域博主
- 📢努力努力再努力嗷~~~✨
💡大纲
⭕首先认识pytorch的基本数据类型:张量tensor
类型 | ||||||||
---|---|---|---|---|---|---|---|---|
CPU | torch.FloatTensor | torch.DoubleTensor | torch.HalfTensor | torch.ByteTensor | torch.CharTensor | torch.ShortTensor | torch.IntTensor | torch.LongTensor |
GPU | 例如torch.cuda.FloatTensor |
一、查看张量状态
(一)引用模块
import torch
(二)对不同维度的张量进行访问
1、Dimension 0
💡一般是一个标量,维度为0
👉取tensor里面的值
a = torch.tensor(1)
print(a) # tensor(1)
print(a.item()) # 1
⚠️.items() 这是字典里取键值对
👉输出当前类型
print(a.type()) # 这种信息更详细
print(type(a))
'''
torch.LongTensor
<class 'torch.Tensor'>
'''
👉查看设备(CPU、GPU)
print(a.device) # cpu
👉查看形状
print(a.shape) # torch.Size([])
print(a.size())
👉判断现在的数据类型是不是想要的类型,目前该类型为torch.LongTensor
print(isinstance(a,torch.FloatTensor)) # False
👉 查看维度
print(a.dim()) # 0
print(len(a.shape))
2、Dimension 1
💡(Bias;linear layer input),一般是线性层的输入
b= torch.tensor([1,2,3,4])
print(b)
print(b.dim())
print(b.type())
print(b.device)
print(b.shape) # 当前维度下有四个元素
'''
tensor([1, 2, 3, 4])
1
torch.LongTensor
cpu
torch.Size([4])
'''
3、Dimension 2
💡在模型中一般为 [batch,linear layer input]
c= torch.tensor([[1,2,3,4],[5,6,7,8]])
print(c)
print(c.dim())
print(c.type())
print(c.device)
print(c.shape)
'''
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
2
torch.LongTensor
cpu
torch.Size([2, 4])
'''
4、Dimension 3
💡看做文本:文章篇数,文章里的句子,句子里包含的字数;图像:[1, 2, 4] 单通道灰度图像 图像的高度、宽度;图 [3,224,224] 三通道彩色图像
d = torch.tensor([[[1,2,3,4],[5,6,7,8]]])
print(d)
print(d.dim())
print(d.type())
print(d.device)
print(d.shape)
print(torch.numel(d)) # 看目标数据里的元素数量
'''
tensor([[[1, 2, 3, 4],
[5, 6, 7, 8]]])
3
torch.LongTensor
cpu
torch.Size([1, 2, 4])
8
'''
5、Dimension 4
💡[B,3,224,224] 图片数据集,第一个是图片数量
二、张量的创建
(一)有初始化的
1、create from list(python)
a = torch.FloatTensor(4)
# 不指定类型创建
b=torch.tensor(4)
print(a.type())
print(b.type())
'''
torch.FloatTensor
torch.LongTensor
'''
2、根据numpy库创建
import numpy as np
a = np.array([2,3,3])
print(a)
b = torch.from_numpy(a) # 根据numpy创建,转换为tensor数据类型
print(b)
'''
[2 3 3]
tensor([2, 3, 3], dtype=torch.int32)
'''
3、torch.ones/torch.zeros/torch.eye/torch.full
c=torch.ones(3,3) # 三行三列
print(c)
print(c.shape)
print(c.dim())
'''
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
torch.Size([3, 3])
2
'''
c=torch.zeros(3,3) # 全为0
print(c)
print(c.shape)
print(c.dim())
'''
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
torch.Size([3, 3])
2
'''
c=torch.eye(3,3) # 对角矩阵
print(c)
print(c.shape)
print(c.dim())
'''
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
torch.Size([3, 3])
2
'''
c=torch.full([2,3],7)
print(c)
print(c.shape)
print(c.dim())
'''
tensor([[7, 7, 7],
[7, 7, 7]])
torch.Size([2, 3])
2
'''
4、torch.arange/torch.range 创建一维向量
a=torch.arange(0,10,2) # 创建一个一维向量
# a=torch.range(0,10) 功能一样,但是会警告
print(a)
print(a.dim())
b=torch.arange(0,10)
print(b)
'''
tensor([0, 2, 4, 6, 8])
1
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
'''
5、torch.linspace 将一定范围的数据等分
c=torch.linspace(0,10,5) # 将0~10分成四等份
print(c)
# tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])
(二)无初始化,意思并不是不赋值,而是随机赋值
💡针对没有初始化的张量类型,初始化对应的是形状
1、torch.empty 根据给定的类型创建给定大小的张量,然后随机赋值
a = torch.empty((2, 3), dtype=torch.int32, device='cpu') # 数据形状
print(a)
print(a.type())
'''
tensor([[ 929115492, 1663919669, 1701132343],
[1664167990, 778253922, 1952543859]], dtype=torch.int32)
torch.IntTensor
'''
2、torch.Tensor 给定形状创建,默认类型为torch.FloatTensor
a=torch.tensor([2,3])
print(a)
print(a.type())
b= torch.Tensor(2,3) # 与torch.tensor(2)相比,赋值是数据大小,默认是创建浮点型
print(b)
print(b.type())
c=torch.IntTensor(2,3)
print(c)
print(c.type())
d=torch.FloatTensor(2,3) # 可以简写为torch.Tensor(2,3)
print(d)
print(d.type())
'''
tensor([2, 3])
torch.LongTensor
tensor([[8.4561e-307, 1.3796e-306, 8.0663e-308],
[8.0111e-307, 6.8981e-307, 1.7802e-306]])
torch.DoubleTensor
tensor([[ 0, 53, 926443110],
[1697722722, 842281008, 845571684]], dtype=torch.int32)
torch.IntTensor
tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 1.4153e-43]], dtype=torch.float32)
torch.FloatTensor
'''
3、修改默认类型,当然只能修改为float类型的
a=torch.Tensor(2,3)
torch.set_default_tensor_type(torch.DoubleTensor)
b=torch.Tensor(2,3)
print(a.type())
print(b.type())
'''
torch.FloatTensor
torch.DoubleTensor
'''
4、随机赋值
a=torch.rand(3,3) # 创建一个3X3的二维张量类型
print(a)
b=torch.rand_like(a) # 根据a创建b
print(b)
c=torch.randint(1,10,(3,3)) # 给定数据范围为1~10的整数
print(c)
d=torch.randn(3,3) # 正态分布
print(d)
'''
tensor([[0.0458, 0.8445, 0.3981],
[0.0484, 0.1764, 0.9165],
[0.8866, 0.7467, 0.5284]])
tensor([[0.9577, 0.4366, 0.4029],
[0.2032, 0.4377, 0.9875],
[0.5898, 0.2831, 0.0097]])
tensor([[6, 1, 1],
[1, 3, 1],
[9, 4, 5]])
tensor([[ 0.2395, -0.2946, 1.3685],
[-1.6190, 0.0563, 0.0584],
[ 0.7728, 0.6980, -1.3311]])
'''
5、给定数据数量,随机打散输出
a=torch.randperm(10) # 10个数,z在0~10内打散随机
print(a)
b=torch.randperm(10)
print(b)
c=torch.randperm(6)
print(c)
'''
tensor([5, 3, 7, 9, 1, 0, 6, 8, 4, 2])
tensor([9, 4, 5, 7, 3, 0, 1, 6, 8, 2])
tensor([0, 2, 5, 4, 3, 1])
'''
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!