TensorFlow
FastText
word embeddings
NLP
machine learning

Use Tensorflow and pre-trained FastText to get embeddings of unseen words

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

FastText is useful for unseen words because it builds word vectors from character n-grams rather than relying only on a fixed word lookup table. TensorFlow does not generate those out-of-vocabulary vectors by itself, but it can consume them once FastText has produced them.

What FastText Contributes

A traditional embedding matrix only knows words that appeared during training. If a new token arrives, the model often falls back to one unknown-token vector.

FastText works differently. It represents a word using subword pieces, which lets it generate a vector even for a token it never saw as a full word. That is the main reason people pair pre-trained FastText models with downstream TensorFlow models.

Examples of words that benefit from this behavior:

  • misspellings
  • inflected forms
  • domain-specific compounds
  • new product or feature names

The important conceptual correction is this: FastText generates the embedding. TensorFlow is the framework you use around it.

Load a Pre-Trained FastText Model

With the Python fasttext package, loading a binary model is straightforward.

python
1import fasttext
2import tensorflow as tf
3
4model = fasttext.load_model("cc.en.300.bin")
5word = "microservicification"
6vector = model.get_word_vector(word)
7tensor = tf.convert_to_tensor(vector, dtype=tf.float32)
8
9print(tensor.shape)  # (300,)

Even if microservicification was not present as a whole token during FastText training, get_word_vector can still return a meaningful vector because of its subword model.

Turn Words Into TensorFlow Inputs

If you are building a TensorFlow model, you usually want to convert a batch of tokens into a batch of vectors.

python
1import numpy as np
2import fasttext
3import tensorflow as tf
4
5model = fasttext.load_model("cc.en.300.bin")
6words = ["service", "containerized", "microservicification"]
7
8vectors = np.stack([model.get_word_vector(word) for word in words])
9inputs = tf.convert_to_tensor(vectors, dtype=tf.float32)
10
11print(inputs.shape)  # (3, 300)

That tensor can now be fed into a Keras model that expects dense embeddings.

For a simple classifier head:

python
1classifier = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(300,)),
3    tf.keras.layers.Dense(128, activation="relu"),
4    tf.keras.layers.Dense(3, activation="softmax"),
5])
6
7outputs = classifier(inputs)
8print(outputs.shape)  # (3, 3)

This is often enough when each example is already represented by one word or one pre-aggregated vector.

Handling Whole Sentences

For sentence or document tasks, you need one more step. A common baseline is to average the FastText vectors of all tokens in the sentence, then feed that single vector into TensorFlow.

python
1import numpy as np
2
3
4def sentence_vector(text: str) -> np.ndarray:
5    tokens = text.lower().split()
6    word_vectors = [model.get_word_vector(token) for token in tokens]
7    return np.mean(word_vectors, axis=0)
8
9
10text = "new microservicification rollout"
11text_tensor = tf.convert_to_tensor([sentence_vector(text)], dtype=tf.float32)
12print(text_tensor.shape)  # (1, 300)

That is a baseline, not the only design. More advanced pipelines may keep token-level vectors and pass sequences into recurrent or transformer-style models, but the underlying unseen-word vector still comes from FastText.

What TensorFlow Should and Should Not Do Here

TensorFlow is good at:

  • storing the vectors as tensors
  • batching them efficiently
  • training downstream models on top of them
  • combining them with other learned features

TensorFlow is not the mechanism that gives FastText its unseen-word behavior. If you replace FastText with a plain fixed embedding matrix, the unseen-word benefit goes away unless you add another subword strategy.

Common Pitfalls

The biggest mistake is assuming a pre-trained FastText .vec text file alone gives you all out-of-vocabulary behavior. The subword-based inference is preserved when you use the proper FastText model format and library methods, not just a frozen lookup table exported as plain text.

Another mistake is expecting TensorFlow's Embedding layer to magically reproduce FastText behavior. A normal embedding layer only looks up rows by index. It does not decompose unseen tokens into character n-grams.

People also forget preprocessing consistency. If the FastText model was trained on lowercased text and your inference pipeline is not lowercasing, your results may be noisier than expected.

Finally, unseen-word vectors are useful, but they are not magic. For highly technical or multilingual domains, the pre-trained model still has to be reasonably aligned with the language you care about.

Summary

  • FastText, not TensorFlow, is what provides unseen-word embeddings.
  • Pre-trained FastText models can produce vectors for out-of-vocabulary words using subword information.
  • TensorFlow is then used to batch, store, and model those vectors.
  • Use the FastText binary model and get_word_vector if you want real unseen-word behavior.
  • Keep text preprocessing consistent so the generated embeddings stay meaningful.

Course illustration
Course illustration

All Rights Reserved.