深度学习torch基础知识

detach()

detach是截断反向传播的梯度流
将某个node变成不需要梯度的Varibale。因此当反向传播经过这个node时,梯度就不会从这个node往前面传播。

拼接函数torch.stack()

拼接:将多个维度参数相同的张量连接成一个张量

a=torch.tensor([[1,2,3],[4,5,6]])
b=torch.tensor([[10,20,30],[40,50,60]])
c=torch.tensor([[100,200,300],[400,500,600]])
print(torch.stack([a,b,c],dim=0))
print(torch.stack([a,b,c],dim=1))
print(torch.stack([a,b,c],dim=2))
print(torch.stack([a,b,c],dim=0).size())
print(torch.stack([a,b,c],dim=1).size())
print(torch.stack([a,b,c],dim=2).size())
#输出结果为:
tensor([[[  1,   2,   3],
         [  4,   5,   6]],

        [[ 10,  20,  30],
         [ 40,  50,  60]],

        [[100, 200, 300],
         [400, 500, 600]]])
tensor([[[  1,   2,   3],
         [ 10,  20,  30],
         [100, 200, 300]],

        [[  4,   5,   6],
         [ 40,  50,  60],
         [400, 500, 600]]])
tensor([[[  1,  10, 100],
         [  2,  20, 200],
         [  3,  30, 300]],

        [[  4,  40, 400],
         [  5,  50, 500],
         [  6,  60, 600]]])
torch.Size([3, 2, 3])
torch.Size([2, 3, 3])
torch.Size([2, 3, 3])

torch.nn.DataParallel()

torch.nn.DataParallel(module, device_ids=None, output_device=None, dim=0)
module即表示你定义的模型,device_ids表示你训练的device,output_device这个参数表示输出结果的device,而这最后一个参数output_device一般情况下是省略不写的,那么默认就是在device_ids[0],也就是第一块卡上。

np.clip()

a = np.arange(10)
np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8]) # a被限制在1-8之间
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 没改变a的原值

np.clip(a, 3, 6, out=a) # 修剪后的数组存入到a中
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])

torch.linspace()

函数的作用是,返回一个一维的tensor,这个张量包含了从start到end,分成steps个线段得到的向量。

torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) 
→ Tensor

例如

import torch
print(torch.linspace(3,10,5))
#tensor([ 3.0000,  4.7500,  6.5000,  8.2500, 10.0000])

type=torch.float
print(torch.linspace(-10,10,steps=6,dtype=type))
#tensor([-10.,  -6.,  -2.,   2.,   6.,  10.])

PyTorch中tensor.repeat()

当参数只有两个时,第一个参数表示的是行复制的次数,第二个参数表示列复制的次数;
当参数有三个时,第一个参数表示的是通道复制的次数,第二个参数表示的是行复制的次数,第三个参数表示列复制的次数。
(1). 对于已经存在的维度复制

import torch
 
a = torch.tensor([[1], [2], [3]])  # 3 * 1
b = a.repeat(3, 2)
print('a\n:', a)
print('shape of a', a.size())  # 原始shape = (3,1)
print('b:\n', b)
print('shape of b', b.size())  # 新的shape = (3*3,1*2),新增加的数据通过复制得到
 
'''   运行结果   '''
a:
tensor([[1],
        [2],
        [3]])
shape of a torch.Size([3, 1])  注: 原始shape =31)
b:
 tensor([[1, 1],
        [2, 2],
        [3, 3],
        [1, 1],
        [2, 2],
        [3, 3],
        [1, 1],
        [2, 2],
        [3, 3]])
shape of b torch.Size([9, 2])  新的shape =3*31*2

(2). 对于原始不存在的维度数量拓展

import torch
a = torch.tensor([[1, 2], [3, 4], [5, 6]])  # 3 * 2
b = a.repeat(3, 2, 1)   # 在原始tensor的0维前拓展一个维度,并把原始tensor的第1维扩张2倍,都是通过复制来完成的
print('a:\n', a)
print('shape of a', a.size())  # 原始维度为 (3,2)
print('b:\n', b)
print('shape of b', b.size())  # 新的维度为 (3,2*2,2*1)=(3,4,2)

'''   运行结果   '''
a:
 tensor([[1, 2],
         [3, 4],
         [5, 6]])
shape of a torch.Size([3, 2])   注:原始维度为 (32)
b:
 tensor([[[1, 2],
          [3, 4],
          [5, 6],
          [1, 2],
          [3, 4],
          [5, 6]],
 
         [[1, 2],
          [3, 4],
          [5, 6],
          [1, 2],
          [3, 4],
          [5, 6]],
 
         [[1, 2],
          [3, 4],
          [5, 6],
          [1, 2],
          [3, 4],
          [5, 6]]])
shape of b torch.Size([3, 6, 2])   新的维度为 (32*22*1=342

pytorch索引查找 index_select

anchor_w = FloatTensor(scaled_anchors).index_select(1, LongTensor([0]))

先定义了一个tensor,这里用到了linspace和view方法。
第一个参数是索引的对象,第二个参数0表示按行索引,1表示按列进行索引,第三个参数是一个tensor,就是索引的序号,比如b里面tensor[0, 2]表示第0行和第2行,c里面tensor[1, 3]表示第1列和第3列

a = torch.linspace(1, 12, steps=12).view(3, 4)
print(a)
b = torch.index_select(a, 0, torch.tensor([0, 2]))
print(b)
print(a.index_select(0, torch.tensor([0, 2])))
c = torch.index_select(a, 1, torch.tensor([1, 3]))
print(c)

-----输出结果-----
tensor([[ 1.,  2.,  3.,  4.],
        [ 5.,  6.,  7.,  8.],
        [ 9., 10., 11., 12.]])
tensor([[ 1.,  2.,  3.,  4.],
        [ 9., 10., 11., 12.]])
tensor([[ 1.,  2.,  3.,  4.],
        [ 9., 10., 11., 12.]])
tensor([[ 2.,  4.],
        [ 6.,  8.],
        [10., 12.]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值