pytorch中的矩阵变换

交换维度位置
1.transpose()

函数返回输入矩阵input的转置。交换维度dim0和dim1

torch.transpose(input, dim0, dim1, out=None) → Tensor

##交换0和1维度
x = x.transpose(0,1)

x = x.transpose(0,1)
##等价于
x.transpose_()
# 相当于x = x + 1 简化为 x+=1
2.permute()

将tesor维度换位

permute(dims) → Tensor


x = torch.randn(2,3,4)
##按照新维度生成 (3,4,2)
x = x.permute(1,2,0)

transpose和permute

矩阵reshape
1.view()
# 使用方法
x_tensor.view(*args) → Tensor


x = torch.randn(4, 4) 
x.view(2,8) 
2.reshape()

于view相同

3.区别

view()是共享内存,reshape()不共享内存

view和reshape

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值