当前位置:网站首页>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
边栏推荐
- cokkie和session的区别
- 编程语言
- 微信小程序自定义多项选择器
- Keras to tf Vgg19 input in keras_ shape
- Professional structure record
- Member inner class, static inner class, local inner class
- SSD改進CFENet
- Pat (basic level) practice (Chinese) 1003 I want to pass! (20 points) C language implementation
- GPU training cloud platform record
- Let's make a summary
猜你喜欢

uni-app获取当前页面路由url

Scenario analysis of deadlock during MySQL insert

来个小总结吧

Heavyweight released "FISCO bcos application landing guide"

【To .NET】C#数据模型,从Entity Framework Core到LINQ

微积分学习

What is the difference between hyperconverged architecture and traditional architecture?

微信小程序判断url的文件格式

Augfpn: improved multiscale feature learning for target detection

laravel 8 实现 订单表按月份水平分表
随机推荐
微信小程序项目:微信小程序页面布局
uni-app获取当前页面路由url
GPU训练云平台记录
Open3D 隐藏点移除
Macros, functions, and inline functions
抽象类、接口
uniapp微信小程序报错 TypeError: Cannot read property ‘call‘ of undefined
ServerApp. iopub
ActiveMQ message component publish subscribe redelivery message redelivery
Multiplier design (pipeline) Verilog code
来个小总结吧
Verilog 拼接操作符号
Wechat applet project: tab navigation bar
记自定义微信小程序顶部导航栏
训练查看(问题暂存)
AugFPN:改進多尺度特征學習用於目標檢測
Verilog 数据类型
train_ on_ Batch save the image of the loss function change
首次触电,原来你是这样的龙蜥社区 | 龙蜥开发者说第8期
Enrollment brochure for system integration project management engineer certification in July 2022