{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "73e2d6cb-82b5-42cc-919d-dceaaf1f09f7", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/apapagiannis/update_2025/eurovoc_training/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n", "Loading weights: 100%|██████████| 197/197 [00:00<00:00, 8783.65it/s]\n", "[transformers] \u001b[1mBertModel LOAD REPORT\u001b[0m from: ./merged_model\n", "Key | Status | \n", "-------------------------------------------+------------+-\n", "cls.predictions.transform.dense.bias | UNEXPECTED | \n", "cls.predictions.transform.LayerNorm.bias | UNEXPECTED | \n", "cls.predictions.transform.LayerNorm.weight | UNEXPECTED | \n", "cls.predictions.transform.dense.weight | UNEXPECTED | \n", "cls.predictions.bias | UNEXPECTED | \n", "pooler.dense.bias | MISSING | \n", "pooler.dense.weight | MISSING | \n", "\n", "Notes:\n", "- UNEXPECTED:\tcan be ignored when loading from different task/architecture; not ok if you expect identical arch.\n", "- MISSING:\tthose params were newly initialized because missing from the checkpoint. Consider training on your downstream task.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "energy vs energy: 0.7808\n", "energy vs criminal: 0.5920\n" ] } ], "source": [ "import torch\n", "from transformers import AutoModel, AutoTokenizer\n", "\n", "model_dir = \"./merged_model\"\n", "\n", "# What you actually want — embedding quality\n", "model = AutoModel.from_pretrained(model_dir) # NOT AutoModelForMaskedLM\n", "tokenizer = AutoTokenizer.from_pretrained(model_dir)\n", "\n", "def embed(text):\n", " inputs = tokenizer(text, return_tensors=\"pt\", truncation=True, max_length=512)\n", " with torch.no_grad():\n", " out = model(**inputs)\n", " return out.last_hidden_state[:, 0, :] # CLS token\n", "\n", "# Test: similar legal concepts should be closer than unrelated ones\n", "from torch.nn.functional import cosine_similarity\n", "\n", "e1 = embed(\"renewable energy regulation\")\n", "e2 = embed(\"solar power and wind policy\") # should be close\n", "e3 = embed(\"criminal sentencing guidelines\") # should be far\n", "\n", "print(f\"energy vs energy: {cosine_similarity(e1, e2).item():.4f}\") # expect ~high\n", "print(f\"energy vs criminal: {cosine_similarity(e1, e3).item():.4f}\") # expect ~lower" ] }, { "cell_type": "code", "execution_count": null, "id": "94556c9a-bd65-489f-b2ab-d44b9b0812c7", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "eurovoc_training", "language": "python", "name": "my-venv" }, "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }