Downloads · 30 days
13
4% of all-time downloads
nishtahir/ModernBERT-mtg-commander
ModernBERT-mtg-commander is a machine learning model from nishtahir. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
A fine-tune of modern-BERT trained in a Siamese Network (Bi-Encoder) architecture on card data to predict how likely it is to be included in a Commander deck. This models an embedding space that has a much richer sema…
Downloads · 30 days
13
4% of all-time downloads
All-time downloads
316
Public
Parameters
149M
1.8 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors596 MB · 99%
From the Hugging Face model README
A fine-tune of modern-BERT trained in a Siamese Network (Bi-Encoder) architecture on card data to predict how likely it is to be included in a Commander deck. This models an embedding space that has a much richer semantic understanding of Magic cards than the base model

Base model neighbors for 'Yuriko, the Tiger's Shadow':
(high density, low relevance)

Fine-tuned neighbors for 'Yuriko, the Tiger's Shadow':
(low density, high relevance)
import torch
import torch.nn.functional as F
from transformers import AutoModel, AutoTokenizer
def mean_pool(model_output, attention_mask):
token_embeddings = model_output.last_hidden_state
mask = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
summed = torch.sum(token_embeddings * mask, dim=1)
counts = torch.clamp(mask.sum(dim=1), min=1e-9)
return summed / counts
tokenizer = AutoTokenizer.from_pretrained("nishtahir/modern-BERT-MTG-Commander")
model = AutoModel.from_pretrained(model_name).eval()
# Cards are serialized in the Scryfall Text Format
card_data = """Commit {3}{U}
Instant
Put target spell or nonland permanent into its owner's library second from the top.
----
Memory {4}{U}{U}
Sorcery
Aftermath (Cast this spell only from your graveyard. Then exile it.)
Each player shuffles their hand and graveyard into their library, then draws seven cards.
"""
encoded = tokenizer([card_data],padding=True, truncation=True, return_tensors="pt")
ids = encoded["input_ids"].to(device)
mask = encoded["attention_mask"].to(device)
with torch.no_grad():
outputs = model(ids, mask)
# Mean pool to get a single embedding vector
pooled = mean_pool(outputs, mask)
# Normalize
embeddings = F.normalize(torch.cat(all_vecs, dim=0), p=2, dim=1)