Skip to main content

Prime Adjacency Matrix for Multi-relational Networks

Project description

PAM

A bare-bones implementation of the PAM framework, i.e. Prime Adjacency Matrices for Multi-relational Networks.

Mainly used for testing. Check each .py in its "main"" component where we call each of the major functions for their functionalities.

It works as it is for any collection of triples in the form of a .txt where each line corresponds to one triple in the form:

ent1, rel1, ent2,
ent2, rel2, ent3

The delimiters can change. Please see load_data.py file for this.

You can check each of the .py files for the specific functionality you are interested in. For example, running:

from data_loading import load_data
from pam_creation import create_pam_matrices
from rule_generation import create_ruleset
from utils import calculate_hits_at_k

path = "../test/dummy_data"
project_name = "test"

df_train_orig, df_train, df_eval, df_test, already_seen_triples = load_data(
    path, project_name=project_name, add_inverse_edges='NO
)
print(f"\nLoaded Data, will create PAMs... \n")

pam_1hop_lossless, pam_powers, node2id, rel2id = create_pam_matrices(
    df_train, use_log=False, max_num_hops=3
)
print(f"\nCreated PAMs, will generate rules... \n")

all_rules_df = create_ruleset(
    pam_1hop_lossless, pam_powers, use_log=False, max_num_hops=-1
)
print(f"\nCreated {all_rules_df.shape[0]} rules, will generate predictions...  \n")

k_hop_pams = [pam_1hop_lossless] + pam_powers[1:]

predict_tail_with_explanations(
    df_test, all_rules_df, k_hop_pams, node2id, rel2id, rank_rules_by_="score"
)

Will load the dummy data, calculate up to 3-hop PAMs, generate rules, predict possible tail candidates for each the two test queries in the ./test/dummy_data/test.txt file and print those predictions...

Specifically, you will get:

No valid.txt found in ../test/dummy_data... df_eval will contain the train data..
Total: 9 triples in train + eval!)
In train: 9
In valid: 9
In test: 2

Loaded Data, will create PAMs... 

# of unique rels: 3      | # of unique nodes: 5
(5, 5) Sparsity: 68.00 %
Hop 2
Sparsity 2-hop: 76.00 %
Hop 3
Sparsity 3-hop: 88.00 %
Hop 4
Sparsity 4-hop: 96.00 %
Hop 5
Sparsity 5-hop: 100.00 %

Created PAMs, will generate rules... 

100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 806.19it/s]

Created 12 rules, will generate predictions...  

Query (0): Nick - knows - Anna
(Correct Match) 0.4000 : knows(Nick, B ) ^ knows( B ,Anna) -> knows(Nick,Anna)



Query (1): George - lives_in - Athens
(Correct Match) 0.5000 : works_in(George,Athens) -> lives_in(George,Athens)



100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00, 300.44it/s]
Tail Hits@1: 100.00 %
Tail Hits@3: 100.00 %
Tail Hits@5: 100.00 %
Tail Hits@10: 100.00 %
Tail Hits@UpperBound: 100.00 %

TODOS:

  1. Add consistent documentation and move examples from the main sections of each .py to dedicated files or notebooks.
  2. Check the effect of eliminate zeros, sort_indices in create_pam_matrices function.
  3. Link prediction
    1. Add filtering cache mechanism.
    2. Parallelize (WN18RR takes about 2.8 mins)
    3. Refactor tail prediction to link prediction

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prime_adj-0.0.3.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

prime_adj-0.0.3-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file prime_adj-0.0.3.tar.gz.

File metadata

  • Download URL: prime_adj-0.0.3.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.5

File hashes

Hashes for prime_adj-0.0.3.tar.gz
Algorithm Hash digest
SHA256 84ed3b34cc72bb21640b12ff7614d004dfc07dbe9a30542db791df0df7aa7a39
MD5 7938bf142c797c98b2de9216349ffe39
BLAKE2b-256 9ab90a0308b929da70878e8c93b5dc83ecdbdcbc5ae9218a02583a5a0fc46c5d

See more details on using hashes here.

File details

Details for the file prime_adj-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: prime_adj-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.5

File hashes

Hashes for prime_adj-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5ebe94965f72e59196d2526bc0fe7966874c5486135bfbd8059b9e8a50d501c1
MD5 5edda2004301b2ba327ddb3359be1b63
BLAKE2b-256 dda34f25ea9cffe031916c370aa6cad5b9ad338889b109f1ff2b15ce0ca4d747

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page