Skip to main content

OnT (Language Models as Ontology Encoder)

Hugging Face DOI

Quickstart in Google Colab

Open In Colab

notebooks/OnT_Quickstart.ipynb shows the whole workflow in four steps: install the package, build a small pizza ontology and train OnT on it with one fit(...) call, embed concepts (including ∃r.C), and score subsumptions C ⊑ D. It runs on the free Colab CPU runtime in about two to three minutes.

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. See normalization/Readme.md for 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-transformer supports sentence-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')

Metadata

Release files for ontology-transformer 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ontology-transformer 0.1.8
File Size Uploaded
ontology_transformer-0.1.8.tar.gz 35.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ontology-transformer 0.1.8
File Interpreter ABI Platform
ontology_transformer-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 66.7 kB

Release files / ontology_transformer-0.1.8.tar.gz

Download URL ontology_transformer-0.1.8.tar.gz
Size 35.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d91993ffd23d151c8162aedc26093d744a689b73b81222064125c2e9aab6e1ec
BLAKE2b-256 checksum
How to use checksums
424d6ac55b89d8f607478309a3a073046890615edf0ddba74d1131257699f645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.4

Release files / ontology_transformer-0.1.8-py3-none-any.whl

Download URL ontology_transformer-0.1.8-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
311704ee35c7f07c1efa8c6893e638c6bd71127fadbf7234eb377284aa2a3a8d
BLAKE2b-256 checksum
How to use checksums
bf1d122e63bd84dac75a7e0ce63fe203b4a25b7fb7c59c317296ddcfb139e389
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.4

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page