pytorch基本数据类型
1. 概念
1. 1 python与pytorch数据类型对比
pytorch是面向数据计算的加速库,没有完备的语言库,没有对应的string的对应表示方法。使用编码的方式表示:
1.2. 数据类型
1.3 查看数据类型
(1) 判断变量a的类型
data.type()
type(data)
(2) 判断变量data是否为某种类型
isinstance(data,类型)
(3) 将变量data放到GPU上
data = data.cuda()
import torch
a = torch.randn(2, 3)
print(a)
# 查看类型
print("a的类型:",a.type())
print("a的类型:",type(a))
# 判断a是否为某种类型
print("a的类型是否为torch.FloatTensor?",isinstance(a,torch.FloatTensor))
# 查看data是否部署到GPU上
data = a
print("data是否部署到GPU上?",isinstance(data,torch.cuda.FloatTensor))
data = data.cuda()
print("data是否部署到GPU上?",isinstance(data,torch.cuda.FloatTensor))
执行结果:
tensor([[ 0.4922, 0.5048, -0.5209],
[ 0.3875, -1.8049, 0.9392]])
a的类型: torch.FloatTensor
a的类型: <class 'torch.Tensor'>
a的类型是否为torch.FloatTensor? True
data是否部署到GPU上? False
data是否部署到GPU上? True
2. dim = 0
dimension为0的数据是标量。
a = torch.tensor(1.89)
print(a)
执行结果:
tensor(1.8900)
检验维度的方法总结:
len(a.shape) # 检验维度的方法,与a.dim()返回结果相同
a.dim() # 返回张量的维度
a.shape # shape属性返回张量的具体维度分量
a.size() # size()函数返回张量的具体维度分量
a = torch.tensor(1.89)
print(a)
print(len(a.shape)) # 检验维度的方法,与a.dim()返回结果相同
print(a.dim()) # 返回张量的维度
print(a.shape) # shape属性返回张量的具体维度分量
print(a.size()) # size()函数返回张量的具体维度分量
执行结果:
tensor(1.8900)
0
0
torch.Size([])
torch.Size([])
3. dim = 1
一维张量类似于一维矩阵。一维张量只有一行一列, 称为矢量。还有一个零维张量, 也称为标量。
创建一维张量的语法如下:
n= torch.tensor([Tensor elements]) # 括号中接收的是数据内容
n = torch.FloatTensor(dimension) # 括号中接收的是数据的维度
# tensor接受的是具体的数据
c = torch.tensor([1.1])
print('一维向量c,数据1个:', c) # 输出 tensor([1.1000]) 1维
c = torch.tensor([1.1, 2.2])
print('一维向量c,数据2个:', c) # 输出 tensor([1.1000, 2.2000]) 1维
# FloatTensor接受的是数据的shape
c = torch.FloatTensor(1) # dimension为1 size为1
print('随机生成的一维向量c:', c) # 输出 tensor([xxxxxx])随机生成的
c = torch.FloatTensor(2) # dimension为2 size为2
# 输出 tensor([3.2239e-25, 4.5915e-41])随机初始化两个数据
print('随机生成的二位向量c:', c)
data = np.ones(2)
print('使用numpy生成的data:', data,type(data)) # 输出 array([1., 1.]) 生成长度为2的vector
c = torch.from_numpy(data)
print('numpy类型转torch.float64:', c) # 输出 tensor([1., 1.], dtype=torch.float64) 变成了floattensor类型
d = c.numpy()
print('torch.float64类型转numpy:', d,type(d))
执行结果:
一维向量c,数据1个: tensor([1.1000])
一维向量c,数据2个: tensor([1.1000, 2.2000])
随机生成的一维向量c: tensor([1.4013e-45])
随机生成的二位向量c: tensor([1.4013e-45, 0.0000e+00])
使用numpy生成的data: [1. 1.] <class 'numpy.ndarray'>
numpy类型转torch.float64: tensor([1., 1.], dtype=torch.float64)
torch.float64类型转numpy: [1. 1.] <class 'numpy.ndarray'>
4. dim = 2
# a = torch.FloatTensor(2,3)等同
a = torch.randn(2, 3)
print('a:', a) # a tensor([[ 0.2111, 0.8824, -0.6216],
# [-1.0907, -0.3094, 0.3236]])
print('a.shape:', a.shape) # a.shape torch.Size([2, 3])
print('a.size(0):', a.size(0)) # a.size(0) 2
print('a.size(1):', a.size(1)) # a.shape[1] 3
print('a.shape[1]:', a.shape[1]) # 等同于a.size(1) a.shape[1] 3
执行结果:
a: tensor([[ 0.6290, 1.3875, -1.5185],
[-1.2137, 1.8621, 0.8359]])
a.shape: torch.Size([2, 3])
a.size(0): 2
a.size(1): 3
a.shape[1]: 3
5. dim = 3
a = torch.rand(1, 2, 3)
print('a:', a) # tensor([[[0.1648, 0.6084, 0.6563],
# [0.6411, 0.4967, 0.2643]]])
print('a.shape:', a.shape) # torch.Size([1, 2, 3])
print('a[0]:', a[0]) # tensor([[0.1648, 0.6084, 0.6563],
# [0.6411, 0.4967, 0.2643]])
print(list(a.shape)) # 把torch.Siaze([1, 2, 3])数据类型转换为列表
# [1, 2, 3]
执行结果:
a: tensor([[[0.0770, 0.3809, 0.4054],
[0.3206, 0.9724, 0.6227]]])
a.shape: torch.Size([1, 2, 3])
a[0]: tensor([[0.0770, 0.3809, 0.4054],
[0.3206, 0.9724, 0.6227]])
[1, 2, 3]
6. dim = 4
a = torch.rand(2,3,28,28)
print("a:\n",a)
print("a.shape:",a.shape)
执行结果:
a:
tensor([[[[0.1300, 0.4891, 0.6800, ..., 0.8548, 0.2793, 0.3513],
[0.1449, 0.4333, 0.3867, ..., 0.8960, 0.4040, 0.8333],
[0.9764, 0.3233, 0.7478, ..., 0.8572, 0.4800, 0.5722],
...,
[0.2126, 0.0719, 0.2089, ..., 0.5019, 0.3440, 0.7236],
[0.5851, 0.2809, 0.4755, ..., 0.7140, 0.4033, 0.2431],
[0.7239, 0.6618, 0.0441, ..., 0.0502, 0.2044, 0.6905]],
[[0.1710, 0.5416, 0.9584, ..., 0.1659, 0.8656, 0.9036],
[0.4553, 0.7823, 0.2531, ..., 0.9290, 0.6010, 0.1545],
[0.1602, 0.6007, 0.0202, ..., 0.3553, 0.4448, 0.9776],
...,
[0.7261, 0.6510, 0.6102, ..., 0.8449, 0.2696, 0.6459],
[0.4065, 0.5222, 0.3616, ..., 0.8729, 0.6026, 0.1664],
[0.1160, 0.0375, 0.0354, ..., 0.7572, 0.9090, 0.5605]],
[[0.1263, 0.3126, 0.8258, ..., 0.0879, 0.0611, 0.9958],
[0.5201, 0.6832, 0.1802, ..., 0.4769, 0.0221, 0.6487],
[0.4922, 0.7146, 0.0542, ..., 0.1366, 0.2246, 0.5955],
...,
[0.3234, 0.9851, 0.2397, ..., 0.3011, 0.1731, 0.9969],
[0.5859, 0.7228, 0.9959, ..., 0.3320, 0.4614, 0.0338],
[0.5989, 0.9367, 0.9727, ..., 0.6287, 0.4724, 0.1651]]],
[[[0.0540, 0.7606, 0.2052, ..., 0.6771, 0.5501, 0.4546],
[0.4360, 0.5958, 0.0158, ..., 0.8554, 0.9413, 0.6782],
[0.9393, 0.6479, 0.8720, ..., 0.1511, 0.9025, 0.8201],
...,
[0.9696, 0.1540, 0.9641, ..., 0.6718, 0.6302, 0.8201],
[0.2378, 0.9362, 0.5975, ..., 0.6287, 0.8701, 0.9193],
[0.5052, 0.1096, 0.4442, ..., 0.3931, 0.1119, 0.7246]],
[[0.5295, 0.8272, 0.9963, ..., 0.3646, 0.2942, 0.1629],
[0.4560, 0.2638, 0.0455, ..., 0.1083, 0.4526, 0.1506],
[0.3572, 0.4857, 0.8478, ..., 0.7020, 0.0469, 0.0169],
...,
[0.1916, 0.8945, 0.8309, ..., 0.4136, 0.7524, 0.3547],
[0.2985, 0.7242, 0.7401, ..., 0.2496, 0.2191, 0.7607],
[0.0966, 0.1309, 0.3137, ..., 0.5079, 0.6321, 0.8443]],
[[0.6646, 0.5636, 0.7058, ..., 0.4350, 0.3775, 0.6107],
[0.7481, 0.5251, 0.8457, ..., 0.9544, 0.8068, 0.8738],
[0.5628, 0.4364, 0.5534, ..., 0.4377, 0.8461, 0.6200],
...,
[0.5525, 0.4243, 0.9233, ..., 0.5216, 0.8098, 0.2003],
[0.4639, 0.3539, 0.7113, ..., 0.5354, 0.9255, 0.5415],
[0.7458, 0.5662, 0.8376, ..., 0.3349, 0.2827, 0.1254]]]])
a.shape:
torch.Size([2, 3, 28, 28])
查看tensor占用的内存量:
a = torch.rand(2,3,28,28)
print("a占用的内存:",a.numel()) # 2x3x28x28=4704
执行结果:
a占用的内存: 4704
7. 从numpy转换为tensor
8. 从list转换为tensor
9 .生成未初始化的数据
10. 设置缺省(默认)类型
11. tensor数据随机初始化方式
(1)torch.rand():产生0-1之间的数据
(2)torch.rand_like(a):a为一个tensor数据类型,产生一个和a数据shape相同的随
机tensor数据类型
(3)torch.randint(min,max,[d1,d2,d3]):产生一个shape类型为[d1,d2,d3]的
tensor数据,数据最小和最大分别为min和max
(4)torch.randn:产生一个正态分布的数据类型N(0,1),对于自定义的正态分布的数
据N(mean,std),一般需要用到torch.normal()函数,一般
需要两步步骤进行,其具体的用法如下举例所示:
12. 填充相同的数据:torch.full
生成一个全部填充相同的数据:torch.full([d1,d2,de3],a)其中填充数据为a
13. 递增或者递减函数API:arange/range
torch.arange(min,max,distance):左闭右开区间,不包含最大值
torch.range(min,max,distance):全闭区间,包含最大值,不推荐使用
14. linspace/logspace:线性空间
(1)torch.linspace(min,max,steps=data number):返回的是等间距的数据,其中
左右数据均包括,数据个数为steps,数据间隔为(max-min)/(steps-1)
(2)torch.logspace(min,max,steps=data number):返回的是10的各个线性空间次方
的数值
15. torch中一些零、一和单位张量数据生成API
16.randperm:主要是产生随机的索引值
torch.randperm(10):在[0,10),即0-9产生随机的10个索引
a=torch.rand(2,3)
b=torch.rand(2,2)
print(a)
print(b)
idx=torch.randperm(2)
print(idx) # tensor([1, 0])
a=a[idx] # a的第0、1行交换位置
b=b[idx] # b的第0、1行交换位置
print(a)
print(b)
执行结果:
tensor([[0.8464, 0.8225, 0.9956],
[0.1775, 0.7573, 0.7236]])
tensor([[0.8172, 0.1484],
[0.8056, 0.7966]])
tensor([1, 0])
tensor([[0.1775, 0.7573, 0.7236],
[0.8464, 0.8225, 0.9956]])
tensor([[0.8056, 0.7966],
[0.8172, 0.1484]])