# Knowledge Graph Representation PyTorch
## Introduction
We provide code for several knowledge graph representation algorithms here, including TransE, TransH, TransR, and TransD.
Every algorithm has two programs. The program name without "Bernoulli" generates negative samples by corrupting head and tail entities with equal probabilities, otherwise, it generates negative samples by corrupting head and tail entities according to Bernoulli Distribution (See [Knowledge Graph Embedding by Translating on Hyperplanes](https://www.aaai.org/ocs/index.php/AAAI/AAAI14/paper/view/8531/8546)).
## Code Structure
`utils.py` contains basic **Triple** class, its comparison, and other basic computations. It also contains the code for computing head-tail proportion for each relation, classifying relations into 1-1, 1-N, N-1 and N-N, and dividing triples according to them.
`data.py` contains various ways to generate negative triples and get a batch of training samples and its corresponding negative samples.
`model.py` contains our four models used in knowledge graph representation.
`projection.py` contains the projection functions used in TransH, TransR, and TransD.
`loss.py` contains the loss functions used in our algorithms, of which the most important is margin loss and orthogonal loss.
`evaluation.py` is evaluating the model in two metrics: meanrank and hits@10. We use multi-processing to speed up computation.
## Usage
Our programs are all written in Python 3.6, using PyTorch as deep learning architecture. So, please install Python and PyTorch first to run our programs.
We also use hyperboard to display training and validation curve in real time. Users who are interested in hyperboard, please refer to https://github.com/WarBean/hyperboard.
Usage:
python xxx.py [parameters]
Possible parameters includes:
`-d [str]`: Which dataset to use? Possible selections are FB13, FB15k, WN11, WN18.
`-l [float]`: Initial learning rate. Suitable for TransE and TransH. Default 0.001.
`-l1 [float]`: Learning rate for the first phase. Suitable for TransR and TransD. Default 0.001.
`-l2 [float]`: Initial learning rate for the second phase, if -es set to > 0. Suitable for TransR and TransD. Default 0.0005.
`-es [int]`: Number of times for decrease of learning rate. If set to 0, no learning rate decrease will occur. Default 0.
`-L [int]`: If set to 1, it will use L1 as dissimilarity, otherwise L2. Default 1.
`-em [int]`: Embedding size of entities and relations. Default 100.
`-nb [int]`: How many batches to train in one epoch. Default 100.
`-n [int]`: Maximum number of epochs to train. Default 1,000.
`-m [float]`: Margin of margin loss. Default 1.0.
`-f [int]`: Whether to filter false negative triples in training, validating and testing. If set to 1, they will be filtered. Default 1.
`-mo [float]`: Momentum of optimizers. Default 0.9.
`-s [int]`: Fix the random seed, except for 0, which means no random seed is fixed. Default 0.
`-op [int]`: Which optimizer to choose. If set to 0, Stochastic Gradient Descent (SGD) will be used. If set to 1, Adam will be used. Default 1.
`-p [int]`: Port number used by hyperboard. Default 5000.
`-np [int]`: Number of processes when evaluating. Default 4.
Evaluation result on the test set will be written into ./result/[dataset].txt, such as ./result/FB15k.txt. It consists of Hits@10 and MeanRank on the whole test set, as well as Hits@10 and MeanRank on 1-1, 1-N, N-1, N-N subsets, when predicting head and tail, respectively.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
knowledge_representation_pytorch.zip (98个子文件)
knowledge_representation_pytorch
loss.py 1KB
transH_pytorch.py 15KB
transD_pytorch.py 17KB
utils.py 5KB
transR_Bernoulli_pytorch.py 17KB
.git
index 7KB
hooks
pre-push.sample 1KB
prepare-commit-msg.sample 1KB
applypatch-msg.sample 478B
pre-commit.sample 2KB
pre-receive.sample 544B
pre-applypatch.sample 424B
commit-msg.sample 896B
pre-rebase.sample 5KB
update.sample 4KB
post-update.sample 189B
config 335B
description 73B
refs
tags
heads
master 41B
remotes
origin
HEAD 32B
logs
refs
heads
master 214B
remotes
origin
HEAD 214B
HEAD 214B
packed-refs 107B
objects
info
pack
pack-b20666fa59cbfebe4ce841d9474b350e389e14ae.idx 6KB
pack-b20666fa59cbfebe4ce841d9474b350e389e14ae.pack 9.19MB
info
exclude 240B
HEAD 23B
__pycache__
loss.cpython-36.pyc 2KB
utils.cpython-36.pyc 5KB
evaluation.py 28KB
transH_Bernoulli_pytorch.py 16KB
transE_Bernoulli_pytorch.py 15KB
transE_pytorch.py 15KB
transD_Bernoulli_pytorch.py 17KB
.DS_Store 8KB
transR_pytorch.py 16KB
model.py 13KB
LICENSE.md 1KB
.gitignore 32B
中文版使用说明.md 3KB
.ipynb_checkpoints
test-checkpoint.ipynb 72B
README.md 3KB
projection.py 1KB
datasets
.DS_Store 14KB
WN18
train2id.txt 1.86MB
head_tail_proportion.pkl 340B
test2id.txt 67KB
one_to_one_test.txt 605B
.DS_Store 6KB
many_to_one_test.txt 26KB
entity2id.txt 589KB
valid2id.txt 68KB
one_to_many_test.txt 26KB
many_to_many_test.txt 15KB
relation2id.txt 363B
triple2id.txt 2MB
WN11
train2id.txt 1.39MB
negvalid2id.txt 34KB
head_tail_proportion.pkl 214B
test2id.txt 133KB
one_to_one_test.txt 286B
.DS_Store 6KB
many_to_one_test.txt 66KB
entity2id.txt 797KB
valid2id.txt 33KB
one_to_many_test.txt 67KB
negtest2id.txt 136KB
many_to_many_test.txt 2B
relation2id.txt 187B
triple2id.txt 1.56MB
FB13
train2id.txt 3.65MB
negvalid2id.txt 75KB
head_tail_proportion.pkl 250B
test2id.txt 271KB
one_to_one_test.txt 2B
.DS_Store 6KB
many_to_one_test.txt 271KB
entity2id.txt 1.65MB
valid2id.txt 68KB
one_to_many_test.txt 2B
negtest2id.txt 300KB
many_to_many_test.txt 2B
relation2id.txt 171B
triple2id.txt 3.98MB
FB15k
train2id.txt 6.74MB
head_tail_proportion.pkl 24KB
test2id.txt 844KB
one_to_one_test.txt 12KB
.DS_Store 8KB
many_to_one_test.txt 123KB
entity2id.txt 220KB
valid2id.txt 715KB
one_to_many_test.txt 74KB
many_to_many_test.txt 635KB
relation2id.txt 88KB
triple2id.txt 8.26MB
data.py 8KB
共 98 条
- 1
资源评论
- Ficus_nuaa2020-01-02TypeError: can only concatenate str (not "NoneType") to str
jimmywangheng
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- mycode.asm
- 基于Python和粒子群算法解决01背包问题并进行可视化.zip
- Qt/qt creator实现TCP通信,多线程实现服务器的并发(server/client)
- Qt/qt creator实现TCP通信,多线程实现服务器的并发(server/client)
- IMG_20240510_083752.jpg
- 中兴R5300G4 BIOS&BMC 最新版本 24年5月
- 基于C++实现的哈夫曼二叉树.zip
- 基于SSM框架和推荐算法的机票预订系统源码(高分项目).zip
- retouch_2024050716253909.jpg
- 基于mediapipe + opencv的手势识别python源码+详细代码注释(高分项目).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功