【发布时间】:2019-02-11 19:06:49
【问题描述】:
我有一些高级代码,所以模型训练等被pipeline_network 类包裹。我的主要目标是在每次新折叠时都训练新模型。
for train_idx, valid_idx in cv.split(meta_train[DEPTH_COLUMN].values.reshape(-1)):
meta_train_split, meta_valid_split = meta_train.iloc[train_idx], meta_train.iloc[valid_idx]
pipeline_network = unet(config=CONFIG, suffix = 'fold' + str(fold), train_mode=True)
然后我继续进行第二次折叠,一切都因 gpu 内存而失败:
RuntimeError: cuda runtime error (2) : out of memory at /pytorch/torch/lib/THC/generic/THCStorage.cu:58
在 epoch 结束时,我尝试手动删除该管道,但没有成功:
def clean_object_from_memory(obj): #definition
del obj
gc.collect()
torch.cuda.empty_cache()
clean_object_from_memory( clean_object_from_memory) # calling
调用它也无济于事:
def dump_tensors(gpu_only=True):
torch.cuda.empty_cache()
total_size = 0
for obj in gc.get_objects():
try:
if torch.is_tensor(obj):
if not gpu_only or obj.is_cuda:
del obj
gc.collect()
elif hasattr(obj, "data") and torch.is_tensor(obj.data):
if not gpu_only or obj.is_cuda:
del obj
gc.collect()
except Exception as e:
pass
如何重置 pytorch 然后我继续下一个折叠?
【问题讨论】:
标签: python python-3.x out-of-memory gpu pytorch