也就是构造一棵Huffman Tree,输入是按照词汇频次由高到低排序的

采用层次SoftMax的做法,是为了使得训练和预测时候的softmax输出加速,原有multinomal softmax,是和

训练词汇量|V|成正比的,而现在由于二叉树的特性,变成了log(|V|),也就是平均每个预测只做log(|V|)次

的binary的softmax。当然还有另外一种不采用HSTree的方法也就是nce(Noise Contrastive Estimation),后面再分析。

 
 

由于</s>会被统计,这里为了模拟这个,用</s>代替了 "我"

[root@cq01-forum-rstree01.cq01.baidu.com faster-rnnlm]# cat shijiebei.txt

</s> 喜欢 观看 巴西 足球 世界杯

</s> 喜欢 观看 巴西 足球

</s> 喜欢 观看 巴西 足球

</s> 喜欢 观看 巴西

</s> 喜欢 观看 巴西

</s> 喜欢 观看

</s> 喜欢

喜欢

[root@cq01-forum-rstree01.cq01.baidu.com faster-rnnlm]# pwd

/home/users/chenghuige/other/faster-rnnlm.debug/faster-rnnlm

 
 

(gdb) p vocab

$63 = (const Vocabulary &) @0x7fffffffdb60: {static kWordOOV = 4294967295, words_ = std::vector of length 6, capacity 8 = {{freq = 15, word = 0x6ae1c0 "</s>"}, {freq = 8,

word = 0x6aea30 "\317\262\273\266"}, {freq = 6, word = 0x6ae4a0 "\271\333\277\264"}, {freq = 5, word = 0x6aead0 "\260\315\316\367"}, {freq = 3, word = 0x6aeaf0 "\327\343\307\362"}, {freq = 1,

word = 0x6aeba0 "\312\300\275\347\261\255"}}, hash_impl_ = 0x6ae780}

(gdb) n

203         for (int branch = 0; branch < ARITY; ++branch) {

(gdb) n

204         if (next_leaf_node >= 0 && weight[next_leaf_node] < weight[next_inner_node]) {

(gdb) c

Continuing.

 
 

Breakpoint 6, HSTree::CreateHuffmanTree (vocab=..., layer_size=5) at hierarchical_softmax.cc:222

222         return new HSTree(vocab.size(), layer_size, children);

(gdb) p children

$64 = std::vector of length 10, capacity 10 = {5, 4, 6, 3, 2, 1, 7, 8, 0, 9}

(gdb) p weight

$65 = std::vector of length 13, capacity 13 = {15, 8, 6, 5, 3, 1, 4, 9, 14, 23, 38, 0, 0}

 
 

Breakpoint 7, HSTree::Tree::Tree (this=0x6aedf0, leaf_count=6, children=std::vector of length 10, capacity 10 = {...}) at hierarchical_softmax.cc:165

165        }

(gdb) p Tree

$66 = {void (HSTree::Tree * const, int, const std::vector<int, std::allocator<int> > &)} 0x418718 <HSTree::Tree::Tree(int, std::vector<int, std::allocator<int> > const&)>

(gdb) p *this

$67 = {leaf_count_ = 6, root_node_ = 10, tree_height_ = 4, children_ = std::vector of length 10, capacity 10 = {5, 4, 6, 3, 2, 1, 7, 8, 0, 9}, path_lengths_ = std::vector of length 6, capacity 6 = {

2, 4, 4, 4, 5, 5}, points_ = std::vector of length 246, capacity 246 = {10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 10, 9, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 9, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 9, 7, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 9, 7, 6,

4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...}, branches_ = std::vector of length 240, capacity 240 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...}}

 
 

仿照写了一个简化版的
这个看起来更清楚一些

//主要输出接口是根据wordId来定位 路径上面的node和branch

class HuffmanTree

{

public:

//默认假定输入的freqs是从大到小排好序的 needSort = false

//@TODO 对应 NumBranch不是2 比如3叉huffman 要检查叶子数目是否ok

HuffmanTree(const vector<int64>& freqs, int numBranchces = 2)

:_numBranches(numBranchces),

_numLeaves(freqs.size()),

_numInterNodes(freqs.size() - 1),

_numNodes(_numLeaves + _numInterNodes),

_childs(_numBranches, _numInterNodes),

_root(_numNodes - 1),

_weight(freqs.begin(), freqs.end()),

_nodePaths(_numLeaves),

_branchPaths(_numLeaves)

{

Init();

Build();

}

 
 

int NumBranches() const

{

return _numBranches;

}

 
 

bool IsLeaf(int node) const

{

return node < _numLeaves;

}

 
 

int Root() const

{

return _root;

}

 
 

int NumLeaves() const

{

return _numLeaves;

}

 
 

size_t size() const

{

return _numLeaves;

}

 
 

const vector<int>& NodePaths(int node) const

{

return _nodePaths[node];

}

 
 

const vector<int>& BranchPaths(int node) const

{

return _branchPaths[node];

}

 
 

int Height() const

{

return _height;

}

 
 

protected:

private:

void Init()

{

_weight.resize(_numNodes, std::numeric_limits<int64>::max());

}

 
 

int InterNodeIndex(int index) const

{

return _numLeaves + index;

}

 
 

void Build()

{

int minLeafIndex = _numLeaves - 1;

int minInterIndex = _numLeaves;

 
 

vector<int> parents(_numNodes);

vector<int> branches(_numNodes);

//每次选取_numBranches个最小的节点,并将它们的和按照顺序

for (int interIndex = 0; interIndex < _numInterNodes; interIndex++)

{

int64 weight = 0;

int index = InterNodeIndex(interIndex);

for (int branch = 0; branch < _numBranches; branch++)

{

if (minLeafIndex >= 0 && _weight[minLeafIndex] <= _weight[minInterIndex])

{

weight += _weight[minLeafIndex];

_childs[branch][interIndex] = minLeafIndex;

parents[minLeafIndex] = index;

branches[minLeafIndex] = branch;

minLeafIndex--;

}

else

{

weight += _weight[minInterIndex];

_childs[branch][interIndex] = minInterIndex;

parents[minInterIndex] = index;

branches[minInterIndex] = branch;

minInterIndex++;

}

}

_weight[index] = weight;

}

 
 

for (int leafNode = 0; leafNode < _numLeaves; leafNode++)

{

for (int node = leafNode; node != _root; node = parents[node])

{

_nodePaths[leafNode].push_back(parents[node]);

_branchPaths[leafNode].push_back(branches[node]);

}

if (_branchPaths[leafNode].size() > _height)

{

_height = _branchPaths[leafNode].size();

}

std::reverse(_nodePaths[leafNode].begin(), _nodePaths[leafNode].end());

std::reverse(_branchPaths[leafNode].begin(), _branchPaths[leafNode].end());

}

}

 
 

private:

int _numBranches = 2;

 
 

//@TODO 目前都是只考虑int 后面考虑改为 WordIndex ? unsigned

int _numLeaves;

int _numInterNodes;

int _numNodes;

int _root;

int _height = 0;

 
 

//存储所有node的weight最开始存储所有叶子节点从大到小排列,然后存储内部节点从小到大排列,root节点就是最后一个节点

//保持和faster-rnnlm中策略一致

vector<int64> _weight;

ufo::Matrix<int> _childs;

 
 

//记录每一个叶子节点对应从root到它自身路径的所有节点,不包括它自己

vector<vector<int> > _nodePaths;

//记录每一个叶子节点对应从root到它自身的所有路径的branch标记 比如 010100

vector<vector<int> > _branchPaths;

};

 
 

测试程序

TEST(huffmantree, func)

{

vector<int64> vec = { 15, 8, 6, 5, 3, 1 };

HuffmanTree tree(vec);

Pvec(tree._weight);

for (int i = 0; i < tree.NumBranches(); i++)

{

Pval(i);

Pvec(tree._childs[i]);

}

 
 

for (size_t i = 0; i < tree.size(); i++)

{

Pval(i);

Pvec(tree.NodePaths(i));

Pvec(tree.BranchPaths(i));

}

Pval(tree.Height());

}

 
 

测试结果

[root@cq01-forum-rstree01.cq01.baidu.com ds]# pwd

/home/users/chenghuige/rsc/app/search/sep/anti-spam/gezi/test/ds

[root@cq01-forum-rstree01.cq01.baidu.com ds]# ./test_huffmantree

[==========] Running 1 test from 1 test case.

[----------] Global test environment set-up.

[----------] 1 test from huffmantree

[ RUN ] huffmantree.func

I1109 16:06:23.855368 26257 test_huffmantree.cc:32] tree._weight --- 11

I1109 16:06:23.855466 26257 test_huffmantree.cc:32] 0 15

I1109 16:06:23.855478 26257 test_huffmantree.cc:32] 1 8

I1109 16:06:23.855484 26257 test_huffmantree.cc:32] 2 6

I1109 16:06:23.855489 26257 test_huffmantree.cc:32] 3 5

I1109 16:06:23.855494 26257 test_huffmantree.cc:32] 4 3

I1109 16:06:23.855499 26257 test_huffmantree.cc:32] 5 1

I1109 16:06:23.855504 26257 test_huffmantree.cc:32] 6 4

I1109 16:06:23.855509 26257 test_huffmantree.cc:32] 7 9

I1109 16:06:23.855515 26257 test_huffmantree.cc:32] 8 14

I1109 16:06:23.855520 26257 test_huffmantree.cc:32] 9 23

I1109 16:06:23.855525 26257 test_huffmantree.cc:32] 10 38

I1109 16:06:23.855530 26257 test_huffmantree.cc:35] i --- [0]

I1109 16:06:23.855538 26257 test_huffmantree.cc:36] tree._childs[i] --- 5

I1109 16:06:23.855543 26257 test_huffmantree.cc:36] 0 5

I1109 16:06:23.855550 26257 test_huffmantree.cc:36] 1 6

I1109 16:06:23.855554 26257 test_huffmantree.cc:36] 2 2

I1109 16:06:23.855559 26257 test_huffmantree.cc:36] 3 7

I1109 16:06:23.855563 26257 test_huffmantree.cc:36] 4 0

I1109 16:06:23.855569 26257 test_huffmantree.cc:35] i --- [1]

I1109 16:06:23.855574 26257 test_huffmantree.cc:36] tree._childs[i] --- 5

I1109 16:06:23.855579 26257 test_huffmantree.cc:36] 0 4

I1109 16:06:23.855584 26257 test_huffmantree.cc:36] 1 3

I1109 16:06:23.855589 26257 test_huffmantree.cc:36] 2 1

I1109 16:06:23.855594 26257 test_huffmantree.cc:36] 3 8

I1109 16:06:23.855599 26257 test_huffmantree.cc:36] 4 9

I1109 16:06:23.855604 26257 test_huffmantree.cc:41] i --- [0]

I1109 16:06:23.855610 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 1

I1109 16:06:23.855615 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855620 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 1

I1109 16:06:23.855626 26257 test_huffmantree.cc:43] 0 0

I1109 16:06:23.855631 26257 test_huffmantree.cc:41] i --- [1]

I1109 16:06:23.855636 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 3

I1109 16:06:23.855641 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855646 26257 test_huffmantree.cc:42] 1 9

I1109 16:06:23.855651 26257 test_huffmantree.cc:42] 2 8

I1109 16:06:23.855656 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 3

I1109 16:06:23.855662 26257 test_huffmantree.cc:43] 0 1

I1109 16:06:23.855667 26257 test_huffmantree.cc:43] 1 1

I1109 16:06:23.855672 26257 test_huffmantree.cc:43] 2 1

I1109 16:06:23.855677 26257 test_huffmantree.cc:41] i --- [2]

I1109 16:06:23.855682 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 3

I1109 16:06:23.855687 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855692 26257 test_huffmantree.cc:42] 1 9

I1109 16:06:23.855697 26257 test_huffmantree.cc:42] 2 8

I1109 16:06:23.855702 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 3

I1109 16:06:23.855707 26257 test_huffmantree.cc:43] 0 1

I1109 16:06:23.855711 26257 test_huffmantree.cc:43] 1 1

I1109 16:06:23.855716 26257 test_huffmantree.cc:43] 2 0

I1109 16:06:23.855721 26257 test_huffmantree.cc:41] i --- [3]

I1109 16:06:23.855726 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 3

I1109 16:06:23.855731 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855736 26257 test_huffmantree.cc:42] 1 9

I1109 16:06:23.855741 26257 test_huffmantree.cc:42] 2 7

I1109 16:06:23.855746 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 3

I1109 16:06:23.855751 26257 test_huffmantree.cc:43] 0 1

I1109 16:06:23.855756 26257 test_huffmantree.cc:43] 1 0

I1109 16:06:23.855762 26257 test_huffmantree.cc:43] 2 1

I1109 16:06:23.855767 26257 test_huffmantree.cc:41] i --- [4]

I1109 16:06:23.855770 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 4

I1109 16:06:23.855775 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855780 26257 test_huffmantree.cc:42] 1 9

I1109 16:06:23.855785 26257 test_huffmantree.cc:42] 2 7

I1109 16:06:23.855790 26257 test_huffmantree.cc:42] 3 6

I1109 16:06:23.855795 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 4

I1109 16:06:23.855800 26257 test_huffmantree.cc:43] 0 1

I1109 16:06:23.855805 26257 test_huffmantree.cc:43] 1 0

I1109 16:06:23.855810 26257 test_huffmantree.cc:43] 2 0

I1109 16:06:23.855814 26257 test_huffmantree.cc:43] 3 1

I1109 16:06:23.855819 26257 test_huffmantree.cc:41] i --- [5]

I1109 16:06:23.855824 26257 test_huffmantree.cc:42] tree.NodePaths(i) --- 4

I1109 16:06:23.855829 26257 test_huffmantree.cc:42] 0 10

I1109 16:06:23.855834 26257 test_huffmantree.cc:42] 1 9

I1109 16:06:23.855839 26257 test_huffmantree.cc:42] 2 7

I1109 16:06:23.855844 26257 test_huffmantree.cc:42] 3 6

I1109 16:06:23.855849 26257 test_huffmantree.cc:43] tree.BranchPaths(i) --- 4

I1109 16:06:23.855854 26257 test_huffmantree.cc:43] 0 1

I1109 16:06:23.855859 26257 test_huffmantree.cc:43] 1 0

I1109 16:06:23.855865 26257 test_huffmantree.cc:43] 2 0

I1109 16:06:23.855870 26257 test_huffmantree.cc:43] 3 0

I1109 16:06:23.855875 26257 test_huffmantree.cc:45] tree.Height() --- [4]

[ OK ] huffmantree.func (0 assertion, 0 ms)

[----------] 1 test from huffmantree (0 ms total)

 
 

[----------] Global test environment tear-down

[==========] 1 test from 1 test case ran. (0 assertion total, 0 ms total)

[ PASSED ] 1 test.

 
 

Faster-rnnlm代码分析2 - HSTree的构造的更多相关文章

  1. tensorflow faster rcnn 代码分析一 demo.py

    os.environ["CUDA_VISIBLE_DEVICES"]=2 # 设置使用的GPU tfconfig=tf.ConfigProto(allow_soft_placeme ...

  2. tensorflow笔记:多层LSTM代码分析

    tensorflow笔记:多层LSTM代码分析 标签(空格分隔): tensorflow笔记 tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) ten ...

  3. angular代码分析之异常日志设计

    angular代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...

  4. wifi display代码 分析

    转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTS ...

  5. JQuery html API支持解析执行Javascript脚本功能实现-代码分析

    JQuery html用法(功能类似innerHTML) 开发中需要使用Ajax技术来更新页面局部区域, 使用的方法是ajax获取html代码段(字符串),然后将这个html代码段作为参数,传入目标D ...

  6. lighttpd与fastcgi+cgilua原理、代码分析与安装

    原理 http://www.cnblogs.com/skynet/p/4173450.html 快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关 ...

  7. 虚拟机创建流程中neutron代码分析(二)

    前言: 当nova服务发送了创建port的restful调用信息之后,在neutron服务中有相应的处理函数来处理调用.根据restful的工作原理,是按照 paste.ini文件中配置好的流程去处理 ...

  8. Hive metastore整体代码分析及详解

    从上一篇对Hive metastore表结构的简要分析中,我再根据数据设计的实体对象,再进行整个代码结构的总结.那么我们先打开metadata的目录,其目录结构: 可以看到,整个hivemeta的目录 ...

  9. (五) vivi代码分析

    目录 vivi代码分析 初始化注册 使用open/read/ioctl 系统调用分析 ioctl流程一览 总结 title: vivi代码分析 date: 2019/4/23 19:30:00 toc ...

随机推荐

  1. am335x sd卡启动开启识别emmc kernel 上的改动

    sbc 7109-454 sd 卡启动qt系统后一直识别不了  emmc 也就是mmc1口, 一开始以为是硬件初始化的问题,后面又以为是io口复用,最后才知道是根本没有注册mmc1设备. 更改下面的代 ...

  2. Mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist

    安装mysql后,启动时候没有启动成功,查看了下日志报错如下:---------------------------------------------1   可以:初始化mysql:mysql_in ...

  3. 2016年11月19日--连接查询,变量、if else、while

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union     在关 ...

  4. 读书笔记-Android初学笔记

    Eclipse [ADT] 源 https://dl-ssl.google.com/android/eclipse Notice that no matter what scenario causes ...

  5. web图片轮播实现

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 【转】【编码】ASCII 、UNICODE和UTF-8之二

    字符发展 1. 美国 ASCII-(American standard code information interchange) 美国信息互换标准代码 范围:1-127 ; 单字 备注:前部用作控制 ...

  7. ASP.NET MVC Area使用-将Area设置成独立项目

    环境说明:Vistual Studio 2013 MVC 4.0 其实关于ASP.NET MVC Area使用的基础知识可以参考 http://www.cnblogs.com/willick/p/33 ...

  8. JavaScript Window对象

    1.Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Location对象的href属性是一个字符串,后者包含 ...

  9. python to be Windows Daemon

    参考:http://assback.iteye.com/blog/1731565 安装 pywin32-.win32-py2..exe #32bit pywin32-.win-amd64-py2..e ...

  10. mybatis xml 大于 小于

    当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...