pytorch | contiguous() 函数

1. 背景

torch中一些操作会改变原数据,比如:narrow() view() expand() transpose()等操作,在使用transpose()进行转置操作时,pytorch并不会创建新的、转置后的tensor,而是修改了tensor中的一些属性(也就是元数据),使得此时的offset和stride是与转置tensor相对应的。转置的tensor和原tensor的内存是共享的,即改变转置后的tensor, 原先tensor中内容也会改变,而contiguous方法就类似深拷贝,使得上面这些操作不会改变元数据

2. 示例

x = torch.randn(3, 2)
y = torch.transpose(x, 0, 1)
print("修改前:")
print("x-", x)
print("y-", y)
 
print("\n修改后:")
y[0, 0] = 11
print("x-", x)
print("y-", y)

输出:修改后的 x 会随 y 的改变而改变

修改前:

x- tensor([[-1.2076, -0.5300],

[-0.0826, -1.0144],

[ 1.2097, -1.2360]])

y- tensor([[-1.2076, -0.0826, 1.2097],

[-0.5300, -1.0144, -1.2360]])

修改后:

x- tensor([[11.0000, -0.5300],

[-0.0826, -1.0144],

[ 1.2097, -1.2360]])

y- tensor([[11.0000, -0.0826, 1.2097],

[-0.5300, -1.0144, -1.2360]])

使用 conguous方法

import torch
x = torch.randn(3, 2)
y = torch.transpose(x, 0, 1).contiguous()
print("修改前:")
print("x-", x)
print("y-", y)
 
print("\n修改后:")
y[0, 0] = 11
print("x-", x)
print("y-", y)

输出: 可以看到x并没有随y的改变而改变

x- tensor([[ 1.3756, -0.1766],

[ 0.9518, -1.7000],

[-1.0423, -0.6077]])

y- tensor([[ 1.3756, 0.9518, -1.0423],

[-0.1766, -1.7000, -0.6077]])

修改后:

x- tensor([[ 1.3756, -0.1766],

[ 0.9518, -1.7000],

[-1.0423, -0.6077]])

y- tensor([[11.0000, 0.9518, -1.0423],

[-0.1766, -1.7000, -0.6077]])

3. 总结

当调用 contiguous() 时,会强制拷贝一份 tensor,让它的布局和从头创建的一模一样,使得两个 tensor 完全没有联系,类似于深拷贝

相关推荐

  1. 损失函数(目标函数

    2024-03-30 05:40:05       66 阅读
  2. 字符函数字符串函数

    2024-03-30 05:40:05       53 阅读
  3. Python函数——函数介绍

    2024-03-30 05:40:05       56 阅读
  4. 匿名函数函数

    2024-03-30 05:40:05       32 阅读
  5. linux | pause函数 、alarm函数、signal函数

    2024-03-30 05:40:05       51 阅读
  6. MySQL 条件函数/加密函数/转换函数

    2024-03-30 05:40:05       27 阅读
  7. split函数

    2024-03-30 05:40:05       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 05:40:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 05:40:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 05:40:05       87 阅读
  4. Python语言-面向对象

    2024-03-30 05:40:05       96 阅读

热门阅读

  1. 安全算法 - 加密算法

    2024-03-30 05:40:05       40 阅读
  2. ubuntu 一键安装python3.10和pip3.10

    2024-03-30 05:40:05       34 阅读
  3. 发生播放错误,即将重试 jellyfin

    2024-03-30 05:40:05       39 阅读
  4. 达梦数据库集成mybatis-plus

    2024-03-30 05:40:05       42 阅读
  5. uni-app左上角退出,嵌套iframe退出异常问题

    2024-03-30 05:40:05       34 阅读
  6. 【shell】shell 设置快捷技巧

    2024-03-30 05:40:05       36 阅读
  7. vue3项目搭建企业级

    2024-03-30 05:40:05       34 阅读
  8. (一)认识微服务

    2024-03-30 05:40:05       41 阅读