Cuda Out of Memory Ошибка после нескольких успешных партий - PullRequest
0 голосов
/ 27 декабря 2018

Вот мой код.Я получаю CUDA из-за ошибки памяти после успешного выполнения 140 пакетов.Я использовал метод item, чтобы избежать сохранения тензора.Использовал empty_cache, gc collect, retain_graph как False при обратном вызове.Но все напрасно.Пожалуйста, предложите.

for epoch in range(1, n_epochs+1):
    train_loss = 0.0
    model.train()
    for batch_idx, (image,label) in enumerate(loaders['train']):
    # move to GPU
    if use_cuda:
        image,label = image.cuda(), label.cuda()
    if (batch_idx +1) % 20 == 0:
        print('Batch Id '+ str(batch_idx +1))
    ## find the loss and update the model parameters accordingly
    ## record the average training loss, using something like
    ## train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - 
    ## train_loss))
    optimizer_scratch.zero_grad()
    output = model_scratch(image)
    loss = criterion_scratch(output,label)
    ##loss.backward(retain_graph=False)
    loss.backward()

    optimizer_scratch.step()
    train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.item() - 
    train_loss))
    torch.cuda.empty_cache() 
    ##gc.collect()
...