动态知识图谱库DynGEM的学习之库的安装与调试

背景

迫于毕设开题,要研究下动态知识图谱的内容,正赶上读到一篇文章dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning,里面开源了根据自己文章写的库DynGEM。本着拿来主义的原则,我于上周开始了对其的探索过程

根据此库README.md中所述,要先安装DynTriad及其依赖环境,再安装DynGEM自己及环境。本认为库的安装应该不难,孰料想,由于很多依赖都是要源码安装,而且即便安装完成能被导包了,但由于依赖版本、环境变量和库代码本身的问题,还得修改些源码才能顺利运行,所以颇费了些周折,最后竟耗时5天才完事儿。 故在此记录在CentOS7下的安装调试过程,希望大家少走些弯路。

DynTriad

以下步骤建议按顺序进行

Boost.Python

要下载安装1.58版本1.73版本,然后的命令都是一样的,只需要把73改成58即可。

以1.73为例,运行命令解压并切入

[root@scentos DynamicGraph]# tar -xvzf boost_1_73_0.tar.gz
[root@scentos DynamicGraph]# cd boost_1_73_0/

然后运行命令进行编译安装

[root@scentos boost_1_73_0]# ./bootstrap.sh
[root@scentos boost_1_73_0]# ./b2 install

静候其完成,完成后,会在/usr/local/lib下产生很多的库

[root@scentos boost_1_73_0]# ll/usr/local/lib
total 42032
.....

如果需要编译安装python3版本,则按照以下步骤进行:

首先修改bootstrap.sh文件,修改python.configured下面的内容。注释掉原来的using python : $PYTHON_VERSION : "$PYTHON_ROOT" ;,照猫画虎写上Python3的内容using python : 3.5 : "/usr/local/bin/python3.5" : /usr/local/include/python3.5m : /usr/local/lib ;

import python ;
if ! [ python.configured ]
{
    # using python : $PYTHON_VERSION : "$PYTHON_ROOT" ;
    # using python : 3.6 : "/usr/bin/python3.6" : /usr/include/python3.6m : /usr/local/lib ;
    using python : 3.5 : "/usr/local/bin/python3.5" : /usr/local/include/python3.5m : /usr/local/lib ;
}
EOF
fi

然后调用bootstrap脚本,加上参数

[root@scentos boost_1_73_0]# ./bootstrap.sh --with-python=python3 --with-python-root=/usr/local/bin/python3.5 --with-python-version=3.5

安装命令还是./b2 install。

经过试错,发现1.73版本用Python3.6和Python2.7编译,1.58版本的用Python3.5编译

CMake

下载包https://cmake.org/download/,然后运行命令

[root@scentos DynamicGraph]# tar -zxvf cmake-3.18.2.tar.gz
[root@scentos DynamicGraph]# cd cmake-3.18.2/
[root@scentos cmake-3.18.2]# ./bootstrap
[root@scentos cmake-3.18.2]# make
[root@scentos cmake-3.18.2]# make install

eigen

下载包http://eigen.tuxfamily.org/index.php?title=Main_Page#Download,然后解压

[root@scentos DynamicGraph]# tar -zxvf eigen-3.3.7.tar.gz

不要在解压出来的eigen-3.3.7目录里编译安装,得新建一个目录(此处名曰eigen3.3.7),然后进去这个目录,在这里进行eigen的编译和安装

[root@scentos DynamicGraph]# mkdir eigen3.3.7
[root@scentos DynamicGraph]# cd eigen3.3.7
[root@scentos eigen3.3.7]# cmake ../eigen-3.3.7
[root@scentos eigen3.3.7]# make install

mpfr和expat

下载路径https://www.mpfr.org/mpfr-current/#downloadhttps://sourceforge.net/projects/expat/,下载后解压,进入两者目录,分别执行

./configure
make && make install

cgal

下载路径https://github.com/CGAL/cgal/tree/releases/CGAL-4.7,下载解压,进入目录,执行命令

[root@scentos cgal-releases-CGAL-4.7]# cmake .
[root@scentos cgal-releases-CGAL-4.7]# make && make install

GraphTool

下载项目压缩包https://git.skewed.de/count0/graph-tool/-/tree/master,先确保自己的gcc版本>=9.1.0

[root@scentos DynamicGraph]# gcc --version
gcc (GCC) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果版本比较低,或者压根儿没有gcc,那么可以参考我的文章记CentOS7下升级gcc到9.3.0来进行升级或者安装

然后安装python2的pip,下载路径https://pypi.org/project/pip/#files,解压后进入对应目录,执行命令

[root@scentos pip-20.2.3]# python setup.py install
[root@scentos pip-20.2.3]# pip install pycairo scipy

再用yum安装一些依赖

yum -y install numpy.x86_64 scipy.x86_64

最后运行命令编译安装graph-tool,这里会耗时很久,我虚拟机四核8G内存得两个多小时才完事儿

[root@scentos DynamicGraph]# tar -zxvf graph-tool-master.tar.gz
[root@scentos DynamicGraph]# cd graph-tool-master/
[root@scentos graph-tool-master]# ./autogen.sh
[root@scentos graph-tool-master]# ./configure --disable-sparsehash
[root@scentos graph-tool-master]# make && make install

DynamicTriad

安装完上面的库之后,就可以编译安装DynamicTriad了,项目地址https://github.com/luckiezhou/DynamicTriad

先用pip安装dill

[root@scentos DynamicTriad-master]# pip install dill

然后执行安装,下面的路径等变量要改成自己的

[root@scentos DynamicTriad-master]# ./build.sh
entering /home/szc/DynamicGraph/DynamicTriad-master
You may need to specify some environments before building
PYTHON_LIBRARY? (default: /usr/lib64/libpython2.7.so.1.0, use a space ' ' to leave it empty) 
PYTHON_INCLUDE_DIR? (default: /usr/include/python2.7, use a space ' ' to leave it empty) 
EIGEN3_INCLUDE_DIR? (default: /usr/include, use a space ' ' to leave it empty) /home/szc/DynamicGraph/eigen-3.3.7
BOOST_ROOT? (default: , use a space ' ' to leave it empty) /usr/local/lib
name for boost_python library? (useful when boost_python cannot be detected by cmake) (default: boost_python, use a space ' ' to leave it empty) 

里面的一些变量路径(/home/szc/DynamicGraph/eigen-3.3.7、/usr/local/lib)根据自己的情况改

运行结果如下

而后运行test.py看看是否构建完成

到这儿,DynTriad及其依赖包就安装落停了

DynGEM

此库除了DynTriad之外,还得安装MATLAB和LaTex

MATLAB

下载地址https://www.mathworks.com/products/compiler/matlab-runtime.html,选择2019(9.6)的下载。下载解压后运行./install,然后一路默认即可。完成后修改/etc/profile文件,在后面加入一行

export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:/usr/local/MATLAB/MATLAV_Runtime/v96/sys/os/glnxa64

保存退出后source之,使之生效

LaTex

下载地址https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/,选择里面的textlive.iso。下载完放到虚拟机里,清除原来的,挂载新的,安装即可

[root@scentos szc]# locate texlive | xargs rm -rf
[root@scentos szc]# mount  -o loop texlive.iso /mnt/
[root@scentos szc]# cd /mnt/
[root@scentos mnt]# ./install-tl

选择安装方式直接选i就行,安装完后,在/etc/profile文件末尾加上几行环境变量

export PATH=/usr/local/texlive/2020/bin/x86_64-linux:$PATH

export MANPATH=/usr/local/texlive/2020/texmf-dist/doc/man
export INFOPATH=/usr/local/texlive/2020/texmf-dist/doc/info

然后source使能之,查看latex版本

[root@scentos mnt]# source /etc/profile
[root@scentos mnt]# tex --version
TeX 3.14159265 (TeX Live 2020)
kpathsea version 6.3.2
Copyright 2020 D.E. Knuth.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the TeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the TeX source.
Primary author of TeX: D.E. Knuth.
[root@scentos mnt]#

DynGEM

项目地址https://github.com/palash1992/DynamicGEM,下载解压进入目录后,直接运行

python setup.py install

即可,如果报错

Traceback (most recent call last):
  File "./setup.py", line 185, in <module>
    setup_package()
  File "./setup.py", line 138, in setup_package
    write_version_py()
  File "./setup.py", line 119, in write_version_py
    version = imp.load_source('DynamicGEM.version', 'dynamicgem/version.py')
  File "/usr/lib64/python3.6/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "dynamicgem/version.py", line 3
    short_version = '1.0.0'

则修改dynamicgem/version.py文件,清除原来的内容,复制以下内容(这其实就是代码生成的version.py给每一行代码多加了缩进造成的)。

short_version = '1.0.0'
version = '1.0.0'
full_version = '1.0.0'
git_revision = 'Unknown'
release = True
if not release:
    version = full_version
    short_version += ".dev"

调试DynGEM

使用pycharm,用python3.6粘贴DynGEM官网README.md中的测试代码运行,发现以下问题:

1)、由于事先编译好的DynamicTraid中的mygraph是so包,所以DynamicGEM中对其的引入方式不对,要改成ctype的引入方式:

# /usr/local/lib/python3.6/site-packages/dynamicgem/dynamictriad/core/gconv.py

import ctypes
import os
# mygraph=ctypes.cdll.LoadLibrary(os.path.realpath('')+'/dynamicgem/dynamictriad/core/mygraph.so')
dll_path_ = "/home/szc/DynamicGraph/DynamicGEM-master/dynamicgem/dynamictriad/core/mygraph-build/mygraph.so"
mygraph = ctypes.cdll.LoadLibrary(dll_path_)

# import dynamicgem.dynamictriad.core.mygraph as mygraph

以及

# /usr/local/lib/python3.6/site-packages/dynamicgem/dynamictriad/core/mygraph_utils.py

import os
import ctypes
# mygraph=ctypes.cdll.LoadLibrary(os.path.realpath('')+'/dynamicgem/dynamictriad/core/mygraph.so')
dll_path_ = "/home/szc/DynamicGraph/DynamicGEM-master/dynamicgem/dynamictriad/core/mygraph-build/mygraph.so"
mygraph = ctypes.cdll.LoadLibrary(dll_path_)
# import dynamicgem.dynamictriad.core.mygraph as mygraph

2)、替换MATLAB的libstdc++文件。这是因为MATLAB9.6的6.0.22版本的libstdc++.so文件里没有GLIBCXX_3.4.26,而6.0.28版本里有,所以要换成后者,否则就等着报错吧

[root@scentos boost_1_73_0]# mv /usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64/libstdc++.so.6.0.22 /usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64/libstdc++.so.6.0.22_old
[root@scentos boost_1_73_0]# cp /usr/lib64/libstdc++.so.6.0.28 /usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64/
[root@scentos boost_1_73_0]# rm -f /usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64/libstdc++.so.6
[root@scentos boost_1_73_0]# ln -s libstdc++.so.6.0.28 /usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64/libstdc++.so.6

3)、确保keras和tensorflow的版本匹配,其中tensorflow应该是1.11版本,对照https://docs.floydhub.com/guides/environments/修改keras版本

[root@scentos Textset]#  /usr/local/bin/pip3.6 install tensorflow==1.11 
[root@scentos Textset]#  /usr/local/bin/pip3.6 install keras==2.2.4

然后升级numpy,修改h5py版本

[root@scentos Textset]#  /usr/local/bin/pip3.6 install -U numpy
[root@scentos Textset]#  /usr/local/bin/pip3.6 install h5py==2.8.0

4)、运行过程中如果还遇到超参不对等问题,就要跳到对应的代码部分进行修改。

比如我在运行到环境变量中MATLAB的地址,所以我修改了对应文件(/usr/local/lib/python3.6/site-packages/TIMERS_ALL/__init__.py)中的对应部分(if not os.environ[self.path_var].__contains__("MATLAB")部分):

    def get_paths_from_os(self):
        ....

        path_elements = []
        if self.path_var in os.environ:
            if not os.environ[self.path_var].__contains__("MATLAB"):
                os.environ[self.path_var] = os.environ[self.path_var] + os.pathsep\
                                            + "/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:" \
                                              "/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:" \
                                              "/usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64"
            path_elements = os.environ[self.path_var].split(os.pathsep)
        if not path_elements:
            .....

也可以在Edit Configuration->Environment Vairables中进行指定

5)、走到这里,其实大部分DynGEM的代码就能运行了,只是DynTriad的还是有问题

  ....
  File "/usr/local/lib/python3.6/site-packages/dynamicgem/dynamictriad/core/mygraph_utils.py", line 90, in load_adjlist
    g = mygraph.Graph(node_type, weight_type)
  File "/usr/lib64/python3.6/ctypes/__init__.py", line 356, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib64/python3.6/ctypes/__init__.py", line 361, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /...../DynamicGEM-master/dynamicgem/dynamictriad/core/mygraph-build/mygraph.so: undefined symbol: Graph

对应代码为

# dynamicgem\dynamictriad\core\mygraph_utils.py

def load_adjlist(fn, node_type='string', weight_type='float'):
    """
    loads only undirected graph, if multiple instances of the same edge is detected,
    their weights are summed up
    :param fn:
    :param node_type:
    :param weight_type:
    :return:
    """
    py_node_type = type2python(node_type)
    py_weight_type = type2python(weight_type)

    edgeset = set()  # check if the graph is undirected
    g = mygraph.Graph(node_type, weight_type)
    for line in open(fn, 'r'):
        fields = line.split()

由报错信息可知,错误是mygraph.so里没有Graph这一属性或者类,而我把so包路径从DynGEM自带的换成DynTriad中编译好的,还是有这问题,但是site-packages里的dyngem/dyntraid/core目录下又实在没有mygraph.py文件,所以这应该是DynGEM代码本身的问题,我已经给作者提了议题,尚未见答复

运行结果

以文章中招牌模型AERNN为例,测试运行DynGEM,40多分钟后运行结束,截图如下:

结语

至此,DynGEM就算是安装完成并且可以正常运行了(DynTriad测试部分除外),我们可以通过断点调试的方式对其进行理解和探索。

最后,附上所有要下载东西的网盘链接:

链接:https://pan.baidu.com/s/1TaDTq555xIF6o_xWGlHq1Q 
提取码:kkwf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值