pytorch入门--Tensor常用操作

其他相关操作:https://blog.csdn.net/qq_43923588/article/details/108007534

本篇对tensor的一些常用操作进行展示,包含:

  • torch导入numpy矩阵
  • torch导入list
  • tensor/Tensor/FloatTensor 区别
  • 未初始化的tensor
  • 默认数据类型改变
  • 随机初始tensor
  • 正态分布值初始化tensor
  • full为tensor赋相同值
  • 生成等差数列
  • 生成等分数列
  • 全0全1矩阵及对角矩阵
  • 随机打散函数randperm()

使用方法和含义均在代码的批注中给出,因为有较多的输出,所以设置输出内容的第一个值为当前print()方法所在的行

tensor常用操作

import torch
import numpy as np
import sys
loc = sys._getframe()


'''用numpy生成数据,之后导入torch使用GPU进行加速运算'''
a = np.array([2, 3.3])
aa = torch.from_numpy(a)
a1 = np.array([2, 3])
aa1 = torch.from_numpy(a1)
# 这里导入以后会保持原来的数据类型
print(loc.f_lineno, '\n', aa, '\n', aa1)


'''list中数据导入torch使用GPU进行加速运算'''
b = [1, 2, 3]
bb = torch.tensor(b)
print(loc.f_lineno, '\n', bb)


'''此处区别一下torch.tensor(b)和torch.Tensor(b)
Tensor与FloatTensor类似,接收维度作为参数(shape)生成一个对应维度的张量,同时也可以用接收现有数据
tensor()用于接收已经生成的现有数据'''
c = torch.Tensor(2, 3, 2)
cc = torch.Tensor([2, 3])
print(loc.f_lineno, '\n', c, '\n', cc)


'''申请一个未初始化的空间'''
# 一般在使用后要立马进行赋值
d = torch.empty([2, 3])
dd = torch.FloatTensor(2, 3)
# dd = torch.IntTensor(2, 3)
print(loc.f_lineno, '\n', d, '\n', dd)
# 在输出中可以看到,未经初始化的tensor的数值有的非常大,有的非常小,不利于进行运算,并可能报错
# 未初始化的tensor一般只作为一个容器,进行赋值以后才会由于运算


'''默认的tensor的类型转换
在进行初始化tensor时,系统会自动将生产的tensor转换为默认数据类型,一般为float
在增强学习中,使用的默认数据类型为double
所以有时候需要改变tensor的默认数据类型'''
e = torch.tensor([3, 2.2])
print(loc.f_lineno, '\n', e.type())
torch.set_default_tensor_type('torch.DoubleTensor')
ee = torch.tensor([3, 2.2])
print(loc.f_lineno, '\n', ee.type())


'''随机初始化的tensor'''
# 使用rand()函数进行(0:1)随机分布初始化
f = torch.rand(3, 3)
# like()函数进行对tensor的shape的复制
ff = torch.rand_like(f)
print(loc.f_lineno, '\n', f, '\n', ff)

# randint()函数,支持指定初始化值的范围(可以取左边界,不能取右边界)
# 第一个参数为最小值,第二个参数为最大值,第三个参数为shape
g = torch.randint(1, 10, [3, 3])
print(loc.f_lineno, '\n', g)


'''正态分布初始化'''
h = torch.randn(3, 3)
# 自定义正态分布的均值和方差,mean取均值(上传一个10维全0向量取均值),std为方差,也是不包含右边界
hh = torch.normal(mean=torch.full([10], 0.), std=torch.arange(1, 0, -0.1))
# 这里对均值方差进行输出
print(loc.f_lineno, '\n', torch.full([10], 0.), '\n', torch.arange(1, 0, -0.1), '\n', h, '\n', hh)


'''将tensor完全赋值为某一个元素'''
# 需要注意:如果报错的话,这里的数据类型需要设置为float   我也不知道为什么报错,不是因为改了默认类型
i = torch.full([2, 3], 5.)
ii = torch.full([], 5.)
# 因为上面吧默认类型改为了double,所以这里输出double
print(loc.f_lineno, '\n', i, '\n', ii, '\n', i.type())


'''torch生成等差数列'''
# 生成等差数列,默认公差为1
j = torch.arange(0, 10)
# 生成公差为2的等差数列
jj = torch.arange(0, 10, 2)
# 与range()相比,可以包含右边界值,并且会输出警告信息,一般不使用,会使用arange()函数代替
jjj = torch.range(0, 10)
print(loc.f_lineno, '\n', j, '\n', j, '\n', jjj)


'''torch生成等分的数列'''
# 值得注意的是,这里会包含右边界
k = torch.linspace(0, 10, steps=4)
kk = torch.linspace(0, -2, steps=10)
print(loc.f_lineno, '\n', k, '\n', kk)


'''生成全0,全1的tensor,以及生成对角矩阵'''
l = torch.zeros(3, 3)
ll = torch.ones(3, 3)
lll = torch.eye(3, 3)
# 同样ones和zeros也有_like()方法可以使用
print(loc.f_lineno, '\n', l, '\n', ll, '\n', lll)


'''随机打散函数randperm()'''
# 不包含右边界
m = torch.randperm(10)
print(loc.f_lineno, '\n', m)
# 一般用于索引操作,可以保证相应位置对其
m1 = torch.rand(2, 3)
m2 = torch.rand(2, 3)
idx = torch.randperm(2)
print(loc.f_lineno, '\n', m1, '\n', m2, '\n', m1[idx], '\n', m2[idx])

输出结果

13 
 tensor([2.0000, 3.3000], dtype=torch.float64) 
 tensor([2, 3], dtype=torch.int32)
19 
 tensor([1, 2, 3])
27 
 tensor([[[0., 0.],
         [0., 0.],
         [0., 0.]],

        [[0., 0.],
         [0., 0.],
         [0., 0.]]]) 
 tensor([2., 3.])
35 
 tensor([[0.0000e+00, 0.0000e+00, 2.8026e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]]) 
 tensor([[0., 0., 0.],
        [0., 0., 0.]])
45 
 torch.FloatTensor
48 
 torch.DoubleTensor
56 
 tensor([[0.0498, 0.7839, 0.3798],
        [0.5728, 0.9653, 0.3778],
        [0.9931, 0.9079, 0.1136]]) 
 tensor([[0.8657, 0.7780, 0.9812],
        [0.4690, 0.0896, 0.6510],
        [0.2210, 0.0278, 0.5827]])
61 
 tensor([[3, 3, 9],
        [6, 8, 6],
        [6, 2, 5]])
69 
 tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) 
 tensor([1.0000, 0.9000, 0.8000, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2000,
        0.1000]) 
 tensor([[-1.8840, -1.4429,  0.3300],
        [-1.1386,  0.4550,  0.5290],
        [-0.6208,  0.5545, -0.3362]]) 
 tensor([ 0.1460,  2.2914,  0.4966,  0.3761,  0.1462, -0.0325, -0.0465,  0.1316,
        -0.1955,  0.0358])
77 
 tensor([[5., 5., 5.],
        [5., 5., 5.]]) 
 tensor(5.) 
 torch.DoubleTensor
87 
 tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 
 tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 
 tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
94 
 tensor([ 0.0000,  3.3333,  6.6667, 10.0000]) 
 tensor([ 0.0000, -0.2222, -0.4444, -0.6667, -0.8889, -1.1111, -1.3333, -1.5556,
        -1.7778, -2.0000])
102 
 tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]) 
 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]) 
 tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
108 
 tensor([4, 7, 2, 8, 0, 3, 5, 1, 9, 6])
113 
 tensor([[0.8608, 0.4485, 0.5131],
        [0.8717, 0.8186, 0.1990]]) 
 tensor([[0.5564, 0.1326, 0.6467],
        [0.2383, 0.8949, 0.5849]]) 
 tensor([[0.8608, 0.4485, 0.5131],
        [0.8717, 0.8186, 0.1990]]) 
 tensor([[0.5564, 0.1326, 0.6467],
        [0.2383, 0.8949, 0.5849]])
        
86: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
  jjj = torch.range(0, 10)

Process finished with exit code 0

posted @ 2020-08-13 10:34  博0_oer~  阅读(65)  评论(0编辑  收藏  举报