line_向量化

一、line模型是怎么训练的

论文链接:http://de.arxiv.org/pdf/1503.03578

用北京大学的一个:https://github.com/tangjianpku/LINE

/data/mllibs/LINE/linux/myline -train /data/static/data/graph_index/EveryDay/query_pairs_processed_${dt1}.txt -output /data/static/data/graph_index/EveryDay/vec_1st_wo_norm_${dt1}.txt -binary 0 -size 128 -order 1 -negative 5 -samples 10000 -threads 20

/data/mllibs/LINE/linux/myline -train /data/static/data/graph_index/EveryDay/query_pairs_processed_${dt1}.txt -output /data/static/data/graph_index/EveryDay/vec_2nd_wo_norm_${dt1}.txt -binary 0 -size 128 -order 2 -negative 5 -samples 10000 -threads 20

官方说明:

./line -train network_file -output embedding_file -binary 1 -size 200 -order 2 -negative 5 -samples 100 -rho 0.025 -threads 20
  • -train, the input file of a network;
  • -output, the output file of the embedding;
  • -binary, whether saving the output file in binary mode; the default is 0 (off);
  • -size, the dimension of the embedding; the default is 100;
  • -order, the order of the proximity used; 1 for first order, 2 for second order; the default is 2;
  • -negative, the number of negative samples used in negative sampling; the deault is 5;
  • -samples, the total number of training samples (*Million);
  • -rho, the starting value of the learning rate; the default is 0.025;
  • -threads, the total number of threads used; the default is 1.

特别说明输入的形式

Each line of the input file represents a DIRECTED edge in the network, which is specified as the format "source_node target_node weight" (can be either separated by blank or tab). For each undirected edge, users must use TWO DIRECTED edges to represent it.

一行代表一个有向边,无向边的话,必须使用两条有向边表示

good the 3
the good 3
good bad 1
bad good 1
bad of 4
of bad 4

二、怎么构建annoy索引的(同word2vec)

/data/static/data/graph_index/EveryDay/vec_1st_wo_norm_${dt1}.txt

里面内容形式 :1 -0.349799 0.062615 -0.022505 0.174733 0.273782 0.009730 

 

query_pairs_processed_${dt1}.txt 

里面内容形式:42760386,4276w左右,里面不是双向的14227638 2406512 8只出现一次

1 2 69
3 4 7
5 6 4
7 8 4
9 10 11
11 12 5
13 14 23
15 16 11
17 18 6
19 20 5

1.怎么得到这个处理后的pair的,里面是qa-qb-cnt,现indexa-indexb-cnt

(1)得到query_pairs_20210522.txt

里面的数据形如:

李白乘舟将欲行,忽闻岸上踏歌声 李白的诗 69
黑暗圣经 艳母在线观看 7

sql="select query_a,query_b,count from comsearch.da_nlp_browser_search_session_query_pair_d where day='${etl_date}' and period='90day' and count>2"

save_path="${out_path}/query_pairs_$(date -d -1day +%Y%m%d ).txt"

 

三、怎么找近邻的

ai.get_nns_by_vector(vec, 30, include_distances=True)#输入一个向量,输出一些索引

vec是一个向量

问题:

query2vec_fuzzy_'+task+'.bin',里面存的idx还是存的是query,里面存的是query

annoy里面加的item是add_item(count, vec),annoysave的是ann形式的

只针对头部query建立索引吗?

 

四、line模型讲解

line是一种graph embedding算法。在浏览器搜索发现场景,图中的点为query,边为query共现频次。

模型使用作者唐建在github上的开源工具进行训练。输入训练数据,指定embedding输出的路径以及一些模型参数就可以了。

模型训练目标为使得相似的query的embedding在高维语义空间中是相近的。

对于相似作者提到一阶相似与二阶相似,一阶相似意思就是直接相连的query可以看做相似的,二阶相似指的是图中的两个点,虽然没有直接相连,但是他们有很多相同的邻点。

 损失函数度量的是通过向量表达得到的相似与样本得到的一致程度。

实际一阶相似的条件概率就是边中两个点的向量表达式相乘在过一个sigmoid,经验条件概率为连接边权重。

二阶相似性,其实对比的就是两个节点的条件概率分布的相似性。只要我们能够保证所有节点的条件概率和经验条件概率一致,那么这个embedding就可以很好地保存这两个节点的二阶相似信息

 

训练技巧:用到了负采样

posted @ 2022-04-29 12:05  HappierJoanne  阅读(259)  评论(0)    收藏  举报