Feature Extraction
sentence-transformers
ONNX
Safetensors
Transformers
Transformers.js
English
bert
sentence-similarity
text-embeddings-inference
information-retrieval
knowledge-distillation
Instructions to use MongoDB/mdbr-leaf-mt with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use MongoDB/mdbr-leaf-mt with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("MongoDB/mdbr-leaf-mt") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Transformers
How to use MongoDB/mdbr-leaf-mt with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="MongoDB/mdbr-leaf-mt")# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("MongoDB/mdbr-leaf-mt") model = AutoModel.from_pretrained("MongoDB/mdbr-leaf-mt", device_map="auto") - Transformers.js
How to use MongoDB/mdbr-leaf-mt with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('feature-extraction', 'MongoDB/mdbr-leaf-mt'); - Inference
- Notebooks
- Google Colab
- Kaggle
File size: 4,670 Bytes
c342f94 05cae9a c342f94 05cae9a c342f94 05cae9a c342f94 05cae9a c342f94 05cae9a c342f94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2a12a2b3",
"metadata": {},
"outputs": [],
"source": [
"from safetensors import safe_open\n",
"import torch\n",
"from torch.nn import functional as F\n",
"from transformers import AutoModel, AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "148ce181",
"metadata": {},
"outputs": [],
"source": [
"# First clone the model locally\n",
"!git clone https://huggingface.co/MongoDB/mdbr-leaf-mt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba9ec6c7",
"metadata": {},
"outputs": [],
"source": [
"# Then load it\n",
"MODEL = \"mdbr-leaf-mt\"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(MODEL)\n",
"model = AutoModel.from_pretrained(MODEL, add_pooling_layer=False)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ebaf1a76",
"metadata": {},
"outputs": [],
"source": [
"tensors = {}\n",
"with safe_open(MODEL + \"/2_Dense/model.safetensors\", framework=\"pt\") as f:\n",
" for k in f.keys():\n",
" tensors[k] = f.get_tensor(k)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03ffcd9c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Similarities:\n",
"tensor([[0.9063, 0.7287],\n",
" [0.6725, 0.8287]])\n"
]
}
],
"source": [
"if 'linear.bias' in tensors:\n",
" W_out = torch.nn.Linear(in_features=384, out_features=1024, bias=True)\n",
" W_out.load_state_dict({\n",
" \"weight\": tensors[\"linear.weight\"], \n",
" \"bias\": tensors[\"linear.bias\"]\n",
" })\n",
"else:\n",
" W_out = torch.nn.Linear(in_features=384, out_features=1024, bias=False)\n",
" W_out.load_state_dict({\n",
" \"weight\": tensors[\"linear.weight\"]\n",
" })\n",
"\n",
"_ = model.eval()\n",
"_ = W_out.eval()\n",
"\n",
"# Example queries and documents \n",
"queries = [\n",
" \"What is machine learning?\", \n",
" \"How does neural network training work?\" \n",
"] \n",
" \n",
"documents = [ \n",
" \"Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data.\", \n",
" \"Neural networks are trained through backpropagation, adjusting weights to minimize prediction errors.\" \n",
"]\n",
"\n",
"# Tokenize\n",
"QUERY_PREFIX = 'Represent this sentence for searching relevant passages: '\n",
"queries_with_prefix = [QUERY_PREFIX + query for query in queries]\n",
"\n",
"query_tokens = tokenizer(queries_with_prefix, padding=True, truncation=True, return_tensors='pt', max_length=512)\n",
"document_tokens = tokenizer(documents, padding=True, truncation=True, return_tensors='pt', max_length=512)\n",
"\n",
"# Perform Inference\n",
"with torch.inference_mode():\n",
" y_queries = model(**query_tokens).last_hidden_state\n",
" y_docs = model(**document_tokens).last_hidden_state\n",
"\n",
" # perform pooling\n",
" y_queries = y_queries * query_tokens.attention_mask.unsqueeze(-1)\n",
" y_queries_pooled = y_queries.sum(dim=1) / query_tokens.attention_mask.sum(dim=1, keepdim=True)\n",
"\n",
" y_docs = y_docs * document_tokens.attention_mask.unsqueeze(-1)\n",
" y_docs_pooled = y_docs.sum(dim=1) / document_tokens.attention_mask.sum(dim=1, keepdim=True)\n",
"\n",
" # map to desired output dimension\n",
" query_embeddings = W_out(y_queries_pooled)\n",
" document_embeddings = W_out(y_docs_pooled)\n",
"\n",
"similarities = F.cosine_similarity(query_embeddings.unsqueeze(0), document_embeddings.unsqueeze(1), dim=-1).T\n",
"print(f\"Similarities:\\n{similarities}\")\n",
"\n",
"# Similarities:\n",
"# tensor([[0.9063, 0.7287],\n",
"# [0.6725, 0.8287]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a2b0244",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "alexis",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|