深度学习笔记——pytorch的使用(下)

上篇博文我们安装并且学会了初步使用pytorch

深度学习笔记——pytorch的使用(上)

现在我们来开始更加深入的学习
在这里插入图片描述

标准正态分布数据的张量

import pytorch
import numpy as np
a = torch.ones((3,4))
t = torch.randn_like(a)
print(t)

tensor([[-1.3571e-01, -2.4681e-01, -7.6430e-01, -1.5986e+00],
[ 8.1159e-04, -2.8634e-01, -3.8962e-01, 1.1206e+00],
[-1.5792e+00, 1.0334e+00, -1.2379e+00, 1.3063e+00]])

torch.randint(2, 6, (4,))

tensor([4, 2, 5, 5])

torch.randint(9, (3, 2))

tensor([[7, 2],
[7, 2],
[8, 7]])

torch.randperm(6)

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

a = torch.empty(3, 3).uniform_(0, 1)
a
torch.bernoulli(a)#使用上面创建的张量a作为概率值创建伯努利分布

tensor([[0.8743, 0.3144, 0.0818],
[0.0236, 0.0744, 0.3838],
[0.5188, 0.8553, 0.4509]])
tensor([[1., 0., 0.],
[0., 0., 0.],
[1., 0., 0.]])

张量的拼接

t = torch.ones((3,2))
t0 = torch.cat([t,t],dim=0)#在第0个维度上拼接
t1 = torch.cat([t,t],dim=1)#在第1个维度上拼接
print(t0,'\n\n',t1)

tensor([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]])

tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])

t = torch.ones((3,2))
t1 = torch.stack([t,t],dim=2)#在新创建的维度上进行拼接
print(t1,t1.shape) #拼接完会从2维变成3维

tensor([[[1., 1.],
[1., 1.]],

    [[1., 1.],
     [1., 1.]],

    [[1., 1.],
     [1., 1.]]]) torch.Size([3, 2, 2])
t = torch.ones((3,2))
t1 = torch.stack([t,t],dim=0)#在新创建的维度上进行拼接
#由于指定是第0维,会把原来的3,2往后移动一格,然后在新的第0维创建新维度进行拼接
print(t1,t1.shape)

tensor([[[1., 1.],
[1., 1.],
[1., 1.]],

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

张量的切分

使用torch.chunk()切分
a = torch.ones((5,2))
t = torch.chunk(a,dim=0,chunks=2)#在5这个维度切分,切分成2个张量
for idx, t_chunk in enumerate(t):
    print(idx,t_chunk,t_chunk.shape)

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

使用torch.split()切分
a = torch.ones((5,2))
t = torch.split(a,2,dim=0)#指定了每个张量的长度为2
for idx, t_split in enumerate(t):
    print(idx,t_split,t_split.shape)#切出3个张量

0 tensor([[1., 1.], [1., 1.]]) torch.Size([2, 2])
1 tensor([[1., 1.], [1., 1.]]) torch.Size([2, 2])
2 tensor([[1., 1.]]) torch.Size([1, 2])

a = torch.ones((5,2))
t = torch.split(a,[2,1,2],dim=0)#指定了每个张量的长度为列表中的大小【2,1,2】
for idx, t_split in enumerate(t):
    print(idx,t_split,t_split.shape)#切出3个张量

0 tensor([[1., 1.], [1., 1.]]) torch.Size([2, 2])
1 tensor([[1., 1.]]) torch.Size([1, 2])
2 tensor([[1., 1.],[1., 1.]]) torch.Size([2, 2])

张量的索引

t = torch.randint(0,12,size=(4,3))
mask = t.ge(6) #ge是greater than or equal ,gt是greater than , le   lt 
t_select = torch.masked_select(t,mask)
print(t,'\n',mask,'\n',t_select)#将大于等于6的数据挑出来,返回一维张量

tensor([[ 0, 2, 5],
[ 5, 3, 6],
[ 4, 11, 6],
[ 8, 7, 5]])
tensor([[False, False, False],
[False, False, True],
[False, True, True],
[ True, True, False]])
tensor([ 6, 11, 6, 8, 7])

t = torch.randperm(10)
t1 = torch.reshape(t,(2,5))
print(t,'\n',t1)

tensor([3, 8, 0, 1, 9, 2, 6, 5, 7, 4])
tensor([[3, 8, 0, 1, 9],
[2, 6, 5, 7, 4]])

t1 = torch.reshape(t,(-1,5))# -1代表根据其他维度计算得到
print(t,'\n',t1)

tensor([3, 8, 0, 1, 9, 2, 6, 5, 7, 4])
tensor([[3, 8, 0, 1, 9],
[2, 6, 5, 7, 4]])

t = torch.randperm(10)
t[0] = 1024
print(t,'\n',t1)
print(id(t.data),id(t1.data))
#共享内存,id相同

tensor([1024, 1, 5, 2, 6, 8, 9, 7, 0, 3])
tensor([[3, 8, 0, 1, 9],
[2, 6, 5, 7, 4]])
2084622541192 2084622541192

t = torch.rand((1,2,1,1))
t1 = torch.squeeze(t)
t2 = torch.squeeze(t,dim=0)
t3 = torch.squeeze(t,dim=1)#指定的轴长度不为1,不能移除
print(t.shape,'\n',t1.shape,t2.shape,t3.shape)

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

t = torch.rand((4,3,2))
t1 = torch.transpose(t,dim0=0,dim1=1)#交换他们的第0,1维度
print(t.shape,t1.shape)

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

x = torch.randn(3,2)
print(x)
torch.t(x)

tensor([[-1.4659, 2.2305],
[ 0.0881, 1.6183],
[ 0.7413, -0.6628]])
tensor([[-1.4659, 0.0881, 0.7413],
[ 2.2305, 1.6183, -0.6628]])

学到这里,还是和上篇博文给我的感觉一样
和numpy好像
不过如果你也不熟练numpy
可以看看我的这篇博文
进阶python数据分析,python科学计算包numpy[从入门到精通]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值