Giter VIP home page Giter VIP logo

tkbc-lie's Introduction

Mitigating Heterogeneity among Factor Tensors via Lie Group Manifolds for Tensor Decomposition Based Temporal Knowledge Graph Embedding

Static Badge

Installation

Create a conda environment with pytorch and scikit-learn :

conda create --name tkbc_env python=3.x
source activate tkbc_env
conda install --file requirements.txt -c pytorch

Then install the kbc package to this environment

python setup.py install

Datasets

To download the datasets, go to the tkbc/scripts folder and run:

chmod +x download_data.sh
./download_data.sh

Once the datasets are downloaded, add them to the package data folder by running :

python tkbc/process.py

Reproducing results

python tkbc/learner.py --dataset ICEWS14 --model TeAST (TComplEx & TNTcomplEx & TeLM(rank=121)) --rank 128 --emb_reg 1e-2 --time_reg 1e-2

python tkbc/learner.py --dataset ICEWS05-15 --model TeAST (TComplEx & TNTcomplEx & TeLM(rank=121)) --rank 128 --emb_reg 1e-3 --time_reg 1

License

MIT licensed, as found in the LICENSE file.

Our Method

# Copyright (c) Mitigating Heterogeneity among Factor Tensors via Lie Group Manifolds for Tensor Decomposition Based Temporal Knowledge Graph Embedding

import torch

class MatrixOperationsLie:
    def __init__(self, mat_n):
        """
        Initialize the MatrixOperations class.
        """
        self.mat_n = mat_n

    def map_to_lie(self, embeddings):
        """
        Maps the embeddings to a Lie group representation.
        """
        a, b, c, d = embeddings.chunk(4, dim=-1)
        
        a = torch.cos(a)
        b = -torch.sin(b)
        c = torch.sin(c)
        d = torch.cos(d)
        
        rotation_matrix = torch.cat((a, b, c, d), dim=-1)
        rotation_matrix = self.matrix_logarithm(rotation_matrix)
        return rotation_matrix

    def matrix_logarithm(self, R_batch):
        """
        Computes the matrix logarithm of a batch of rotation matrices.
        """
        R_batch = R_batch.contiguous().view(-1, self.mat_n, self.mat_n)
        R_batch = R_batch / R_batch.norm(dim=(1, 2), keepdim=True)
        traces = R_batch.diagonal(dim1=-2, dim2=-1).sum(-1)
        theta = torch.acos((traces - 1) / 2)
        epsilon = 1e-8
        sin_theta = torch.sin(theta) + epsilon
        close_to_zero = theta < epsilon
        log_R_batch = torch.zeros_like(R_batch)
        log_R_batch[~close_to_zero] = theta[~close_to_zero].unsqueeze(-1).unsqueeze(-1) \
                                      / (2 * sin_theta[~close_to_zero].unsqueeze(-1).unsqueeze(-1)) \
                                      * (R_batch[~close_to_zero] - R_batch[~close_to_zero].transpose(-2, -1))

        assert not torch.isnan(log_R_batch).any(), "log_R_batch contains nan values."
        return log_R_batch.contiguous().view(-1, self.mat_n * self.mat_n)

tkbc-lie's People

Contributors

dellixx avatar

Stargazers

JasonGuo avatar sxdkxgwan2 avatar  avatar  avatar

Watchers

Kostas Georgiou avatar  avatar

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.