Frozen Encoder
A frozen encoder is a pretrained model whose parameters are held fixed while an added head learns. The forward pass computes representations. Parameter freezing controls which parameters receive updates; it does not by itself disable stochastic layers or stop non-parameter state such as BatchNorm running statistics from changing.
The usual argument for freezing has two parts, and they are not equally solid. Excluding encoder parameters from updates can reduce backward-pass and optimizer-state costs; the total saving depends on the implementation. The accuracy argument — that freezing protects you from overfitting when labels are scarce — is a claim about your data, and it is easy to check.
What freezing actually buys
Below, an encoder is fitted on 20,000 unlabeled source rows and then frozen. A head is trained on it with increasing numbers of labeled target rows, and compared against models that use all 200 raw dimensions instead.
import numpy as np
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
d, latent = 200, 15
basis = rng.normal(size=(latent, d))
make = lambda n: rng.normal(size=(n, latent)) @ basis + rng.normal(scale=0.5, size=(n, d))
source = make(20_000)
encoder = PCA(n_components=latent).fit(source)
"""The encoder was fitted on 20,000 unlabeled source rows and is then frozen.
The target task has a different label rule and far fewer rows."""
target = make(6000)
w = rng.normal(size=latent)
y = ((target @ basis.T @ np.linalg.pinv(basis @ basis.T) @ w) > 0).astype(int)
xtr_all, xte, ytr_all, yte = train_test_split(target, y, test_size=3000, random_state=0)
print(f"{'labeled target rows':>21} {'frozen + head':>15} {'all 200 dims, regularized':>27}"
f" {'all 200 dims, no penalty':>26}")
for n in (25, 50, 100, 400, 1600, 3000):
xtr, ytr = xtr_all[:n], ytr_all[:n]
frozen = LogisticRegression(max_iter=3000).fit(encoder.transform(xtr), ytr)
tuned = LogisticRegression(max_iter=3000).fit(xtr, ytr)
raw = LogisticRegression(max_iter=5000, C=np.inf).fit(xtr, ytr)
print(f"{n:>21} {frozen.score(encoder.transform(xte), yte):>15.4f}"
f" {tuned.score(xte, yte):>27.4f} {raw.score(xte, yte):>26.4f}")
print(f"\n the frozen encoder turns {d} raw dimensions into {latent},"
f" so the head has {latent + 1} parameters instead of {d + 1}")
mismatch = rng.normal(size=(latent, d))
off_source = rng.normal(size=(20_000, latent)) @ mismatch + rng.normal(scale=0.5, size=(20_000, d))
off_encoder = PCA(n_components=latent).fit(off_source)
"""Now freeze an encoder fitted on a source whose structure does not match the target."""
print(f"\n{'labeled target rows':>21} {'matched encoder':>17} {'mismatched encoder':>20}")
for n in (100, 400, 1600, 3000):
xtr, ytr = xtr_all[:n], ytr_all[:n]
a = LogisticRegression(max_iter=3000).fit(encoder.transform(xtr), ytr)
b = LogisticRegression(max_iter=3000).fit(off_encoder.transform(xtr), ytr)
print(f"{n:>21} {a.score(encoder.transform(xte), yte):>17.4f}"
f" {b.score(off_encoder.transform(xte), yte):>20.4f}")
# labeled target rows frozen + head all 200 dims, regularized all 200 dims, no penalty
# 25 0.7727 0.7707 0.7857
# 50 0.8940 0.9043 0.8850
# 100 0.9440 0.9517 0.9460
# 400 0.9840 0.9813 0.9720
# 1600 0.9953 0.9903 0.9913
# 3000 0.9970 0.9910 0.9933
#
# the frozen encoder turns 200 raw dimensions into 15, so the head has 16 parameters instead of 201
#
# labeled target rows matched encoder mismatched encoder
# 100 0.9440 0.8517
# 400 0.9840 0.8490
# 1600 0.9953 0.8697
# 3000 0.9970 0.8693
Read the first block carefully, because it does not say what the folklore says. At 25 labeled rows — eight times fewer rows than raw dimensions, the regime where freezing is supposed to rescue you — the frozen model scores 0.7727 and the regularized model on all 200 dimensions scores 0.7707. At 50 and 100 rows the regularized full model is slightly ahead. The frozen encoder only pulls clear from 400 rows onward, and then by less than a point.
The comparison shows that a regularized model on raw inputs can match or exceed the frozen PCA representation at these small sample sizes. It does not isolate freezing from fine-tuning the same encoder, and it does not identify a universal share of the benefit attributable to L2. Projection and regularization both restrict the fitted rule in different ways; compare them on the target task before attributing an improvement to freezing.
The head here fits 16 parameters rather than 201. In a neural transfer setup, excluding encoder parameters from updates can save their gradient and optimizer-state costs. Caching representations across epochs is valid only when the encoder computation and inputs are fixed: control train/eval mode, running buffers, dropout and input augmentation. A frozen weight tensor alone does not establish those conditions.
What a mismatched fixed representation can discard
The second block is the real risk of freezing, and it shows up as a flat line. With a matched encoder, accuracy climbs from 0.9440 to 0.9970 as labels go from 100 to 3,000. With an encoder frozen on a mismatched source, it sits at 0.8517, 0.8490, 0.8697, 0.8693 — essentially unmoved by a thirtyfold increase in labeled data.
If a fixed representation discards information required by the target, labels alone cannot restore that information to a head that sees only the representation. That limits the attainable performance for this representation and head family, but it is not unique to freezing: other lossy feature designs also impose limits. A flat learning curve can motivate investigation without proving the asymptotic ceiling from a few sample sizes.
Peters, Ruder and Smith tested feature extraction against fine-tuning across NLP tasks and reported that which one wins “depends on the similarity of the pretraining and target tasks”. That is the same finding stated from the other direction: task similarity is what decides, so there is no default answer to carry between projects.
Freezing is a dial, not a switch
Between a fully frozen encoder and full fine-tuning there is a range: unfreeze the last block, unfreeze everything at a reduced learning rate, or start frozen and unfreeze once the head has stopped changing. Yosinski and colleagues found that transferred features help even across fairly distant task pairs, and that the benefit of initializing from them “lingers even after fine-tuning” on the target data. They also identified two distinct ways transfer degrades: higher layers specialize to the original task, and splitting a network between co-adapted units causes optimization difficulties.
Those two failure modes point in opposite directions, which is why the choice is empirical. Specialization argues for unfreezing the upper layers; co-adaptation argues against cutting the network at an arbitrary depth. The practical procedure is to treat the freeze depth as a hyperparameter and measure it, in the same way you would treat any other transfer learning decision.
Version the encoder parameters, persistent buffers, preprocessing and tokenizer or image transform with the head. For deterministic cached feature extraction, also preserve the appropriate evaluation mode and input processing. A checkpoint change can invalidate the cached representation and the head’s performance assumptions, so check compatibility and reevaluate rather than silently swapping it.
Freezing model weights alone does not freeze all module behavior.
References
- Peters, M. E., Ruder, S., and Smith, N. A. (2019). To Tune or Not to Tune? Adapting Pretrained Representations to Diverse Tasks. Proceedings of the 4th Workshop on Representation Learning for NLP, 7–14. aclanthology.org
- Yosinski, J., Clune, J., Bengio, Y., and Lipson, H. (2014). How transferable are features in deep neural networks? NeurIPS 27. arxiv.org
- scikit-learn documentation,
sklearn.decomposition.PCAandsklearn.linear_model.LogisticRegression. scikit-learn.org - PyTorch documentation, Autograd mechanics: freezing parameters and evaluation mode
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
