OnT (Language Models as Ontology Encoder)
Project Overview
OnT is a language model-based framework for ontology embeddings, enabling effective representation of concepts as points in hyperbolic space and axioms as hierarchical relationships between concepts. Built upon the HierarchyTransformer, this implementation provides enhanced capabilities for ontological reasoning through specialized embedding techniques such as concept rotation, transition, and existential quantifier representation. The model has been trained on various biomedical ontologies including GO, GALEN, and ANATOMY datasets. For the test of geometric-embedding methods, please refer to the BoxSquaredEL and TransBox.
Features
- Hyperbolic embeddings for ontology concept encoding
- Modeling of hierarchical relationships between concepts
- Support for role embeddings as rotations over hyperbolic spaces
Installation via pip
The easiest way to get started is to install the package from PyPI ontology-transformer:
pip install ontology-transformer
The distribution is named ontology-transformer, while its Python import package is
lowercase ont. The root-level OnT.py file is retained as legacy research code and
is not the module installed from PyPI.
You can fine-tune the model directly from your OWL ontology file without any manual preprocessing:
from ont import OntologyTransformer
# Simply provide your OWL file path - that's it!
model = OntologyTransformer.fit(
owl_path="path/to/your/ontology.owl", # Your ontology file
output_dir="./my_ontology_model",
num_epochs=5,
batch_size=128,
eval_ratio=0.1 # Use 10% for evaluation
)
# The model is now fine-tuned on your ontology
# Use it immediately for encoding
embeddings = model.encode(["YourConcept1", "YourConcept2"])
# Save for later use
model.save("./my_ontology_model/final")
Project Structure
The data and models folders should be downloaded and unzipped to the root directory. The Google Drive links are all anonymous.
-
OnT.py: Main model implementation containing the OntologyTransformer class -
data/: Contains training and testing data, download from here or from Zenodo View Dataset on Zenodo -
models/: Stores pre-trained and fine-tuned models, download from here or from huggingface View Models on Hugging Face -
normalization/: Scripts for normalizing the EL part of a given ontology. Seenormalization/Readme.mdfor details.
Installation from Github
We recommend using Conda for a reproducible setup. The steps below install PyTorch, HierarchyTransformers, and a compatible sentence-transformers version, then perform a quick verification.
1) Create and activate Conda environment
conda create -y -n ont python=3.12
conda activate ont
2) Install PyTorch (GPU, CUDA 12.1)
conda install -y pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
3) Install HierarchyTransformers and sentence-transformers
ontology-transformersupportssentence-transformers>=3.0, including 6.x; no 3.4.0 pin is required.
pip install git+https://github.com/KRR-Oxford/HierarchyTransformers.git
pip install 'sentence-transformers>=3.0'
Usage
Then load the model and use it for inference or training as follows.
import torch
from ont import OntologyTransformer
# Load from Hugging Face (recommended)
ont = OntologyTransformer.from_pretrained('Hui97/OnT-MiniLM-L12-galen')
# entity names to be encoded.
entity_names = ["continuant", "occurrent", "independent continuant", "process"]
# get the entity embeddings
entity_embeddings = ont.encode(entity_names)
# role sentences to be encoded.
role_sentences = ["application attribute", "attribute", "chemical modifier", "chemical process modifier attribute"]
# get the role embeddings, consist of the rotation and scaling (regarded as 1 by default for model uploaded to huggingface)
role_rotations, _ = ont.encode_roles(role_sentences)
For training, run the following command. Remember to update the dataset_path and dataset_name in config.yaml :
python train_ont.py -c config.yaml
Available Pre-trained Models
The following models are available on Hugging Face and can be loaded directly:
| Model Name | Base Model | Dimension | Training Dataset |
|---|---|---|---|
Hui97/OnT-MPNet-galen |
MPNet | 768 | GALEN |
Hui97/OnT-MPNet-anatomy |
MPNet | 768 | ANATOMY |
Hui97/OnT-MPNet-go |
MPNet | 768 | GO |
Hui97/OnT-MiniLM-L12-galen |
MiniLM-L12 | 384 | GALEN |
Hui97/OnT-MiniLM-L12-anatomy |
MiniLM-L12 | 384 | ANATOMY |
Hui97/OnT-MiniLM-L12-go |
MiniLM-L12 | 384 | GO |
All models can be loaded using:
from ont import OntologyTransformer
ont = OntologyTransformer.from_pretrained('Hui97/OnT-MiniLM-L12-galen')
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ontology_transformer-0.1.7.tar.gz.
File metadata
- Download URL: ontology_transformer-0.1.7.tar.gz
- Upload date:
- Size: 33.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfe4618af490b371c2c848ee8030794281b45d90151f2d0632aada6d2217b6bd
|
|
| MD5 |
6e7fa1d33c0c7e6abdddfea7bcbacdf6
|
|
| BLAKE2b-256 |
651efdf64101e9d4b531a24b90e827a8a59463faeb94ae25b5feaeef16af4db6
|
File details
Details for the file ontology_transformer-0.1.7-py3-none-any.whl.
File metadata
- Download URL: ontology_transformer-0.1.7-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad56fc23a4148099513c719098d8b2b655203f4dea088c0e5e92c6a13fd7fa9c
|
|
| MD5 |
d0b51dae7e439134345faa9cfe0399d2
|
|
| BLAKE2b-256 |
187c253c4603b1fe95f8fff89c51ded180ea86ea30c02a2f6378c60ba87c2485
|