Hello everyone.
I have 2 different jupyter notebooks that almost do the same thing.
In the first notebook I do the following:
a) I downloaded vgg16
b) Pass my images to vgg16 and take the output vector from an intermidiate layer
c) Create a matrix with all those vectors.
d) Create my own MLP and train it with this matrix.
In the second notebook I do the exactly same thing as the previous one, but the only difference is that instead of the vgg16 model I have resnet50 with the pre-trained weights of MoCo model. All the other code is the same.
My problem is that when I train my MLP with resnet50 MoCo model I have the following error: “Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time”.
So after a search I did, I change loss.backward() to loss.backward(retain_graph=True) and it worked.
But the problem is, that it is running very very slow.
I do not understand why this is happening when I have resnet50 MoCo model and not with vgg16.
Can anyone help me how to fix this?
My train function was the following for both notebooks in the beginning:
def train_model(model, data, actual_labels):
    model.train()
    
    optimizer.zero_grad()
    
    predicted_training_labels = model(data)

    loss = loss_function(predicted_training_labels, actual_labels)
    
    accuracy = compute_accuracy(predicted_training_labels, actual_labels)

    loss.backward()

    optimizer.step()
    
    return loss.item()

Thanks a lot.