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

连续张量理解和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.

原文地址:https://www.cnblogs.com/lishikai/p/12368246.html

时间: 2024-08-29 05:50:39

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

百度Android语音识别SDK语义理解与解析方法

百度语义理解开放平台面向互联网开发者提供自然语言文本的解析服务,也就是可以根据文本的意图解析成相应的表示. 为了易于人阅读,同时也方便机器解析和生成,意图表示协议采用 json 语言进行描述,采用 gb18030 编码. json 语言的基本概念: 1.属性名/属性值 即键值对(key-value) 2.数组 在 json 中是"[]"括起来的内容,数据结构为 ["value1","value2",...],取值方式和所有语言中一样,使用索引获取

C#Lambda表达式的理解:谓词方法 匿名方法 使用Lambda

Lambda表达式 "Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量.它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式.所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to".Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块. 下面三个方法会帮你会容易理解到Lambda表达式的好处,

TableViewCell reuse 重用 我认为的正确理解与使用方法

其实有点失望,因为用google搜索“uitableviewcell dequeueReusableCellWithIdentifier”出来一堆堆资深博主的文章.看了看,大部分都是在解决一个问题:使用重用时cell显示混乱的问题.该问题本身并不让我失望,失望的是博主们的解释. 首先,回顾一下UITableViewCell的重用,其基本逻辑就是tableView一开始会创建一屏幕的cell(如果有那么多)并把他们标记(Identifier),之后用户上下滑动tableView时,使用Identi

计划:怎样理解水平集方法 ITK Level set V4 框架介绍

简易解释:在曲面中插入一个平面所形成的轮廓,即是该轮廓的水平集表示,可见,该轮廓的水平集表示有多个.对于图像分割,在图像力的驱动下曲面进行更新. 轮廓的数学表达有隐式和显式两种表达.用曲面演化代替Front (C)演进. C(t) = {(x, y)|φ(x, y, t) = 0} ?φ/ ?t + F|?φ| =0 (1) φ(x, y, 0) = φ0(x, y) 方程的本质是什么? 几何解释是什么 edge-based level set ?φ /?t = g(?I)|?φ| (div (

理解 ES6 Generator-next()方法

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>generator-next</title> 6 </head> 7 <body> 8 9 <h3>理解 ES6 Generator-next() 方法</h3> 10 11 <scr

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

#[JVM虚拟机](8)--深入理解Class中--方法.属性表集合 之前有关class文件已经写了两篇博客: 1.[JVM虚拟机](5)---深入理解JVM-Class中常量池 2.[JVM虚拟机](6)---深入理解Class中访问标志.类索引.父类索引.接口索引 3.[JVM虚拟机](7)---深入理解Class中-属性集合 那么这篇博客主要讲有关 方法表集合 相关的理解和代码示例. 方法表集合: 告知该方法是什么修饰符修饰?是否有方法值?返回类型是什么?方法名称,方法参数,还有就是方法内

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

1. SurfaceView 游戏框架实例 实例效果:就是屏幕上的文本跟着点击的地方移动,效果图如下: 步骤: 新建项目“GameSurfaceView”,首先自定义一个类"MySurfaceView",此类继承SurfaceView,并实现android.view.SurfaceHolder.Callback 接口,代码如下 package com.example.ex4_5; import android.content.Context; import android.graphi

View和ViewGroup的区别

View.java源码: /frameworks/base/core/java/android/view/View.java View.java的 dispatchTouchEvent 方法 /** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched.

View 和 SurfaceView的区别

这里说的是在绘图中两者的区别: 1View在绘图中,重写onDraw(Canvas canvas)方法,通过invaldate()和pastInvalidate()两个方法进行重新绘制画布; invalidate()不能再自己创建的线程中循环调用: postInvalidate()可以在自己创建的线程中循环调用执行.如果不在当前View 创建线程循环重绘画布的话,这两种重绘画布的函数就没什么区别了,都可以用. public class MyView extends View { private