Category
Use Category for a single label from a bounded vocabulary: class labels, country codes, product families, merchant IDs, and similar discrete values.
{
"merchant": "ACME_MARKET",
"merchant_id": "12345"
}import json2vec as jv
merchant = jv.Category(
"merchant",
size=4096,
topk=[3, 10],
p_unavailable=0.02,
)
merchantmerchant [category] active
pooling=query weight=1 p_mask=0 p_prune=0 n_heads=4 n_linear=1
size=4096 p_unavailable=0.02 topk=[3, 10]
Use Category when exactly one label is present. Use Set when the field can contain multiple labels, and use Entity when the goal is local equality matching inside repeated records rather than a persistent vocabulary.
Input Values
Category expects scalar labels. Strings are the normal input. Use a preprocessor if upstream labels or codes need to be converted into stable strings or otherwise cleaned. None is encoded as a null state, and missing branch positions are encoded as padded state.
Examples
Common category fields include:
- A supervised class label such as
fraud,churn,species, ordiagnosis. - A bounded business label such as customer tier, region, product family, or channel.
- A stable categorical code such as country, currency, device type, or merchant category.
Avoid using Category for massive identifiers such as customer IDs, account IDs, session IDs, or device IDs. A large global vocabulary is memory-heavy and usually generalizes poorly. Keep unique IDs as metadata, or use Entity when the model only needs local equality within a repeated context.
target = jv.Category("fraud", target=True, size=3, topk=[2])
targetfraud [category] active target
pooling=query weight=1 p_mask=0 p_prune=1 n_heads=4 n_linear=1
size=3 p_unavailable=0.01 topk=[2]
Configuration
| Option | Default | Notes |
|---|---|---|
size |
1024 |
Maximum number of learned labels. One extra internal bucket is reserved for unavailable labels. |
p_unavailable |
0.01 |
During training, randomly routes known labels to the unavailable bucket so the decoder learns that case. |
topk |
[] |
Optional top-k accuracy metrics and prediction candidates. Values must be positive, not 1, and less than size. |
Target Behavior
When a Category is masked or used as a target, the decoder predicts:
state: probabilities forvalued,null,padded, andmasked.content: a categorical distribution over learned vocabulary labels plus the internal unavailable bucket.
Top-k metrics are tracked for each configured value in topk.
Top-K Nuances
topk serves two roles:
- During training and evaluation,
json2vectracks a separate top-k accuracy metric for each configured value. - During prediction, the output contains one candidate list sized to
max(topk), capped by the current learned vocabulary size.
topk=1 is disallowed because top-1 is already represented by the primary content.value and content.probability fields. Values must be less than size so the requested candidates stay inside the learned label space. Duplicate values are normalized away.
The reserved unavailable bucket can affect training loss and metrics, but it is not emitted as a prediction candidate.
Prediction Output
Rendered examples use string paths for readability. The Python dictionary returned by Model.predict(...) is keyed by jv.Address objects.
Model.predict(...) returns the most likely known label, its probability, and the candidate list requested by topk:
{
"record/merchant": {
"state": {"valued": ..., "null": ..., "padded": ..., "masked": ...},
"content": {
"value": ...,
"probability": ...,
"topk": ...,
},
}
}{"record/merchant": {"state": {"valued": "text/plain:Ellipsis", "null": "text/plain:Ellipsis", "padded": "text/plain:Ellipsis", "masked": "text/plain:Ellipsis"}, "content": {"value": "text/plain:Ellipsis", "probability": "text/plain:Ellipsis", "topk": "text/plain:Ellipsis"}}}The special <unavailable> token is not emitted as a predicted label.
Notes
Use Set when a field can contain multiple labels (such that categories are not mutually exclusive). Use Entity when the goal is local identity matching inside repeated records rather than a persistent global vocabulary. Use Text if you want to break category labels down into word pieces.