torch学习第二天-基础知识

维度变换

view reshape
a = torch.rand(4,1,28,28)
print(a.view(-1,28*28).shape)
print(a.reshape(-1,28*28).shape)

在这里插入图片描述

unsqueeze ,sequeeze

增加或减少维度
sequeeze只能压缩对应维度大小为1的,不是1时则不操作

a = torch.rand(3,4)
print(a.shape)
print(a.unsqueeze(1).shape)
print(a.unsqueeze(1).squeeze(1).shape)

在这里插入图片描述

expand/repeat

expand 对对于维度进行扩充,只有在使用时,才填充值,-1表示对应维度值保持不变

a = torch.rand(4,32,14,14)
b = torch.rand(32)
b = b.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
print(b.shape)
print(b.expand(4,32,14,14).shape)
print(b.expand(4,32,14,-1).shape)

在这里插入图片描述
repeat表示对应维度copy的次数

a = torch.rand(4,32,14,14)
b = torch.rand(32)
b = b.unsqueeze(0).unsqueeze(-1).unsqueeze(-1)
print(b.shape)
print(b.repeat(4,1,1,1).shape)

拼接

cat

对应维度进行叠加

a = torch.rand(1,4,5)
b = torch.rand(2,4,5)
torch.cat([a,b],dim=0).shape

在这里插入图片描述

stack

新的维度叠加

a = torch.rand(4,5)
b = torch.rand(4,5)
torch.stack([a,b],dim=1).shape

在这里插入图片描述

运算操作

clamp

用于裁剪tensor

a = torch.randperm(10)
print(a)
a = a.clamp(5)
print(a)
a = a.clamp(5,8)
print(a)

在这里插入图片描述

统计属性

norm ,mean,sum,max,min,prod,argmax,argmin\where \gather
prob = torch.randn(4,10)
idx = prob.topk(dim=1,k=3)
label = torch.arange(10)+100
torch.gather(label.expand(4,10),dim=1,index=idx[1])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值