Runner

Hacking to the gate


  • Home

  • Tags

  • Categories

  • Archives

Beauty of Math 2

Posted on 2017-05-24 | In Reading Note , Beauty of Math

数学之美 笔记2

<br>

如上篇笔记所言,我非常喜欢吴军老师的这本《数学之美》。这篇阅读笔记包含信息论中的一些重要概念,它们对于通信、数据压缩以及自然语言处理都有很重要的指导意义。

信息熵与冗余度

信息熵是衡量信息大小的一个度量概念,由香农博士(Claude Shannon)提出。书中首先用了一个简单的例子来介绍信息量的概念,假设有32支球队参加世界杯,那么所有球队需要5位的二进制编码来完整表示,所以”谁是世界冠军“的信息量就是5比特。信息量的大小一般与所有可能情况的对数函数 $log$ 相关。

但是众所周知,这32只球队每支球队夺冠的可能性,或者说概率是不可能完全相等的,因此假设让一个懂球的人来猜,或许只需要3-4次就能猜出冠军球队(将少数几个强队分成一组,其他分成另一组,然后猜测冠军是否在热门球队组中,重复此过程)。所以,严格来说”谁是世界杯冠军“的信息量要比5比特少。香农博士指出,其准确的信息量是

$$ H = - \sum p_i ; log(p_i) $$   其中,$p_i$ 表示第 $i$ 支球队的夺冠概率。香农将它称作信息熵(Entropy),用 $H$ 表示,单位是比特。信息熵的通用公式为 $$    H = -\sum\limits_{x\in X} P(x);logP(x) $$

信息论中另一个有用的概念是冗余度。举个简单的例子,一本书有50万字,用一个较好的算法对其进行压缩,信息量大约是250万比特,也就是320KB;但是如果不进行压缩,仅用国际的两字节编码储存这本书,需要大约1MB,这两个数量的差距就是这本书的冗余度。在所有语言中,汉语是一个冗余度相对较小的语言。

信息的作用

自古以来,信息与消除不确定性就是相互关联的。抽象来说,一个事物内部总是会存在随机性,也就是不确定性,假定为 $U$ ,而消除它的唯一办法就是引入信息 $I$ ,引入信息的量取决于 $U$ 的大小,要 $I > U$ 才行。若引入的信息量 $I$ 小于 $U$,则引入信息后的不确定性可以用式子

$$ U' = U - I $$

来表示。

网页搜索本质上来说就是利用信息消除不确定性的过程。给定用户提供的关键词,怎么才能从成千上万亿的网页中找到用户需要的呢?这时正确的做法应当是去挖掘正确有用的信息,比如网站的质量信息;如果还是不够,不妨问问用户。不正确的做法是去玩弄数字以及公式,这样做往往没有什么效果;而最糟糕的做法是去人为地“蒙”,虽然“蒙”增加了信息,并满足了一部分用户的口味,但是同时却牺牲了其他大部分用户,本质上看搜索结果更糟。合理运用信息,而非玩弄什么公式和机器学习算法是做好搜索的关键。

互信息

互信息是香农在信息论中提出的概念,用以量化度量两个随机事件的相关性。假设有两个随机事件 $X$ 和 $Y$ ,它们的互信息定义是: $$   I(X;Y) = \sum\limits_{x\in X,,y\in Y}P(x,y),log\frac{P(x,y)}{P(x)P(y)} $$   别看这个公式复杂,其实它与之前我们提到的信息熵密切相关。通过数学证明,互信息就是随机事件的信息熵 $H(X)$ 与在知道随机事件 $Y$ 后的条件熵 $H(X|Y)$ 的差,即 $$ I(X;Y) = H(X) - H(X|Y) $$   值得指出的是,互信息是一个取值在 0 到 $min(H(X), H(Y))$ 之间的函数。当 $X$ 和 $Y$ 完全相关时,它的取值是 $H(X)$,同时 $H(X)=H(Y)$; 当两者完全无关时,它的取值是 0。

那么互信息有什么用呢?机器翻译中,最难的两个问题之一是词义的多元性,(又称歧义性)。比如Bush既是总统布什的名字,又是灌木丛的英文,所以在翻译时机器很难简单的判断选择哪个意思。一种简单而有效的解决办法就是使用互信息:首先从大量文本中找出和总统布什一起出现的互信息最大的词,比如总统、国会等;再用同样方法找出和灌木丛一起出现的互信息打的词,比如土壤、植物等。有了这些,在翻译时看看上下文哪组词多就行了。

交叉熵

交叉熵 或者 相对熵 (Kullback-Leibler Divergence)是库尔贝克和莱伯勒提出的用以衡量两个取值为正的函数的相似性的概念。其定义如下: $$ KL(f(x)||g(x)) = \sum\limits_{x\in X}f(x)\cdot log\frac{f(x)}{g(x)} $$   如书中所说,公式本身并不重要,只要记住下面三条结论:

  1. 对于两个完全相同的函数,其交叉熵为 0。

  2. 交叉熵越大,两个函数差异越大;反之,交叉熵越小,函数差异越小。

  3. 对于概率分布或者概率密度函数,若取值均大于零,交叉熵可以用来度量两个随机分布的相似性。

    值得注意的是,交叉熵是不对称的,这样用起来有时候不是很方便。因此,詹森和香农提出了一种新的交叉熵: $$ JS(f(x)||g(x)) = \frac{1}{2}[KL(f(x)||g(x)+KL(g(x)||f(x))] $$

小结

信息熵,条件熵和交叉熵这三个概念与语言模型的关系非常密切。信息熵不仅是对信息的量化度量,而且是整个信息论的基础。它可以直接用于衡量统计模型的好坏。如果有了上下文条件,应该用条件熵。如果再考虑到从训练语料和真实应用的文本中得到的概率函数有偏差,就需要再引入交叉熵的概念。


参考

-《数学之美》第二版第六章 -- 吴军

Beauty of Math 1

Posted on 2017-05-22 | In Reading Note , Beauty of Math

数学之美 笔记1

<br>

Beauty of Math is a book by Jun Wu, the former vice CEO of Tencent. It gives me a lot of inspiration so I chose to write down some of the reading note. Since the book is in Chinese, I felt much more convenient to write my note in Chinese and please forgive that.

前一周有幸拜读了吴军老师当下的畅销书 --《智能时代》,感悟良多。根据书中指引,遂购买了吴军老师前几年颇具影响力的另一著作 -- 《数学之美》。作为数学专业的学生,阅读此书只觉得爱不释手,颇受启发,于是萌生记录阅读心得的想法。此篇写于去往南京的飞机上,于南京完成。

隐含马尔可夫模型

隐含马尔可夫模型(Hidden Markov Model)是书中的第五章,在前几张介绍了自然语言处理的基本思路后,自然而然的过渡到了这个简单高效的,解决自然语言处理难题的模型。

通信模型

书中首先介绍了基本的通信模型。雅各布森(Roman Jakobson)提出的通信六要素:发送者(信息源),信道,接受者,信息,上下文和编码。这一模型同样可被应用到自然语言的处理中,几乎所有的自然语言处理问题都可以等价成通信的解码问题。

给定观测信号$o_1, o_2, o_3...$,推测源信息$s_1, s_2, s_3...$可通过从所有信息中找到最可能产生观测信号的那一信息来完成。从概率角度说,就是求条件概率的最大值,即 $$ s_1,s_2,s_3... = Arg,Max;P(s_1,s_2,s_3...|o_1,o_2,o_3...) $$ 条件概率可通过贝叶斯公式变换为 $$ \frac {P(o_1,o_2,o_3...|s_1,s_2,s_3...) \cdot P(s_1,s_2,s_3...)}{P(o_1,o_2,o_3...)} $$

其中,因为 $o_1, o_2, o_3...$ 已经产生了,其概率是一个可以忽略的常数,所以剩下的只是

$$ P(o_1,o_2,o_3...|s_1,s_2,s_3...) \cdot P(s_1,s_2,s_3...) $$

而这个公式完全可以用隐含马尔可夫模型来估计。

隐含马尔可夫模型

随机过程一般有两个维度的不确定性,一是对任意时间 $t$ ,其状态 $s$ 都是不确定的;二是一个状态 $s$ 受其他状态影响。而马尔可夫模型假设对任意状态 $s_t$ ,它只受前一个状态,即 $s_{t-1}$ 影响。这一假设被称为马尔可夫假设,而符合这一假设的随机过程被称为马尔可夫链。

计算某一状态 $m_i$ 到另一状态 $m_j$ 的转移概率可以通过数出 $m_i$ 出现的次数以及 $m_i$ 出现后 $m_j$ 出现的次数,后者除以前者便是其转移概率的估值。

隐含马尔可夫模型是上述的一个拓展:任一时刻 $t$ 的状态 $s_t$ 是不可见的,因此无法观测得到状态序列 $s_1,s_2,s_3...$ 来推测转移概率。但是,隐含马尔可夫模型在每一时刻 $t$ 都会输出一个符号 $o_t$ ,且 $o_t$ 只跟 $s_t$ 有关,所以我们可以就算出某个特定的状态序列 $s_1, s_2, s_3...$ 产生输出符号 $o_1, o_2, o_3...$ 的概率。

$$ P(o_1,o_2,o_3...|s_1,s_2,s_3...) = \prod P(s_t|s_{t-1}) \cdot P(o_t|s_t) $$

至于如何找出上式的极值,可以利用维特比算法(Viterbi Algorithm)。

小结

隐含马尔可夫模型最初用于通信领域,是连接自然语言处理和通信的桥梁,同时也是机器学习的一大工具。


参考

-《数学之美》第二版第五章 -- 吴军

Hadoop Basic

Posted on 2017-05-21 | In Study Note , Bigdata , Hadoop

Hadoop Study Note 1

<br>

The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.

Hadoop is built by Doug Cutting, the pioneer in the area of Bigdata. One fun fact, the name Hadoop is borrowed from Cutting's three-year-old son, and it is originally for his toy, a yellow elephant, which also becomes the well-known logo of Hadoop.

<img src="http://tse4.mm.bing.net/th?id=OIP.te5YCTFCrqh4N2S65P8cVgEpEs&pid=15.1" alt="Hadoop logo" width=100px, align=center></img>

Nowadays, Hadoop mainly refers to the enormous products in its ecosystem.

Main Components

MapReduce

MapReduce is the core component of Hadoop.

MapReduce is a programming model and an associ- ated implementation for processing and generating large data sets.

MapReduce consists of two main parts: Map function and Reduce function. It's first brought up by engineers from Google and inspired from functional programming languages.

HDFS

Hadoop Distributed File System (HDFS) is a open-source implementation of Google File System (GFS). It's specifically made for big data with high-throughput access to application data and is highly fault tolerant. In addition, HDFS can be deployed on relatively cheap machine.

Hive

A data warehouse infrastructure that provides data summarization and ad hoc querying.

Hive provides complete functionality for SQL query. It transforms SQL into MapReduce task. Hive is specifically designed for statistical analysis.

HBase

HBase is a distributed, column-store database. It can be seen as a open-source implementation of Google's Bigtable. Unlike relational database, it supports storage for non-structured data.

Pig

Pig is a high-level programming language that, as its inventor puts, fits in the sweet spot between the declarative style of SQL, and the low-level, procedural style of MapReduce.

As my Bigdata course lecturer says, when something doesn't work out very well, programmers just put an extra layer of abstraction on top of it. Pig is the one on top of MapReduce. It highly simplifies the code and free engineers from heavy work for simple analysis.

ZooKeeper

ZooKeeper is the open-source implementation of Google's Chubby. It provides a high-performance coordination service for distributed applications.

Application of Hadoop

Hadoop is suitable for all of the following situations:

  1. Huge volume of data.
  2. Offline data processing. It's well-known that Hadoop perform poorly in streaming data processing or real time analysis.
  3. Parallel computing. MapReduce works pretty well for parallel task, for example, web search for information.
  4. Large files. Base on the design choice of HDFS, Hadoop is suitable for processing bigger files.

Reference

[1]. MapReduce: Simplified Data Processing on Large Clusters, Jeffrey Dean and Sanjay Ghemawat

[2]. http://hadoop.apache.org/

[3]. Pig Latin: A Not-So-Foreign Language for Data Processing, Christopher Olston, Benjamin Reed and Utkarsh Srivastava

Hacking Hexo

Posted on 2017-05-20 | In Study Note , Hexo

This is a post I used to test the functionality of Hexo.

Basics

I added a tag and a category to this post to learn how Hexo works. In addition, I added a music player at the bottom of this page, which is much simpler than I expected. The code for the music player is as followed:

1
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=28481646&auto=1&height=66"></iframe>

Here I play music from music.163.com, where one can generate external link easily. To auto play the music, simply change the auto=1 to auto=0 in the code above.

Math Formulas

I also tried Mathematic formula here with my editor. For example, Eulers Formula is:

$$ e^{\pi i} + 1 = 0 $$

To support $\LaTeX{}$ in Hexo, we need to install additional plugin. Thanks to [1], the process is quite simple. All we need to do is to install MathJax by:

1
2
npm install hexo-math --save
hexo math install

However, since Hexo already uses Marked.js to render our page, we need to ensure no conflicts between these two plugins.

The only conflicts are escape characters and underscore for italic. We can ignore the latter since for markdown, * can also be used to show italic.

For escape characters, we need to change marked.js file in ./node_modules/marked/lib/.

First change

1
escape: /^\\([\\`*{}\[\]()# +\-.!_>])/,

to

1
escape: /^\\([`*\[\]()# +\-.!_>])/,

Then change

1
em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

to

1
em:/^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,

Done!


<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 auto=0 src="//music.163.com/outchain/player?type=2&id=28481646&auto=0&height=66"></iframe>


Reference

[1] http://blog.csdn.net/emptyset110/article/details/50123231

1…34
Ziqi Yang

Ziqi Yang

Welcome to my hub

34 posts
14 categories
14 tags
GitHub E-Mail
© 2018 Ziqi Yang
Powered by Hexo
|
Theme — NexT.Pisces v5.1.4