当前位置:网站首页>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
边栏推荐
- mongoDB 持久化
- Calculus Learning
- (转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- July 2022 (advanced soft test) information system project manager certification enrollment Brochure
- train_ on_ Batch save the image of the loss function change
- 首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期
- Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
- MySQL uses union all to count the total number of combinations of multiple tables and the number of tables respectively
- 工厂模式和策略模式的区别
- GPU训练云平台记录
猜你喜欢
随机推荐
Augfpn: amélioration de l'apprentissage des caractéristiques à plusieurs échelles pour la détection des cibles
Augfpn: improved multiscale feature learning for target detection
修改exif信息
使用GPU训练kernel切换
First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8
超融合架构和传统架构有什么区别?
[target detection] | indicator a probabilistic challenge for object detection
ActiveMQ message component publish subscribe redelivery message redelivery
Analysis of c voice endpoint detection (VAD) implementation process
GPU训练云平台记录
uniapp微信小程序报错 TypeError: Cannot read property ‘call‘ of undefined
js轮播图观后重做(较长的完整版,可运行)
记微信小程序setData动态修改字段名
H5软键盘问题
Activemq消息组件发布订阅ReDelivery消息重新投递
Wechat applet project: wechat applet page layout
Network learning of pointnet
PointNet/Pointnet++训练及测试
Can we trust bounding box annotations for object detection
Let you know today that the passing rate of the PMP Exam is 97%, is it believable








