Skip to content

Instantly share code, notes, and snippets.

@birkin
Last active April 13, 2023 01:42
Show Gist options
  • Select an option

  • Save birkin/d1a4a4ba978ed5a48e674c82d77ac7bd to your computer and use it in GitHub Desktop.

Select an option

Save birkin/d1a4a4ba978ed5a48e674c82d77ac7bd to your computer and use it in GitHub Desktop.
chatGPT explains a poorly-worded feature-freeze instruction in a neural-network tutorial.

User:

I'm going through a pytorch tutorial, and see this statement and code:

"""

In order to keep fix the update of the feature part of the network, we can code below:

for param in model.features.parameters():
    param.requires_grad = False

"""

...but that explanatory statement is poorly written. Can you elaborate?


ChatGPT:

Certainly! The statement you've provided is indeed a bit unclear. Let me rephrase and elaborate on it:

"""
To prevent the feature extraction part of the network from being updated during training, you can use the following code:

for param in model.features.parameters():
    param.requires_grad = False
"""

In this context, the code snippet is used to "freeze" the feature extraction part of a pre-trained neural network. By setting param.requires_grad to False, the gradients for those parameters will not be computed during backpropagation, and thus, their values will not be updated during the optimization process. This is typically done when you want to use the pre-trained feature extraction part as-is and only train the remaining layers, e.g., the classifier part of the network, for a new task (transfer learning).


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment