torch进阶(2)--tensro操作

单个tensor

torch的数据类型转换

tensor.int()
tensor.long()
tensor.float()
tensor.double()
tensor.byte()  ## 转换为无符号整型
## 查看类型
tensor_a.type()

torch.arange(), torch.range()

torch.arange() 内部元素默认为整数

torch.range()内部元素默认为浮点数

t = torch.arange(7)    # t = tensor([0, 1, 2, 3, 4, 5, 6])
t = torch.range(0, 7)  # t = tensor([0., 1., 2., 3., 4., 5., 6., 7.])

正常的range(0,7)生成长度为7的list

torch.linespace(start,end,steps=100,out=None)

返回一个tensor 包含step个元素,每个元素之间间隔(end-start)/step

torch.linspace(0,1,10, dtype=torch.float32)
tensor([ 0.0000,  0.1111,  0.2222,  0.3333,  0.4444,  0.5556,  0.6667,
         0.7778,  0.8889,  1.0000])

torch.flatten(input,start_dim=0,end_dim=-1)

>>c = torch.randn(3,3)
>>tensor([[ 0.3476, -1.4443, -0.7253],
        [ 1.0562, -0.8764, -1.0724],
        [-0.3447, -0.7049, -0.0549]])
>>d = torch.flatten(c)
>>tensor([ 0.3476, -1.4443, -0.7253,  1.0562, -0.8764, -1.0724, -0.3447, -0.7049,-0.0549])

torch.repeat(input,*size)

对张量进行重复扩充

  • 当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。
  • 当参数有三个时:(通道数的重复倍数,行的重复倍数,列的重复倍数),1表示不重复。
x = torch.tensor([1,2,3]) 
x1 = x.repeat(4) 
x2 = x.repeat(4,1)
x3 = x.repeat(4,2)
x4 = x.repeat(4,2,1)
x1:
 tensor([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]) 
x2:
 tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
x3:
 tensor([[1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3]])
x4:
 tensor([[[1, 2, 3],
         [1, 2, 3]],
        [[1, 2, 3],
         [1, 2, 3]],
        [[1, 2, 3],
         [1, 2, 3]], 
        [[1, 2, 3],
         [1, 2, 3]]])

torch.split()

向量分割,生成的每个向量的长度为传入的参数值

a = [1,2,3,4,5,6,7,0,2]
a = torch.tensor(a)
a = a.unsqueeze(dim=1)
print(a.size())  # torch.Size([9, 1])
t = a.split(2)
print(t)
# (tensor([[1],[2]]), tensor([[3],[4]]), tensor([[5],[6]]), tensor([[7], [0]]), tensor([[2]]))
t = a.split([3,6])
print(t)
# (tensor([[1],[2],[3]]), tensor([[4],[5],[6], [7], [0], [2]]))

两个tensor间操作

首先生成两个4*3的数据

#start到end均匀分为num个,返回值是tensor
>>> a=t.linspace(1,23,12).reshape(4,3)
>>> a
tensor([[ 1.,  3.,  5.],
        [ 7.,  9., 11.],
        [13., 15., 17.],
        [19., 21., 23.]])

>>> b=t.linspace(2,24,12).reshape(4,3)
>>> b
tensor([[ 2.,  4.,  6.],
        [ 8., 10., 12.],
        [14., 16., 18.],
        [20., 22., 24.]])

cat

沿着某一维进行拼接,cat后数据的总维度不变

>>> c=t.cat((a,b),0) # 0表示对行拼接,1表示对列拼接,这个值不能超过a,b的最大维数
>>> c
tensor([[ 1.,  3.,  5.],
        [ 7.,  9., 11.],
        [13., 15., 17.],
        [19., 21., 23.],
        [ 2.,  4.,  6.],
        [ 8., 10., 12.],
        [14., 16., 18.],
        [20., 22., 24.]])
>>> c.shape
torch.Size([8, 3])



>>> c=t.cat((a,b),1)
>>> c
tensor([[ 1.,  3.,  5.,  2.,  4.,  6.],
        [ 7.,  9., 11.,  8., 10., 12.],
        [13., 15., 17., 14., 16., 18.],
        [19., 21., 23., 20., 22., 24.]])
>>> c.shape
torch.Size([4, 6])

Stack

增加新的维度进行堆叠,如对tensor a,b在第0个维度上stack,则会变为2×4×3的tensor;在第1个维度上stack,则会变为4×2×3的tensor。

# 测试第0个维度
>>> d=t.stack((a,b),0)
>>> d
tensor([[[ 1.,  3.,  5.],
         [ 7.,  9., 11.],
         [13., 15., 17.],
         [19., 21., 23.]],

        [[ 2.,  4.,  6.],
         [ 8., 10., 12.],
         [14., 16., 18.],
         [20., 22., 24.]]])
>>> d.shape
torch.Size([2, 4, 3])

# 测试第1个维度
>>> e=t.stack((a,b),1)
>>> e
tensor([[[ 1.,  3.,  5.],
         [ 2.,  4.,  6.]],

        [[ 7.,  9., 11.],
         [ 8., 10., 12.]],

        [[13., 15., 17.],
         [14., 16., 18.]],

        [[19., 21., 23.],
         [20., 22., 24.]]])
>>> e.shape
torch.Size([4, 2, 3])

# 测试第2个维度
>>> f=t.stack((a,b),2)
>>> f
tensor([[[ 1.,  2.],
         [ 3.,  4.],
         [ 5.,  6.]],

        [[ 7.,  8.],
         [ 9., 10.],
         [11., 12.]],

        [[13., 14.],
         [15., 16.],
         [17., 18.]],

        [[19., 20.],
         [21., 22.],
         [23., 24.]]])
>>> f.shape
torch.Size([4, 3, 2])

Tranpose

torch.transpose 只能交换2维的tensor

numpy.transpose 可以交换任意维的数组

#transpose 只能交换2维的tensor,输入三个参数会报错
## 这时可以尝试使用permute

Permute(交换多维的数组)

squeeze和unsqueeze

>>> a = a.reshape(1,-1) #reshape成1x12的
>>> a_ = a.squeeze()  #  squeeze 不写参数,默认把所有等于1的维度都给压缩了
>>>_a = a__.unsqueeze(0)  # 指定在第0维上扩展

更多操作

[Pytorch深度指南-torch与Tensor常用操作方法 - 知乎 (zhihu.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得出以下结论: 根据引用\[1\]中的推导过程,torch==1.13.0对应torchvision==0.14.0。这是因为torchtorchvision的小版本号是一一对应的,所以可以根据这个思路来确定它们的对应关系。 引用\[2\]提供了关于torch.triu()函数的信息,它是torch库中的一个函数,用于返回一个张量的上三角部分。 引用\[3\]提供了安装torchtorchvision的命令。可以使用pip命令来安装torchtorchvision的特定版本。 综上所述,根据提供的信息,无法确定torch2trt-0.4.0的相关信息。请提供更多关于torch2trt-0.4.0的信息,以便我能够为您提供更准确的答案。 #### 引用[.reference_title] - *1* [torch 1.13.0 对应的torchvision版本](https://blog.csdn.net/weixin_43301333/article/details/128060203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [torch.triu() - torch.triu_() - v1.5.0](https://blog.csdn.net/chengyq116/article/details/106877146)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [安装torch1.2.0 和 torchvision0.4.1(0.4.0没有成功..)](https://blog.csdn.net/weixin_42521185/article/details/127166109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值