PyTorch 中contiguous函数使用详解和代码演示

在 PyTorch 中,contiguous() 是一个用于 张量内存布局优化 的函数。它的作用是在需要时返回一个内存布局为连续(contiguous)的张量,常用于 transpose、permute 等操作后。


一、为什么需要 contiguous()

PyTorch 的张量是以 行优先(row-major)顺序 存储的。当你对张量使用 transpose()permute() 等操作时,虽然张量的维度看起来改变了 ,但底层的内存并没有重新排列,只是修改了索引方式。

一些 PyTorch 函数(如 .view())要求输入张量必须是 连续的内存块,否则就会报错。


二、函数定义与用法

python 复制代码
Tensor.contiguous(memory_format=torch.contiguous_format) → Tensor

返回值:

返回一个与当前张量具有相同数据但在内存中 连续排列 的副本。如果当前张量已经是连续的,就直接返回自身。


三、典型使用场景

1. view() 前需要 .contiguous()

python 复制代码
x = torch.randn(2, 3, 4)
y = x.permute(1, 0, 2)  # 改变维度顺序
z = y.contiguous().view(3, 8)  # 安全 reshape

如果不加 .contiguous()

python 复制代码
z = y.view(3, 8)  # ⚠️ 报错:RuntimeError: view size is not compatible with input tensor's size and stride

2. 使用 transpose() 后需要 .contiguous() 参与后续操作

python 复制代码
a = torch.randn(10, 20)
b = a.transpose(0, 1)  # Not contiguous now
b = b.contiguous()     # 重新在内存中复制数据为连续块

四、查看是否是连续的

python 复制代码
x.is_contiguous()

五、底层原理简要

PyTorch 张量有 .stride() 属性定义每一维的跳步。连续的张量满足:

python 复制代码
x.stride()[i] = product(x.shape[i+1:])

一旦 .transpose() / .permute() 修改了维度顺序,这个规则就被破坏,因此 .contiguous() 会重新分配内存来确保是连续的。


六、contiguous()项目演示

下面是一个完整的 PyTorch 小项目,演示 .contiguous() 的必要性与作用。将看到在对张量进行 permute() 后,使用 .view() reshape 会失败,只有 .contiguous() 可以解决问题。

项目内容:张量维度变换与 .contiguous() 对比演示

项目结构:

复制代码
contiguous_demo/
├── main.py
└── requirements.txt

requirements.txt

txt 复制代码
torch>=2.0

main.py

python 复制代码
import torch

def describe_tensor(tensor, name):
    print(f"{name}: shape={tensor.shape}, strides={tensor.stride()}, is_contiguous={tensor.is_contiguous()}")

def main():
    print("=== 创建张量 ===")
    x = torch.randn(2, 3, 4)  # 原始张量 shape [2, 3, 4]
    describe_tensor(x, "x")

    print("\n=== 进行 permute 操作(交换维度) ===")
    y = x.permute(1, 0, 2)  # shape: [3, 2, 4]
    describe_tensor(y, "y (after permute)")

    print("\n尝试 view reshape 到 [3, 8](不使用 contiguous)")
    try:
        z = y.view(3, 8)  # ⚠️ 报错:因为 y 的内存不是连续的
    except RuntimeError as e:
        print(f"RuntimeError: {e}")

    print("\n=== 使用 .contiguous() 后 reshape ===")
    y_contig = y.contiguous()
    describe_tensor(y_contig, "y_contig (after .contiguous())")

    z = y_contig.view(3, 8)
    describe_tensor(z, "z (reshaped)")

    print("\n✅ reshape 成功,结果如下:")
    print(z)

if __name__ == "__main__":
    main()

运行方法

  1. 安装依赖:
bash 复制代码
pip install -r requirements.txt
  1. 运行程序:
bash 复制代码
python main.py

运行结果概览

将看到:

  • 原始张量是连续的;
  • permute() 后变成非连续;
  • 使用 .view() 报错;
  • .contiguous() 修复内存后成功 reshape。

小总结要点

操作 是否连续 能否 .view()
原始张量 ✅ 是 ✅ 是
permute() ❌ 否 ❌ 报错
.contiguous() ✅ 是 ✅ 是

总结记忆:

操作 是否影响连续性? 是否需要 .contiguous()
view() ❗ 需要连续 ✅ 是
permute() / transpose() 破坏连续性 ✅ 是
reshape() 自动处理 ❌ 不需要(内部处理)
相关推荐
Jay Kay30 分钟前
TensorFlow源码深度阅读指南
人工智能·python·tensorflow
FF-Studio34 分钟前
【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
人工智能·pytorch·深度学习·线性代数·机器学习·数学建模·transformer
会的全对٩(ˊᗜˋ*)و44 分钟前
【数据挖掘】数据挖掘综合案例—银行精准营销
人工智能·经验分享·python·数据挖掘
云渚钓月梦未杳1 小时前
深度学习03 人工神经网络ANN
人工智能·深度学习
在美的苦命程序员1 小时前
中文语境下的视频生成革命:百度 MuseSteamer 的“产品级落地”启示录
人工智能·百度
___波子 Pro Max.1 小时前
GitHub Actions配置python flake8和black
python·black·flake8
kngines1 小时前
【字节跳动】数据挖掘面试题0007:Kmeans原理,何时停止迭代
人工智能·数据挖掘·kmeans
Kali_071 小时前
使用 Mathematical_Expression 从零开始实现数学题目的作答小游戏【可复制代码】
java·人工智能·免费
贾全1 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
每日摸鱼大王1 小时前
互联网摸鱼日报(2025-07-01)
人工智能