Language models, retrieval, prompting, and language adaptation · Count-based and distributed representations · LNG006 · Model
fastText subword embeddings
Extends static word embeddings using character n-grams, allowing vectors for rare or previously unseen words. It is particularly useful for morphologically rich text and spelling variation, while remaining much cheaper…
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- Word vectors Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- Verified in Python and R through discover.word_vectors. 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
Extends static word embeddings using character n-grams, allowing vectors for rare or previously unseen words. It is particularly useful for morphologically rich text and spelling variation, while remaining much cheaper than contextual encoders.
Question / objective
Can subword composition improve embeddings for rare or unseen word forms?
Architecture / computational structure
Each word is represented through character n-gram vectors, optionally with a word vector. Learned subword/word tables supply the prediction objective; n-gram range and hash-bucket size are hyperparameters.
Assumptions
Shared spelling does not guarantee shared meaning; hash collisions and unusual scripts affect behavior.
Formula / computational definition
Represent word w by v_w=sum_{g in G_w} z_g, where G_w includes character n-grams and optionally the word token. Train a skip-gram-style prediction objective using the summed representation and context vectors.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Character n-gram range, hashing buckets, dimension, context window, vocabulary cutoff, and negative-sampling settings.
Fitting, fine-tuning, and adaptation
Train on domain text or continue a compatible pretrained model. Preserve tokenization and subword hashing when adapting; changing those definitions invalidates stored vectors. Freeze representations for small labeled tasks before considering updates. Validate rare-word and morphological behavior separately from common words.
How to use and calibrate
Measure rare-word and out-of-vocabulary performance separately, including robustness to typographical changes and language-specific tokenization. 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
Represent inflected biomedical terms that are absent from a fixed word vocabulary.
Interpretation / pitfalls
Watch out
Out-of-vocabulary availability is not the same as semantic accuracy, and sentence context still does not change the word vector. 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: Official fastText or Gensim FastText provides Python routes. R can use a verified wrapper or reticulate; native teaching code can sum n-gram vectors but should distinguish that demonstration from full optimized training.
Example scope: Subword-vector composition from supplied character n-gram embeddings; no embedding 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.
Component demo, not a trained model — Subword-vector composition from supplied character n-gram embeddings; no embedding 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)
subwords = {"<ca": [0.3, 0.0], "cat": [0.2, 0.4], "at>": [-0.1, 0.2]}
word_vector = [sum(v[j] for v in subwords.values()) for j in range(2)]
print(word_vector)
Component demo, not a trained model — Subword-vector composition from supplied character n-gram embeddings; no embedding training.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
subwords <- rbind(c(0.3,0), c(0.2,0.4), c(-0.1,0.2))
word_vector <- colSums(subwords)
print(word_vector)
Sources
- Source 1aclanthology.org
From the CodeSplash reference, entry LNG006.