Skip to content

Language models, retrieval, prompting, and language adaptation · Contextual and sentence encoders · LNG012 · Architecture

ALBERT

Reduces encoder parameter storage by factorizing token embeddings and sharing transformer parameters across layers.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Reduces encoder parameter storage by factorizing token embeddings and sharing transformer parameters across layers. It enables wider or deeper representations within a parameter budget, while illustrating that parameter count and computation are different resources.

Question / objective

Can factorization and parameter sharing reduce encoder storage without discarding contextual modeling?

Architecture / computational structure

Token embeddings factor through a smaller matrix and Transformer layers share parameters according to configuration. Learned embedding factors and shared blocks are parameters; factor width and sharing pattern are hyperparameters.

Assumptions

Weight sharing reduces independent capacity and memory but does not proportionally reduce attention FLOPs or activation memory.

Formula / computational definition

E=AB,A∈RV×e,B∈Re×d,Ve+ed≪Vd when e≪d.E=AB,\quad A\in\mathbb R^{V\times e},\quad B\in\mathbb R^{e\times d},\qquad Ve+ed\ll Vd\ \text{when }e\ll d.

Factor a vocabulary-to-hidden embedding matrix as E=AB with A in R^(V by e) and B in R^(e by d), e smaller than d. Repeated layers reuse selected weights. Pretraining combines masked-token prediction with sentence-order prediction.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Embedding bottleneck e, hidden dimension d, sharing pattern, depth, heads, and sentence-order sampling.

Fitting, fine-tuning, and adaptation

Fine-tune a task head with the shared encoder frozen initially, then selectively adapt the shared blocks or compatible adapters. A change to one shared block affects multiple depth positions, so validate stability carefully. Distinguish storage savings from runtime and activation memory.

How to use and calibrate

Report quality, stored parameters, activation memory, latency, and compute separately; compare transfer with equal resource constraints. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Deploy a document encoder under a constrained model-weight storage budget.

Interpretation / pitfalls

Watch out

Downstream behavior still depends on corpus and tokenizer, not only compression. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Transformers offers ALBERT models. R can bridge the Python implementation, while a native R torch version must share actual parameter objects rather than accidentally creating independent layers.

Example scope: ALBERT embedding parameter-count comparison; excludes other network components.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — ALBERT embedding parameter-count comparison; excludes other network components.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
vocab, hidden, factor = 30000, 768, 128
direct = vocab * hidden
factorized = vocab * factor + factor * hidden
print({"direct": direct, "factorized": factorized, "ratio": factorized / direct})
R

Component demo, not a trained model — ALBERT embedding parameter-count comparison; excludes other network components.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
vocab <- 30000; hidden <- 768; factor <- 128
direct <- vocab * hidden
factorized <- vocab * factor + factor * hidden
print(c(direct=direct, factorized=factorized, ratio=factorized/direct))

Sources

From the CodeSplash reference, entry LNG012.