pytorch中深度拷贝_PyTorch中的拷贝与就地操作详解

本文详细介绍了PyTorch中的In-place操作,如.add_(),以及它们如何减少内存使用。同时,文章讨论了浅拷贝和深拷贝的区别,如`.clone()`和`.detach()`,并强调了在训练过程中慎用就地操作,因为它们可能导致梯度丢失。此外,文章通过实例展示了不同拷贝和就地操作对内存分配的影响。

前言

PyTroch中我们经常使用到Numpy进行数据的处理,然后再转为Tensor,但是关系到数据的更改时我们要注意方法是否是共享地址,这关系到整个网络的更新。本篇就In-palce操作,拷贝操作中的注意点进行总结。

In-place操作

pytorch中原地操作的后缀为_,如.add_()或.scatter_(),就地操作是直接更改给定Tensor的内容而不进行复制的操作,即不会为变量分配新的内存。Python操作类似+=或*=也是就地操作。(我加了我自己~)

为什么in-place操作可以在处理高维数据时可以帮助减少内存使用呢,下面使用一个例子进行说明,定义以下简单函数来测量PyTorch的异位ReLU(out-of-place)和就地ReLU(in-place)分配的内存:

import torch # import main library

import torch.nn as nn # import modules like nn.ReLU()

import torch.nn.functional as F # import torch functions like F.relu() and F.relu_()

def get_memory_allocated(device, inplace = False):

'''

Function measures allocated memory before and after the ReLU function call.

INPUT:

- device: gpu device to run the operation

- inplace: True - to run ReLU in-place, False - for normal ReLU call

'''

# Create a large tensor

t = torch.randn(10000, 10000, device=device)

# Measure allocated memory

torch.cuda.synchronize()

start_max_memory = torch.cuda.max_memory_allocated() / 1024**2

start_memory = torch.cuda.memory_allocated() / 1024**2

# Call in-place or normal ReLU

if inplace:

F.relu_(t)

else:

output = F.relu(t)

# Measure allocated memory after the call

torch.cuda.synchronize()

end_max_memory = torch.cuda.max_memory_allocated() / 1024**2

end_memory = torch.cuda.memory_allocated() / 1024**2

# Return amount of memory allocated for ReLU call

return end_memory - start_m

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值