LargeVis安装与使用

297 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第19天,点击查看活动详情

导语

本文为我之前在CSDN平台上的一篇博客记录。原链接为:blog.csdn.net/u011426236/…

简介

LargeVis是一种用于大规模图可视化的布局算法,其在高维数据可视化、网络可视化上具有良好的表现。其基本思想是在低维空间中尽可能的保留高维数据点之间的原始结构性。

由于学习需要进行了一些相关探索,记录如下。

项目地址与介绍

  • 项目地址

github.com/lferry007/L…

  • 项目介绍

LargeVis算法首先构建KNN图,然后构造目标函数进行优化从而得到数据的低维表示。LargeVis通过使用一种更加高效的方法构造KNN图,同时将word2vec中的CBOW和skip-gram思想和Line中边采样的思想用于优化目标函数,大幅度的提升了算法的效率。

我的操作系统是64位Win10,这里仅列出Readme中关于Windows环境下的描述

Windows To compile the source files, use Microsoft Visual Studio, where you need to set the BOOST path.

To install the Python wrapper, modify setup.py to make sure that the BOOST path is correctly set and then run python setup.py install.

其描述比较简略,但其中重要的几点包括:

  • 要在VS环境下编译,所以尽量提前下载与配置好VS,我这里是下载的VS2017社区版,免费的,下载链接:Visual Studio 2017 System Requirements | Microsoft Docs。安装时只选择使用C++的桌面开发即可。(这个少装了其他模块也不用担心,安装完成后可以随时打开Visual Studio Installer修改与添加的。)

​编辑

  • 其次是要正确设置Boost路径,对于有些人来说,电脑里并没有安装过Boost,我们需要重新进行下载与安装,简要步骤在下面介绍

Boost环境配置

  • Boost简介

Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一。由于项目中用到了大量其中的相关库函数,因而必须要先配置好Boost。

  • Boost下载与安装

直接去官网下载即可,当前下载的版本是1.69.0,链接如下

dl.bintray.com/boostorg/re…

选择下载

boost_1_69_0.zip

下载完成后需要先进行解压。

安装的具体步骤可以参考blog.csdn.net/u012424737/… ,这篇博客里提供了详细的安装步骤,一步一步来就可以。

需要注意的是,一定要记住添加环境变量,这样才可保证安装后生效。

源代码的几处修改

Boost与VS环境都配好后,按道理应该是可以运行了。

首先,我们先按照Readme中的要求修改Boost路径,该步骤所涉及到的具体文件即项目解压出来后的Windows目录下的setup.py,我们只要将之前安装Boost时的两个路径替换进来即可。

在完成好以上配置之后,我们接着按要求在Windows这个目录下打开命令行,执行如下命令

python setup.py install

不过接着就报了很多错误,究其原因是由于这个项目中是基于Python2.7的,我的电脑上Python安装的是3.7,导致有些API早已不是原先的名字了,我们需要对某些地方进行一些修改,具体如下:

在LargeVismodule.cpp中

real x = atof(PyString_AsString(PyObject_Str(PyList_GetItem(vec, j))));

改为

real x = atof(PyBytes_AsString(PyObject_Str(PyList_GetItem(vec, j)))); 

PyMODINIT_FUNC initLargeVis()
{
	printf("LargeVis successfully imported!\n");
	Py_InitModule("LargeVis", PyExtMethods);
}

改为

static struct PyModuleDef LargeVismodule =
{
PyModuleDef_HEAD_INIT,
"LargeVis",
NULL,
-1,
PyExtMethods
};

PyMODINIT_FUNC
PyInit_LargeVis(void)
{
	return PyModule_Create(&LargeVismodule);
}

运行示例

直接用Example中的数据集测试即可,Readme中提供了一些用法说明:

对于 Python使用者,可以运行如下的程序

python LargeVis_run.py -input -output
  • -input: Input file of feature vectors or networks (see the Example folders for input format).
  • -output: Output file of low-dimensional representations.

Besides the two parameters, other optional parameters include:

  • -fea: specify whether the input file is high-dimensional feature vectors (1) or networks (0). Default is 1.
  • -threads: Number of threads. Default is 8.
  • -outdim: The lower dimensionality LargesVis learns for visualization (usually 2 or 3). Default is 2.
  • -samples: Number of edge samples for graph layout (in millions). Default is set to data size / 100 (million).
  • -prop: Number of times for neighbor propagations in the state of K-NNG construction, usually less than 3. Default is 3.
  • -alpha: Initial learning rate. Default is 1.0.
  • -trees: Number of random-projection trees used for constructing K-NNG. 50 is sufficient for most cases unless you are dealing with very large datasets (e.g. data size over 5 million), and less trees are suitable for smaller datasets. Default is set according to the data size.
  • -neg: Number of negative samples used for negative sampling. Default is 5.
  • -neigh: Number of neighbors (K) in K-NNG, which is usually set as three times of perplexity. Default is 150.
  • -gamma: The weights assigned to negative edges. Default is 7.
  • -perp: The perplexity used for deciding edge weights in K-NNG. Default is 50.

对于Python,使用格式:

python LargeVis_run.py -input -output

-input 输入文件 
-output 输出文件

其他可选项: 
-fea 指定输入文件是高维特征向量(1)或者网络(0),默认为1 
-threads 线程数,默认为8 
-outdim 表示输出的低维的可视化维度数,默认为2 
-samples 图形布局的边缘样本数量,默认为 data size / 100 (million) 
-prop 邻居传播次数,与K-NNG构造有关,一般少于3,默认为3 
-alpha 定义学习率,梯度下降用,默认为1.0 
-trees 构造K-NNG随机映射树的个数,一般50足够处理很大的数据集,除非数据集超过5 百万,默认根据数据量来定。 
-neg 负采样个数,默认为5 
-neigh K-NNG的邻居数 , which is usually set as three times of perplexity,默认为150 
-gama 分配给负边缘的权重,默认为7 
-prep 在KNN中决定边缘权重的值(perplexity)默认为50

对于示例中的MINST数据集,

python LargeVis_run.py -input mnist_vec784D.txt -output mnist_vec2D.txt -threads 16
python plot.py -input mnist_vec2D.txt -label mnist_label.txt -output mnist_vec2D_plot

即可得到输出图像

可以看到,通过LargeVis的可视化展示,各个类别在二维平面上被区分的很清晰明了,表名LargeVis有着良好的可视化效果,提供了一种将高维数据投射到二维平面的展示方法,极大的方便了高维数据的可视化。

参考