NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

pytorch中contiguous()

已有 3945 次阅读 2020-10-29 09:04 |个人分类:Pytorch|系统分类:科研笔记

       contiguous:view只能用在contiguous的variable上。如果在view之前用了transpose, permute等,需要用contiguous()来返回一个contiguous copy。
一种可能的解释是:
       有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把tensor变成在内存中连续分布的形式。
判断是否contiguous用torch.Tensor.is_contiguous()函数。

import torch

x = torch.ones(10, 10)

x.is_contiguous()  # True

x.transpose(0, 1).is_contiguous()  # False

x.transpose(0, 1).contiguous().is_contiguous()  # True

       在pytorch的最新版本0.4版本中,增加了torch.reshape(), 这与 numpy.reshape 的功能类似。它大致相当于 tensor.contiguous().view()。

【参考】

https://blog.csdn.net/u011394059/article/details/78664694
https://blog.csdn.net/guotong1988/article/details/78730078
https://blog.csdn.net/u011276025/article/details/73826562
点滴分享,福泽你我!Add oil!



https://wap.sciencenet.cn/blog-3428464-1256181.html

上一篇:Pytorch中LayerNorm计算过程详解
下一篇:PyTorch的torch.cat
收藏 IP: 103.149.249.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 08:09

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部