当前位置:网站首页>pytorch学习总结—运算的内存开销
pytorch学习总结—运算的内存开销
2022-06-29 08:31:00 【TJMtaotao】
前⾯面说了了,索引、 view 是不不会开辟新内存的,⽽而像 y = x + y 这样的运算是会新开内存的,然后
将 y 指向新内存。为了了演示这⼀一点,我们可以使⽤用Python⾃自带的 id 函数:如果两个实例例的ID⼀一致,那
么它们所对应的内存地址相同;反之则不不同。
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y = y + x
print(id(y) == id_before) # False
如果想指定结果到原来的 y 的内存,我们可以使⽤用前⾯面介绍的索引来进⾏行行替换操作。在下⾯面的例例⼦子中,
我们把 x + y 的结果通过 [:] 写进 y 对应的内存中。
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y[:] = y + x
print(id(y) == id_before) # True
我们还可以使⽤用运算符全名函数中的 out 参数或者⾃自加运算符 += (也即 add_() )达到上述效果,例如
torch.add(x, y, out=y) 和 y += x ( y.add_(x) )
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
torch.add(x, y, out=y) # y += x, y.add_(x)
print(id(y) == id_before) # True
边栏推荐
猜你喜欢

train_on_batch保存一下loss函数变化的图像

Let you know today that the passing rate of the PMP Exam is 97%, is it believable

train_ on_ Batch save the image of the loss function change

uniapp引入组件不生效解决方法

Cdga | what is the core of digital transformation in the transportation industry?

Instance error iopub data rate exceeded

Debugging H5 page -weinre and spy debugger real machine debugging

微信小程序最新canvas2d手写签名

SSD改进CFENet

Abstract classes and interfaces
随机推荐
H5 soft keyboard problem
ServerApp.iopub
实例报错IOPub data rate exceeded
训练查看(问题暂存)
cmd进入虚拟机
MySQL的分库分表策略及应用场景
promise方法的简单使用
MT yolov6 training and testing
微信小程序判断url的文件格式
mongoDB 持久化
uniapp引入组件不生效解决方法
NPM common commands
[to.Net] C data model, from Entity Framework core to LINQ
Professional structure record
Detecting and counting tiny faces
DevOps到底是什么意思?
网络安全问题
Uniapp wechat applet reports an error typeerror: cannot read property 'call' of undefined
手写VirtualDOM
Mqtt second session -- emqx high availability cluster implementation