一、Tensor的使用

Tensor使用

前言

在深度学习中,tensor的一些常规使用方法。

1.tensor初始化

(1)正态分布随机初始化:

import torch

x=torch.randn(2,3) #随机初始化正态分布矩阵
print("服从正态分布:",x)

结果:

x服从正态分布: tensor([[-0.4914, -0.5140, -0.1600],
                      [-0.0963,  1.3560, -1.1716]])

(2)均匀分布随机初始化:

x0=torch.rand(2,3) #随机初始化均匀分布矩阵
print("服从均匀分布:",x0)

结果:

x0服从均匀分布: tensor([[0.3321, 0.7207, 0.5049],
                       [0.1105, 0.3296, 0.3703]])

(3)未初始化矩阵:

x1=torch.empty(2,3) #未初始化的矩阵
print(x1)

结果:

tensor([[9.7107e-26, 4.5794e-41, 2.7317e-17],
        [3.0631e-41, 0.0000e+00, 0.0000e+00]])

(4)全零矩阵:

x2=torch.zeros(2,3,dtype=torch.int) #全零矩阵,整型
print(x2)

结果:

tensor([[0, 0, 0],
        [0, 0, 0]], dtype=torch.int32)

(5)浮点型全零矩阵:

x3=torch.zeros(2,3) #全零矩阵,浮点型
print(x3)

结果:

tensor([[0., 0., 0.],
        [0., 0., 0.]])

(6)随机整数的张量:

# 返回一个填充了随机整数的张量,这些整数在low和high之间均匀生成。张量的shape由变量参数size定义。
x16=torch.randint(low=0, high=3, size=(10,)).float() 
print("x16", x16)
print("x16.size()", x16.size())

结果:

x16:
tensor([0., 1., 1., 0., 2., 1., 1., 1., 2., 0.])
x16.size() torch.Size([10])
torch.randint(low, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

参数说明:
常用参数:
low ( int , optional ) – 要从分布中提取的最小整数。默认值:0。
high ( int ) – 高于要从分布中提取的最高整数。
size ( tuple ) – 定义输出张量形状的元组。

关键字参数:
generator ( torch.Generator, optional) – 用于采样的伪随机数生成器
out ( Tensor , optional ) – 输出张量。
dtype ( torch.dtype , optional) – 如果是None,这个函数返回一个带有 dtype 的张量torch.int64。
layout ( torch.layout, optional) – 返回张量的所需布局。默认值:torch.strided。
device ( torch.device, optional) – 返回张量的所需设备。默认值:如果None,则使用当前设备作为默认张量类型(请参阅torch.set_default_tensor_type())。device将是 CPU 张量类型的 CPU 和 CUDA 张量类型的当前 CUDA 设备。
requires_grad ( bool , optional ) – 如果 autograd 应该在返回的张量上记录操作。默认值:False。

2.tensor常用操作和计算

(1)拼接:

y=torch.cat((x2,x3),0) #x2和x3沿着维度0(行)拼接
print("x2和x3拼接",y)

结果:

x2和x3拼接 tensor([[0., 0., 0.],
                  [0., 0., 0.],
                  [0., 0., 0.],
                  [0., 0., 0.]])

(2)类型转换:

x4=torch.tensor([[1,2,3],[4,5,6]]) #将数组转换为tensor型
print(x4)

结果:

tensor([[1, 2, 3],
        [4, 5, 6]])

(3)获取tensor的维度:

shape=x4.size() #获取tensor的维度
print(shape)

结果:

torch.Size([2, 3])

(4)改变tensor的形状:

x5=x4.view(3,2) #对tensor形状进行改变
print(x5)

结果:

tensor([[1, 2],
        [3, 4],
        [5, 6]])

(5)改变tensor的形状(系统自动补齐对应维数):

x6=x4.view(6,-1) #-1代表系统自动补齐对应维数。2x3=6xwhat
print(x6)

结果:

x6:
tensor([[1],
        [2],
        [3],
        [4],
        [5],
        [6]])

(6)对tensor的累加(不改变原tensor):

x7=x4.add(10) #使用add函数会生成一个新的Tensor变量
print(x4)
print(x7)

结果:

x4:
tensor([[1, 2, 3],
        [4, 5, 6]])

x7:
tensor([[11, 12, 13],
        [14, 15, 16]])

(7)对tensor的累加(改变原tensor):

x4.add_(10) #add_ 函数会直接再当前Tensor变量上进行操作。所以,对于函数名末尾带有"_"的函数都是会对Tensor变量本身进行操作                 的。可以直接加到x4上面。
print(x4)

结果:

x4:
tensor([[11, 12, 13],
        [14, 15, 16]])

(8)矩阵乘法:

x8=torch.mm(x4,x4.t()) #矩阵乘法,.t()表示矩阵转置
print(x8)

结果:

x8:
tensor([[14, 32],
        [32, 77]])

(9)矩阵乘法(对应元素相乘):

x9=x4*x4 #对应元素相乘
print(x9) 

结果:

x9:
tensor([[1, 4, 9],
        [16, 25, 36]])

(10)tensor转换为numpy数组:

x10=x9.numpy() #tensor转换为numpy数组
print(x10)

结果:

x10:
[[ 1  4  9]
 [16 25 36]]

(11)tensor和numpy的同时修改:

x9.add_(1) #x9,x10共享同个内存,修改会同时修改
print(x9)
print(x10)

结果:

x9:
tensor([[2, 5, 10],
        [17, 26, 37]])

x10:
[[ 2  5 10]
 [17 26 37]]

(12)numpy数组转化为tensor:

x11=torch.from_numpy(x10) #将numpy数组转化为tensor
print(x11)

结果:

x11:
tensor([[2, 5, 10],
        [17, 26, 37]])

3.tensor高级操作

(1)tensor的分离:

x12 = torch.rand(4, 8, 6)
y1,y2 = torch.split(x12, 2, dim=0)  # 按照4这个维度去分,每大块包含2个小块
print(y1)
print(y2)

结果:

y1:
tensor([[[0.8918, 0.4364, 0.6511, 0.8244, 0.3984, 0.5426],
         [0.2489, 0.4235, 0.4493, 0.6743, 0.9311, 0.8228],
         [0.4199, 0.7178, 0.5587, 0.7832, 0.6576, 0.8264],
         [0.5352, 0.9335, 0.2206, 0.4152, 0.3060, 0.9424],
         [0.7289, 0.6198, 0.7418, 0.7039, 0.7047, 0.1248],
         [0.3417, 0.7225, 0.9813, 0.8683, 0.2179, 0.7184],
         [0.4864, 0.4811, 0.5051, 0.9715, 0.4999, 0.0637],
         [0.9034, 0.0588, 0.7027, 0.5483, 0.6213, 0.6419]],[[0.3414, 0.5122, 0.4260, 0.0428, 0.4218, 0.9041],[0.5525, 0.1075, 0.8260, 0.3867, 0.3844, 0.3433],[0.6687, 0.5185, 0.9450, 0.4325, 0.7354, 0.3118],[0.9470, 0.9220, 0.8718, 0.7349, 0.3336, 0.9530],[0.0961, 0.5187, 0.6525, 0.4687, 0.1800, 0.6127],[0.5409, 0.0937, 0.7136, 0.8211, 0.4867, 0.9242],[0.3465, 0.0219, 0.3852, 0.6576, 0.6840, 0.5362],[0.4028, 0.5359, 0.8605, 0.4685, 0.5680, 0.7666]]])

y2:
tensor([[[0.6006, 0.4552, 0.4091, 0.0777, 0.1074, 0.9909],
         [0.5459, 0.3995, 0.3266, 0.3446, 0.8427, 0.5824],
         [0.4368, 0.9603, 0.3377, 0.9057, 0.2842, 0.8762],
         [0.4042, 0.2222, 0.0052, 0.2218, 0.2483, 0.9986],
         [0.4199, 0.4003, 0.2928, 0.6533, 0.8061, 0.1384],
         [0.4629, 0.4535, 0.0572, 0.7204, 0.4043, 0.1096],
         [0.2310, 0.2723, 0.3627, 0.4364, 0.7573, 0.5383],
         [0.5036, 0.8291, 0.2171, 0.8663, 0.8845, 0.7858]],[[0.5686, 0.7343, 0.0760, 0.1289, 0.9365, 0.9152],[0.9237, 0.0922, 0.6559, 0.5756, 0.0279, 0.5999],[0.0453, 0.0433, 0.6558, 0.0437, 0.4098, 0.6749],[0.2907, 0.3682, 0.7137, 0.9764, 0.2512, 0.7986],[0.7796, 0.1292, 0.2830, 0.0039, 0.0758, 0.6491],[0.1322, 0.2943, 0.3130, 0.3497, 0.1905, 0.2106],[0.2027, 0.2638, 0.6296, 0.1494, 0.3401, 0.6082],[0.6496, 0.5802, 0.5183, 0.8513, 0.1938, 0.8242]]])

(2)对tensor求平均值:

x13=torch.randn(3)  #生成一个一维的矩阵
print(x13)
x14=torch.mean(x13) #求平均值
print(x14)

结果:

x13:
tensor([-1.4499,  1.0884, -0.3338])

x14:
tensor(-0.2318)

(3)对tensor求平方:

x15=torch.pow(x13,2) #对每一分量求平方
print(x15)

结果:

x15:
tensor([2.1021, 1.1846, 0.1114])

(4)对tensor沿着维度平均值:

# 求平均值
a=torch.randn(4,4)
print(a)
c=torch.mean(a,dim=0,keepdim=True) #沿着维度0(行)求均值
print(c)
d=torch.mean(a,dim=1,keepdim=True) #沿着维度1(列)求均值
print(d)
e=torch.mean(a,dim=[0,1],keepdim=True) #求整个二维张量的平均值
print(e)

结果:

a:
tensor([[-0.3254, -1.6652, -0.0858,  0.4234],
        [-1.1386, -0.8499, -2.5607, -1.0968],
        [-1.5071, -0.7927,  0.7504, -0.7496],
        [-1.5147, -1.0704,  0.0941, -0.4796]])

b:
tensor([[-1.1215, -1.0946, -0.4505, -0.4757]])

c:
tensor([[-0.4132],
        [-1.4115],
        [-0.5748],
        [-0.7427]])

d:
tensor([[-0.7855]])

(5)对tensor增加维度:

z=torch.arange(0,6) #创建一维整型tensor,并不包含end
p=z.view(2,3) #改变tensor形状
print(p)
q=p.unsqueeze(0) #在第零维增加一个维度
print(q)
print(q.size())

结果:

p:
tensor([[0, 1, 2],
        [3, 4, 5]])

q:
tensor([[[0, 1, 2],
         [3, 4, 5]]])

torch.Size([1, 2, 3])

(5)对tensor去掉维度:

t=q.squeeze(0) #去掉第零维度(只能去掉维度为1的维度)
print(t)
print(t.size())

结果:

t:
tensor([[0, 1, 2],
        [3, 4, 5]])
torch.Size([2, 3])

4.Tensor的切片操作

(1)切片的二维操作:

import torch
a=torch.Tensor([[0,0,0],[-1,1,0],[2,3,4]])
print(a)
b=a[:,0] #取二维tensor中第1维(列)第0列的所有数据。
print(b)
c=a[:,2] #取二维tensor中第1维(列)第2列的所有数据。
print(c)
d=a[:,0:2] #取二维tensor第1维(列)第0列到第1列的所有数据(不含末尾)。
print(d)

结果:

a:
tensor([[0., 0., 0.],
        [-1., 1., 0.],
        [2., 3., 4.]])

b:
tensor([ 0., -1., 2.])

c:
tensor([0., 0., 4.])

d:
tensor([[ 0., 0.],
        [-1., 1.],
        [ 2., 3.]])

(2)切片的三维操作:

e=torch.Tensor([[[0,0,0],[-1,1,0],[2,3,4]],[[1,-2,0],[-1,3,0],[2,1,4]]])
print(e)
f=e[:,:,0] #取三维tensor中第2维(列)第0列的所有数据。
print(f)
g=e[:,:,0:2] #取三维tensor中第2维(列)第0列到第1列的所有数据(不含末尾)。
print(g)

结果:

e:
tensor([[[0., 0., 0.],
         [-1., 1., 0.],
         [2., 3., 4.]],[[1., -2., 0.],[-1., 3., 0.],[ 2., 1., 4.]]])

f:
tensor([[0., -1., 2.],
        [1., -1., 2.]])

g:
tensor([[[0., 0.],
         [-1., 1.],
         [ 2., 3.]],[[1., -2.],[-1., 3.],[2., 1.]]])

(3)切片的四维操作:

h=torch.Tensor([[[[0,0,0],[-1,1,0],[2,3,4]],[[1,-2,0],[-1,3,0],[2,1,4]]]])
print(h)
i=h[:,:,:,0] #取四维tensor中第3维(列)第0列的所有数据。
print(i)
j=h[:,:,:,0:2] #取四维tensor中第3维(列)第0列到第1列的所有数据(不含末尾)。
print(j)
k=h[:,:,0,:] #取四维tensor中两个channel的第0行。
print(k)
l=h[:,:,0:2,:] #取四维tensor中两个channel的第0行和第1行。
print(l)
m=h[:,:,1:3,:] #取四维tensor中两个channel的第1行和第2行。
print(m)
n=m-l  #两个4维tensor求差。
print(n)
o=torch.pow(n,2) #对tensor中每一个数据求平方.
print(o)
p=o.sum() #将tensor中所有数据求和.
print(p)

结果:

h:
tensor([[[[0., 0., 0.],
          [-1., 1., 0.],
          [2., 3., 4.]],[[1., -2., 0.],[-1., 3., 0.],[2., 1., 4.]]]])

i:
tensor([[[0., -1., 2.],
         [1., -1., 2.]]])

j:
tensor([[[[0., 0.],
          [-1., 1.],
          [2., 3.]],[[1., -2.],[-1., 3.],[2., 1.]]]])

k:
tensor([[[0., 0., 0.],
         [1., -2., 0.]]])

l:
tensor([[[[0., 0., 0.],
          [-1., 1., 0.]],[[1., -2., 0.],[-1., 3., 0.]]]])

m:
tensor([[[[-1.,  1.,  0.],
          [ 2.,  3.,  4.]],[[-1.,  3.,  0.],[ 2.,  1.,  4.]]]])

n:
tensor([[[[-1., 1., 0.],
          [3., 2., 4.]],[[-2., 5., 0.],[3., -2., 4.]]]])

o:
tensor([[[[1., 1., 0.],
          [9., 4., 16.]],[[4., 25., 0.],[9., 4., 16.]]]])

p:
tensor(89.)
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值