Skip to content

Language models, retrieval, prompting, and language adaptation · Count-based and distributed representations · LNG002 · Model

Neural probabilistic language model (Bengio)

Jointly learns distributed word representations and a fixed-context next-word predictor. Its importance is structural: similar contexts share parameters through embeddings, allowing generalization beyond observed word…

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

Jointly learns distributed word representations and a fixed-context next-word predictor. Its importance is structural: similar contexts share parameters through embeddings, allowing generalization beyond observed word sequences. It provides a manageable bridge from count models to neural language models.

Question / objective

Can shared distributed representations improve next-word prediction over count-only contexts?

Architecture / computational structure

An embedding lookup, concatenated fixed-window input, hidden nonlinear layer, and vocabulary softmax form the network. Learned parameters are embeddings, dense weights, and biases; layer dimensions are hyperparameters.

Assumptions

A fixed window cannot directly retain arbitrarily long history.

Formula / computational definition

h=tanh⁡(Wx+b),p(w∣x)=softmax⁡(Uh+c)w,L=−log⁡p(wtarget∣x).h=\tanh(Wx+b),\qquad p(w\mid x)=\operatorname{softmax}(Uh+c)_w,\qquad \mathcal L=-\log p(w_{\mathrm{target}}\mid x).

Concatenate context embeddings into x, compute h=tanh(Wx+b), then p(w|context)=softmax(Uh+c)_w. Minimize the negative log probability of observed next words; W,U,b,c and the embedding table are learned parameters.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Context length, vocabulary, embedding dimension, hidden width, regularization, optimizer, and batch size.

Fitting, fine-tuning, and adaptation

Train with next-token cross-entropy using document-level splits. Adapt by fine-tuning the output head first, then embeddings/hidden layers if justified. Tune regularization and learning rate on held-out loss; domain-only updates can erase rare general vocabulary behavior.

How to use and calibrate

Compare held-out cross-entropy with n-gram baselines and inspect domain shifts; embedding-neighbor plots are exploratory rather than proof of linguistic understanding. 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

Model short product-review phrases with limited compute and compare against a smoothed bigram baseline.

Interpretation / pitfalls

Watch out

Large output vocabularies make normalization expensive, while rare-word embeddings receive weak supervision. 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: Implement the embedding lookup, dense layer, and softmax in PyTorch or R torch. A matrix-only implementation is possible for a tiny vocabulary; full original training settings require explicit reproduction work.

Example scope: One fixed-window neural predictor forward pass with supplied weights; no training.

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 — One fixed-window neural predictor forward pass with supplied weights; no training.

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)
x = [0.2, -0.4]
h = [math.tanh(v) for v in matvec([[0.5, 0.1], [-0.2, 0.7]], x)]
p = softmax(matvec([[1, 0], [0, 1], [-1, 0.5]], h))
print(p, -math.log(p[0]))
R

Component demo, not a trained model — One fixed-window neural predictor forward pass with supplied weights; no training.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
x <- c(0.2, -0.4)
h <- tanh(matrix(c(0.5, 0.1, -0.2, 0.7), 2, byrow=TRUE) %*% x)
p <- softmax(drop(matrix(c(1,0,0,1,-1,0.5), 3, byrow=TRUE) %*% h))
print(list(probabilities=p, loss=-log(p[1])))

Sources

From the CodeSplash reference, entry LNG002.