Giter VIP home page Giter VIP logo

dobraczka / kiez Goto Github PK

View Code? Open in Web Editor NEW
24.0 5.0 3.0 925 KB

๐Ÿ˜๏ธ Hubness reduced nearest neighbor search for entity alignment with knowledge graph embeddings

Home Page: https://kiez.readthedocs.io/

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
entity-resolution embedding knowledge-graph hubness nearest-neighbors knowledge-graph-embedding approximate-nearest-neighbor-search entity-alignment

kiez's Introduction

kiez logo

Actions Status Documentation Status Stable python versions License BSD3 - Clause Code style: ruff

A Python library for hubness reduced nearest neighbor search for the task of entity alignment with knowledge graph embeddings. The term kiez is a german word that refers to a city neighborhood.

Hubness Reduction

Hubness is a phenomenon that arises in high-dimensional data and describes the fact that a couple of entities are nearest neighbors (NN) of many other entities, while a lot of entities are NN to no one. For entity alignment with knowledge graph embeddings we rely on NN search. Hubness therefore is detrimental to our matching results. This library is intended to make hubness reduction techniques available to data integration projects that rely on (knowledge graph) embeddings in their alignment process. Furthermore kiez incorporates several approximate nearest neighbor (ANN) libraries, to pair the speed advantage of approximate neighbor search with increased accuracy of hubness reduction.

Installation

You can install kiez via pip:

pip install kiez

If you have a GPU you can make kiez faster by installing faiss (if you do not already have it in your environment):

conda env create -n kiez-faiss python=3.10
conda activate kiez-faiss
conda install -c pytorch -c nvidia faiss-gpu=1.7.4 mkl=2021 blas=1.0=mkl
pip install kiez

For more information see their installation instructions.

You can also get other specific libraries with e.g.:

  pip install kiez[nmslib]

Usage

Simple nearest neighbor search for source entities in target space:

from kiez import Kiez
import numpy as np
# create example data
rng = np.random.RandomState(0)
source = rng.rand(100,50)
target = rng.rand(100,50)
# fit and get neighbors
k_inst = Kiez()
k_inst.fit(source, target)
nn_dist, nn_ind = k_inst.kneighbors(5)

Using (A)NN libraries and hubness reduction methods:

from kiez import Kiez
import numpy as np
# create example data
rng = np.random.RandomState(0)
source = rng.rand(100,50)
target = rng.rand(100,50)
# prepare algorithm and hubness reduction
k_inst = Kiez(n_candidates=10, algorithm="Faiss", hubness="CSLS")
# fit and get neighbors
k_inst.fit(source, target)
nn_dist, nn_ind = k_inst.kneighbors(5)

Torch Support

Beginning with version 0.5.0 torch can be used, when using Faiss as NN library:

    from kiez import Kiez
    import torch
    source = torch.randn((100,10))
    target = torch.randn((200,10))
    k_inst = Kiez(algorithm="Faiss", hubness="CSLS")
    k_inst.fit(source, target)
    nn_dist, nn_ind = k_inst.kneighbors()

You can also utilize tensor on the GPU:

    k_inst = Kiez(algorithm="Faiss", algorithm_kwargs={"use_gpu":True}, hubness="CSLS")
    k_inst.fit(source.cuda(), target.cuda())
    nn_dist, nn_ind = k_inst.kneighbors()

Documentation

You can find more documentation on readthedocs

Benchmark

The results and configurations of our experiments can be found in a seperate benchmarking repository

Citation

If you find this work useful you can use the following citation:

@article{obraczka2022fast,
  title={Fast Hubness-Reduced Nearest Neighbor Search for Entity Alignment in Knowledge Graphs},
  author={Obraczka, Daniel and Rahm, Erhard},
  journal={SN Computer Science},
  volume={3},
  number={6},
  pages={1--19},
  year={2022},
  publisher={Springer},
  url={https://link.springer.com/article/10.1007/s42979-022-01417-1},
  doi={10.1007/s42979-022-01417-1},
}

Contributing

PRs and enhancement ideas are always welcome. If you want to build kiez locally use:

git clone [email protected]:dobraczka/kiez.git
cd kiez
poetry install

To run the tests (given you are in the kiez folder):

poetry run pytest tests

Or install nox and run:

nox

which checks all the linting as well.

License

kiez is licensed under the terms of the BSD-3-Clause license. Several files were modified from scikit-hubness, distributed under the same license. The respective files contain the following tag instead of the full license text.

    SPDX-License-Identifier: BSD-3-Clause

This enables machine processing of license information based on the SPDX License Identifiers that are here available: https://spdx.org/licenses/

kiez's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kiez's Issues

Simplify deletion of ANN indices

Annoy and NGT create permanent indices in the file system. At the moment it is the user's responsibility to remove these. It would be nice to provide some function, that could be called in order to remove such files easily, when they are not needed anymore.

Update docs

  • Explain the new possibilities enabled by class-resolver
  • Incorporate architecture picture with explanation
  • link to readthedocs in readme
  • Give new examples (including for single-source)

Use case: assessing goodness of fit between two PyKEEN models

If I have two different embedding spaces describing the same entities, like if I train two models on the same dataset in PyKEEN, how can I use Kiez to assess how good they correspond? Or maybe there's a notion of how "good" the Kiez fit is?

A naive idea is I could I iterate through each entity and calculate the overlap coefficient of the nearest neighbors in both embedding spaces, then maybe report the average overlap coefficient. I'm sure I could come up with a few things like this, but I bet you know better! Any ideas appreciated.

I would start with code like this:

from pykeen.pipeline import pipeline
from pykeen.datasets import Nations

dataset = Nations()

# Train the same dataset with two different models
r1 = pipeline(
    model='TransE',
    dataset=dataset,
    epochs=1,  # change this to ~25 for real usage on Nations
)

r2 = pipeline(
    model='PairRE',
    dataset=dataset,
    epochs=1,  # change this to ~25 for real usage on Nations
) 

from kiez import Kiez

k_inst = Kiez()
k_inst.fit(
    r1.model.entity_representations[0]().detach().numpy(),
    r2.model.entity_representations[0]().detach().numpy(),
)

# How do I assess how well these spaces correspond? Is there a metric for how "good" the fit is?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.