连续张量理解和contiguous()方法使用,view和reshape的区别

待办

内存共享:

下边的x内存布局是从0开始的,y内存布局,不是从0开始的张量


For example: when you call transpose(), PyTorch doesn't generate new tensor with new layout, it just modifies meta information in Tensor object so offset and stride are for new shape. The transposed tensor and original tensor are indeed sharing the memory! ^[ > x = torch.randn(3,2) y = torch.transpose(x, 0, 1) x[0, 0] = 42
> print(y[0,0])
> # prints 42 ]
This is where the concept of contiguous comes in. Above x is contiguous but y is not because its memory layout is different than a tensor of same shape made from scratch. Note that the word "contiguous" is bit misleading because its not that the content of tensor is spread out around disconnected blocks of memory. Here bytes are still allocated in one block of memory but the order of the elements is different! When you call contiguous(), it actually makes a copy of tensor so the order of elements would be same as if tensor of same shape created from scratch. Normally you don't need to worry about this. If PyTorch expects contiguous tensor but if its not then you will get RuntimeError: input is not contiguous and then you just add a call to contiguous()

view 和 reshape 的区别

Another difference is that reshape() can operate on both contiguous

and non-contiguous tensor while view() can only operate on contiguous

tensor. Also see here about the meaning of contiguous.

如果出现不是连续张量的问题,解决方案

Another difference is that reshape() can operate on both contiguous

and non-contiguous tensor while view() can only operate on contiguous

tensor. Also see here about the meaning of contiguous.

连续张量理解和contiguous()方法使用,view和reshape的区别的更多相关文章

  1. (MTT)连续能量函数最小化方法

    (MTT)连续能量函数最小化方法 Multitarget tracking Multi-object tracking 连续能量函数 读"A.Milan,S. Roth, K. Schind ...

  2. js中的回调函数的理解和使用方法

    js中的回调函数的理解和使用方法 一. 回调函数的作用 js代码会至上而下一条线执行下去,但是有时候我们需要等到一个操作结束之后再进行下一个操作,这时候就需要用到回调函数. 二. 回调函数的解释 因为 ...

  3. 【JVM虚拟机】(8)--深入理解Class中--方法、属性表集合

    #[JVM虚拟机](8)--深入理解Class中--方法.属性表集合 之前有关class文件已经写了两篇博客: 1.[JVM虚拟机](5)---深入理解JVM-Class中常量池 2.[JVM虚拟机] ...

  4. 理解 ES6 Generator-next()方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 【读书笔记《Android游戏编程之从零开始》】11.游戏开发基础(SurfaceView 游戏框架、View 和 SurfaceView 的区别)

    1. SurfaceView 游戏框架实例 实例效果:就是屏幕上的文本跟着点击的地方移动,效果图如下: 步骤: 新建项目“GameSurfaceView”,首先自定义一个类"MySurfac ...

  6. View和ViewGroup的区别 -- Touch事件处理

    View.java源码: /frameworks/base/core/java/android/view/View.java View.java的 dispatchTouchEvent 方法: 经过一 ...

  7. 从tcp原理角度理解Broken pipe和Connection reset by peer的区别

    从tcp原理角度理解Broken pipe和Connection reset by peer的区别 http://lovestblog.cn/blog/2014/05/20/tcp-broken-pi ...

  8. Android界面的View以及ViewGroup的区别

    因为这个问题会经常成为面试的热点,所以我们来谈谈View以及ViewGroup的区别. 先看看View及ViewGroup类关系    Android View和ViewGroup从组成架构上看,似乎 ...

  9. Python join() 方法与os.path.join()的区别

    Python join() 方法与os.path.join()的区别 pythonJoinos.path.join 今天工作中用到python的join方法,有点分不太清楚join() 方法与os.p ...

随机推荐

  1. [SDOI2018]反回文串

    题意 问有多少个长度为\(N\)且字符集大小为\(K\)的字符串可以通过回文串旋转 (把第一个字符移到最后)若干次得到.\(K\le N≤10^{18}\) 做法 ARC64F的加强版 设\(h(d) ...

  2. P1967 货车运输【LCA】【生成树】

    题目描述 A 国有 nn 座城市,编号从 11 到 nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的 ...

  3. 汇桔网被曝拖欠12月份工资至今,强制买产品,CEO称去年交易额超400亿

    三言财经消息,近日有汇桔网员工爆料,汇桔网拖欠12月工资至今,通知延迟到4月才发放. 此外爆料还指出,"各种手法逼迫大家离开,员工要么继续忍受,要么主动离职,要么停薪留职.都不同意只能仲裁. ...

  4. 50元求解pyqt加载并显示pdf问题

    50块钱求解决pyqt5加载pdf文档并显示,类似briss软件的效果

  5. #AcWing系列课程Level-2笔记——2. 归并排序算法

    归并排序算法 编写归并排序,记住下面的思路,代码也就游刃有余了! 1.首先确定数组的中间位置的分界点(下标),也就是mid=(left+right)>>1,分成left,right两段. ...

  6. PTA 邻接表存储图的广度优先遍历

    试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...

  7. CF1230E Kamil and Making a Stream

    题目大意是求 \(\sum_{v,fa,lca(v,fa)=fa}gcd(v \to fa)\) 容易发现 \(\gcd\) 只会变小,所以根据这玩意是从上到下的,每次暴力一下就可以了,\(\gcd\ ...

  8. perfdog使用手册

    PerfDog(性能狗)测试须知 Android平台 ScreenShot(只支持USB模式) FPS(1秒内游戏画面或者应用界面真实平均刷新次数,俗称帖率/FPS) 1)Avg(FPS):平均帖率 ...

  9. 【JavaScript】JS总结 – 乱

    一. 重要:js中的function函数声明.函数表达式 // 函数声明 // Ex: 会在代码执行之前提前加载到作用域中,即js解析器会优先读取,确保在所有代码执行之前声明已经被解析:所以可以在定义 ...

  10. JavaScript 自适应轮播图

    代码 话不多说,先上代码,方便复制粘贴.演示 <!DOCTYPE html> <html lang="en"> <head> <meta ...