keras-team/keras · error · ValueError
Unknown vectorization mode:
Error message
Unknown vectorization mode:
What it means
sequences_to_matrix accepts only the modes 'binary', 'count', 'tfidf', and 'freq'; anything else falls through to raise ValueError('Unknown vectorization mode:', mode). Note the Keras bug of passing mode as a second positional argument to ValueError, so it shows as a tuple in the traceback rather than in the message text.
Source
Thrown at keras/src/legacy/preprocessing/text.py:281
counts[j] += 1
for j, c in list(counts.items()):
if mode == "count":
x[i][j] = c
elif mode == "freq":
x[i][j] = c / len(seq)
elif mode == "binary":
x[i][j] = 1
elif mode == "tfidf":
# Use weighting scheme 2 in
# https://en.wikipedia.org/wiki/Tf%E2%80%93idf
tf = 1 + np.log(c)
idf = np.log(
1
+ self.document_count / (1 + self.index_docs.get(j, 0))
)
x[i][j] = tf * idf
else:
raise ValueError("Unknown vectorization mode:", mode)
return x
def get_config(self):
json_word_counts = json.dumps(self.word_counts)
json_word_docs = json.dumps(self.word_docs)
json_index_docs = json.dumps(self.index_docs)
json_word_index = json.dumps(self.word_index)
json_index_word = json.dumps(self.index_word)
return {
"num_words": self.num_words,
"filters": self.filters,
"lower": self.lower,
"split": self.split,
"char_level": self.char_level,
"oov_token": self.oov_token,
"document_count": self.document_count,
"word_counts": json_word_counts,View on GitHub (pinned to 7a34a03db6)
Solutions
- Use one of the exact lowercase strings: 'binary', 'count', 'tfidf', 'freq'
- Check for hyphens and casing like 'tf-idf' or 'Tfidf'
- For one-hot behavior use to_categorical on sequences instead
Example fix
# before tok.texts_to_matrix(texts, mode='tf-idf') # after tok.texts_to_matrix(texts, mode='tfidf')
Defensive patterns
Strategy: type-guard
Validate before calling
MODES = {'binary', 'count', 'tfidf', 'freq'}
if mode not in MODES:
raise ValueError(f'mode must be one of {MODES}') Type guard
def valid_mode(m):
return m in {'binary', 'count', 'tfidf', 'freq'} Prevention
- Centralize mode strings as constants instead of inline literals
When it happens
Trigger: Calling texts_to_matrix(texts, mode='tf-idf') (hyphen), mode='TFIDF' (uppercase), or a nonexistent mode like 'onehot'.
Common situations: Typos in the mode string; copying 'tf-idf' from other libraries' docs; case-sensitivity surprises.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Unrecognized keyword arguments: {str(kwargs)}
- Specify a dimension (`num_words` argument), or fit on some t
- Fit the Tokenizer on some data before using tfidf mode.
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e574d09a0cb21032.
Report an issue: GitHub.