README.md 7.84 KB
Newer Older
1
2
3
4
5
# DGL - Knowledge Graph Embedding


## Introduction

Da Zheng's avatar
Da Zheng committed
6
DGL-KE is a DGL-based package for computing node embeddings and relation embeddings of
7
8
9
10
11
12
knowledge graphs efficiently. This package is adapted from
[KnowledgeGraphEmbedding](https://github.com/DeepGraphLearning/KnowledgeGraphEmbedding).
We enable fast and scalable training of knowledge graph embedding,
while still keeping the package as extensible as
[KnowledgeGraphEmbedding](https://github.com/DeepGraphLearning/KnowledgeGraphEmbedding).
On a single machine,
Da Zheng's avatar
Da Zheng committed
13
14
15
16
it takes only a few minutes for medium-size knowledge graphs, such as FB15k and wn18, and
takes a couple of hours on Freebase, which has hundreds of millions of edges.

DGL-KE includes the following knowledge graph embedding models:
17
18
19
20
 
- TransE
- DistMult
- ComplEx
21
- RESCAL
22
- TransR
23

Da Zheng's avatar
Da Zheng committed
24
It will add other popular models in the future.
25
26
27

DGL-KE supports multiple training modes:

Da Zheng's avatar
Da Zheng committed
28
29
30
31
32
33
34
35
- CPU training
- GPU training
- Joint CPU & GPU training
- Multiprocessing training on CPUs

For joint CPU & GPU training, node embeddings are stored on CPU and mini-batches are trained on GPU. This is designed for training KGE models on large knowledge graphs

For multiprocessing training, each process train mini-batches independently and use shared memory for communication between processes. This is designed to train KGE models on large knowledge graphs with many CPU cores.
36
37
38
39
40
41

We will support multi-GPU training and distributed training in a near future.

## Requirements

The package can run with both Pytorch and MXNet. For Pytorch, it works with Pytorch v1.2 or newer.
Da Zheng's avatar
Da Zheng committed
42
For MXNet, it works with MXNet 1.5 or newer.
43
44
45
46
47

## Datasets

DGL-KE provides five knowledge graphs:

Da Zheng's avatar
Da Zheng committed
48
49
50
51
52
53
54
| Dataset | #nodes | #edges | #relations |
|---------|--------|--------|------------|
| [FB15k](https://s3.us-east-2.amazonaws.com/dgl.ai/dataset/FB15k.zip) | 14951 | 592213 | 1345 |
| [FB15k-237](https://s3.us-east-2.amazonaws.com/dgl.ai/dataset/FB15k-237.zip) | 14541 | 310116 | 237 |
| [wn18](https://s3.us-east-2.amazonaws.com/dgl.ai/dataset/wn18.zip) | 40943 | 151442 | 18 |
| [wn18rr](https://s3.us-east-2.amazonaws.com/dgl.ai/dataset/wn18rr.zip) | 40943 | 93003 | 11 |
| [Freebase](https://s3.us-east-2.amazonaws.com/dgl.ai/dataset/Freebase.zip) | 86054151 | 338586276 | 14824 |
55
56
57
58
59

Users can specify one of the datasets with `--dataset` in `train.py` and `eval.py`.

## Performance

Da Zheng's avatar
Da Zheng committed
60
The speed is measured with 16 CPU cores and one Nvidia V100 GPU.
61
62
63

The speed on FB15k

64
65
66
67
|  Models | TransE | DistMult | ComplEx | RESCAL | TransR |
|---------|--------|----------|---------|--------|--------|
|MAX_STEPS| 20000  | 100000   | 100000  | 30000  | 100000 |
|TIME     | 411s   | 690s     | 806s    | 1800s  | 7627s  |
68
69
70
71
72
73
74
75

The accuracy on FB15k

|  Models  |  MR   |  MRR  | HITS@1 | HITS@3 | HITS@10 |
|----------|-------|-------|--------|--------|---------|
| TransE   | 69.12 | 0.656 | 0.567  | 0.718  | 0.802   |
| DistMult | 43.35 | 0.783 | 0.713  | 0.837  | 0.897   |
| ComplEx  | 51.99 | 0.785 | 0.720  | 0.832  | 0.889   |
76
77
| RESCAL   | 130.89| 0.668 | 0.597  | 0.720  | 0.800   |
| TransR   | 138.7 | 0.501 | 0.274  | 0.704  | 0.801   |
78

79
80
In comparison, GraphVite uses 4 GPUs and takes 14 minutes. Thus, DGL-KE trains TransE on FB15k twice as fast as GraphVite while using much few resources. More performance information on GraphVite can be found [here](https://github.com/DeepGraphLearning/graphvite).

81
82
The speed on wn18

83
84
85
86
|  Models | TransE | DistMult | ComplEx | RESCAL | TransR |
|---------|--------|----------|---------|--------|--------|
|MAX_STEPS| 40000  | 10000    | 20000   | 20000  | 20000  |
|TIME     | 719s   | 126s     | 266s    | 333s   | 1547s  |
87
88
89
90
91
92
93
94

The accuracy on wn18

|  Models  |  MR    |  MRR  | HITS@1 | HITS@3 | HITS@10 |
|----------|--------|-------|--------|--------|---------|
| TransE   | 321.35 | 0.760 | 0.652  | 0.850  | 0.940   |
| DistMult | 271.09 | 0.769 | 0.639  | 0.892  | 0.949   |
| ComplEx  | 276.37 | 0.935 | 0.916  | 0.950  | 0.960   |
95
| RESCAL   | 579.54 | 0.846 | 0.791  | 0.898  | 0.931   |
96
| TransR   | 615.56 | 0.606 | 0.378  | 0.826  | 0.890   |
97

Da Zheng's avatar
Da Zheng committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
The speed on Freebase

|  Models | DistMult | ComplEx |
|---------|----------|---------|
|MAX_STEPS| 3200000  | 3200000 |
|TIME     | 2.44h    | 2.94h   |

The accuracy on Freebase (it is tested when 100,000 negative edges are sampled for each positive edge).

|  Models  |  MR    |  MRR  | HITS@1 | HITS@3 | HITS@10 |
|----------|--------|-------|--------|--------|---------|
| DistMul  | 6159.1 | 0.716 | 0.690  | 0.729  | 0.760   |
| ComplEx  | 6888.8 | 0.716 | 0.697  | 0.728  | 0.760   |

The configuration for reproducing the performance results can be found [here](https://github.com/dmlc/dgl/blob/master/apps/kg/config/best_config.sh).

114
115
## Usage

Da Zheng's avatar
Da Zheng committed
116
117
118
119
120
121
122
123
DGL-KE doesn't require installation. The package contains two scripts `train.py` and `eval.py`.

* `train.py` trains knowledge graph embeddings and outputs the trained node embeddings
and relation embeddings.

* `eval.py` reads the pre-trained node embeddings and relation embeddings and evaluate
how accurate to predict the tail node when given (head, rel, ?), and predict the head node
when given (?, rel, tail).
Da Zheng's avatar
Da Zheng committed
124
125
126
127
128

### Input formats:

DGL-KE supports two knowledge graph input formats. A knowledge graph is stored
using five files.
129
130
131

Format 1:

Da Zheng's avatar
Da Zheng committed
132
133
134
135
136
- entities.dict contains pairs of (entity Id, entity name). The number of rows is the number of entities (nodes).
- relations.dict contains pairs of (relation Id, relation name). The number of rows is the number of relations.
- train.txt stores edges in the training set. They are stored as triples of (head, rel, tail).
- valid.txt stores edges in the validation set. They are stored as triples of (head, rel, tail).
- test.txt stores edges in the test set. They are stored as triples of (head, rel, tail).
137
138
139

Format 2:

Da Zheng's avatar
Da Zheng committed
140
141
142
143
144
145
- entity2id.txt contains pairs of (entity name, entity Id). The number of rows is the number of entities (nodes).
- relation2id.txt contains pairs of (relation name, relation Id). The number of rows is the number of relations.
- train.txt stores edges in the training set. They are stored as triples of (head, tail, rel).
- valid.txt stores edges in the validation set. They are stored as a triple of (head, tail, rel).
- test.txt stores edges in the test set. They are stored as a triple of (head, tail, rel).

Da Zheng's avatar
Da Zheng committed
146
147
148
149
150
151
152
153
154
155
156
### Output formats:

To save the trained embeddings, users have to provide the path with `--save_emb` when running
`train.py`. The saved embeddings are stored as numpy ndarrays.

* The node embedding is saved as `XXX_YYY_entity.npy`.

* The relation embedding is saved as `XXX_YYY_relation.npy`.

`XXX` is the dataset name and `YYY` is the model name.

Da Zheng's avatar
Da Zheng committed
157
### Command line parameters
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

Here are some examples of using the training script.

Train KGE models with GPU.

```bash
python3 train.py --model DistMult --dataset FB15k --batch_size 1024 \
    --neg_sample_size 256 --hidden_dim 2000 --gamma 500.0 --lr 0.1 --max_step 100000 \
    --batch_size_eval 16 --gpu 0 --valid --test -adv
```

Train KGE models with mixed CPUs and GPUs.

```bash
python3 train.py --model DistMult --dataset FB15k --batch_size 1024 \
    --neg_sample_size 256 --hidden_dim 2000 --gamma 500.0 --lr 0.1 --max_step 100000 \
    --batch_size_eval 16 --gpu 0 --valid --test -adv --mix_cpu_gpu
```

Train embeddings and verify it later.

```bash
python3 train.py --model DistMult --dataset FB15k --batch_size 1024 \
    --neg_sample_size 256 --hidden_dim 2000 --gamma 500.0 --lr 0.1 --max_step 100000 \
    --batch_size_eval 16 --gpu 0 --valid -adv --save_emb DistMult_FB15k_emb

python3 eval.py --model_name DistMult --dataset FB15k --hidden_dim 2000 \
    --gamma 500.0 --batch_size 16 --gpu 0 --model_path DistMult_FB15k_emb/

```

Train embeddings with multi-processing. This currently doesn't work in MXNet.
```bash
python3 train.py --model DistMult --dataset FB15k --batch_size 1024 \
    --neg_sample_size 256 --hidden_dim 2000 --gamma 500.0 --lr 0.07 --max_step 3000 \
    --batch_size_eval 16 --regularization_coef 0.000001 --valid --test -adv --num_proc 8
```