Friday, March 9, 2012

Unconstrained optimization

  • The rationality of the step size

In the following section, we would begin to discuss two conditions which a_k(the step size) have to satisfy. If the two conditions are both satisfied, we could say that the proper step size, from x_k to x_(k+1), has been found. The first condition called “sufficient decrease condition”.  Intuitively, this condition keeps that the value at x_(k+1) should be smaller than x_(k)`s, which is required to reach the convergence. The second one is called “curvature condition”. This condition keeps that the derivative of f at x_k must be smaller than x_(k+1)`s.

  • Sufficient decrease condition

A proper a_k must satisfy sufficient decrease condition. We could make the following descriptions about this condition:

f( x_(k+1) ) <= f( x_k ) + c_1 * a_k * ▽f_k^T * p_k;

if we plugin EQ1, we have

f( x_k + a_k * p_k ) <= f( x_k ) _ c_1 * a_k * ▽f_k^T * p_k

where f( x_k ) is the value of f at x_k; ▽f_k means the gradient at x_k; p_k means the direction from x_k to x_(k+1); a_k means the step size and c_1 is a constant that belongs to [0, 1]

If p_k is the decent direction of function f, we have ▽f_k * p_k < 0. So f( x_k + 1 )<f( x_k ).

As only a_k is a variable, we could rewrite as followed:

Ø(a_k) ≤ l(a_k), where

 

We could visualize the condition as below:

If the step size could make Ø(a_k) in the acceptable interval, the condition is satisfied.

  • Curvature condition

We could describe this condition as following:

 

or     

In the above equation, ▽f_(k+1) is the gradient at x_(k+1); p_k is the decent direction and c_2 is an constant belongs to [0 1] and usually set as 0.9.

When p_k is the decent direction of the function f, then ▽f_k * p_k < 0

So, it means ▽f _k+1 ≥ c_2 * ▽f_k

From the plot below, we could see that the equation above requires that the decent “speed” at x_(k+1)should be smaller than x_k`s.

  • Wolfe condition

Wolfe condition could be seemed as the combination of the two conditions mentioned above, which means the a_k should meet the following equations.

We could visualize the condition as:

In some real optimization application, we may have to use another condition, called string Wolfe condition, which is stemmed from the Wolfe condition. In this case, the a_k need to meet the followings:

The only difference between those two conditions is the strong Wolfe condition avoids the possible positive value of  ▽f(x_k + a_k * p_k).

 

To be continued.

Unconstrained optimization

NOTE: this article is based on a serial blog from 52nlp.cn written by “jianzhu”. I translated from the Chinese version but there are not exactly the same. The original link to the blogs is attached: Click here. 

1. What is unconstrained optimization problem.

The above image shown an example of unconstrained optimization problem. Suppose we have function, called f, and its plot in 2D space is as above. Unconstrained optimization is a process that tries to find the minimum point(s) of f. In the figure above, there are two minimum points, one is global minimum(or minimal point) and the other one is a local minimum. As the limitation of the complexity of the algorithm, most unconstraint optimizer algorithms could only guarantee a local minimum. If we could only find local minimum, why we could still apply those “not-so-good” algorithms to solve our problems? From my point of view, there are mainly two reasons, the first is that many problems we are facing are modeled as a convex function, which means that the minimum and minimal are the same point; the second reason is that our world is not perfect, local minimum may sufficient for us in some cases.

Now we have known what is the unconstraint optimization problem, let us work on the basic process of unconstraint optimization. The first thing we have to do is to find an initial point, denoted as a red dot. Sometimes we randomly choose that point, sometimes we guess it with some heuristics, the rest of the time we could calculate it. It is all depended on what is your problem.

 

After we have the initial point, we could begin a arbitrary optimization algorithm to find the minimum point. During the optimizing, two concepts are critical, direction and step size.

2. Line search

Line search is used to calculate the step size, which mentioned above. Once we find the direction, we need to find how far we should move from current point, x_k and stop at the next “proper” point, x_(k+1). If we use p_k to denote the direction from the k-th point to the (k+1)-th point, x_k denotes current point, x_(k+1) denotes the nect point, and a_k denotes the k-th step size, then the following equation holds:

x_(k+1) = x_k + a_k * p_k

In most line search method, p_k should be the decent direction, which means f(x) would decrease if we move along p_k. As the goal is to find a minimum value, the best situation is that find a a_k that satisfies f(x_(k+1)) is the global minimum along p_k. The following equation shows the best situation:

Ø (a_k) = f(x_k+1) = f(x_k + a_k * p_k)

If we want to find a step size that makes Ø (a_k) to be global minimum, a great amount of calculation related to f(x_k+a_k*p_k); on the other hand, if we consider this problem from derivative point of view, we have to calculate ▽f_k+1, which is also unrealistic. So, we have to make some tradeoff between computation load and problem solving.

If we cannot build Roma in one day, what we should do with a_k? We give two constraints to determine what is a reasonable a_k and what is the size of a_k.

 

To be continued.

Wednesday, March 7, 2012

A small project on Pinterest, which needs web spider...

As we are doing a small project about Pinterest, we have to download some data, including images and "re-pin" relations, from www.pinterest.com. At the beginning, I suppose this project is purely a computer vision project, which only needs taking care of the algorithms and the implementation. But I am worry, Pinterest has not opened its API yet, which means I may need to implement a simple web spider to traverse the web page and extract the data we need out. It is a complicate thing... After few hours of investigation, I found this "answer" from StackOverflow, which is very useful and inspiring.


Writing a spider for the whole WWW is quite a task --- you have to take care about many "little details" such as:
  • Each spider computer needs should receive data from a few thousand servers in parallel in order to make efficient use of the connection bandwidth. (asynchronous socket i/o).
  • You need several computers that spider in parallel in order to cover the vast amount of information on the WWW (clustering; partitioning the work)
  • You need to be polite to the spidered web sites:
    • Respect the robots.txt files.
    • Don't fetch a lot of information too quickly: this overloads the servers.
    • Don't fetch files that you really don't need (e.g. iso disk images; tgz packages for software download...).
  • You have to deal with cookies/session ids: Many sites attach unique session ids to URLs to identify client sessions. Each time you arrive at the site, you get a new session id and a new virtual world of pages (with the same content). Because of such problems, early search engines ignored dynamic content. Modern search engines have learned what the problems are and how to deal with them.
  • You have to detect and ignore troublesome data: connections that provide a seemingly infinite amount of data or connections that are too slow to finish.
  • Besides following links, you may want to parse sitemaps to get URLs of pages.
  • You may want to evaluate which information is important for you and changes frequently to be refreshed more frequently than other pages. Note: A spider for the whole WWW receives a lot of data --- you pay for that bandwidth. You may want to use HTTP HEAD requests to guess whether a page has changed or not.
  • Besides receiving, you want to process the information and store it. Google builds indices that list for each word the pages that contain it. You may need separate storage computers and an infrastructure to connect them. Traditional relational data bases don't keep up with the data volume and performance requirements of storing/indexing the whole WWW.
This is a lot of work. But if your target is more modest than reading the whole WWW, you may skip some of the parts. If you just want to download a copy of a wiki etc. you get down to the specs of wget.
Note: If you don't believe that it's so much work, you may want to read up on how Google re-invented most of the computing wheels (on top of the basic Linux kernel) to build good spiders. Even if you cut a lot of corners, it's a lot of work.
Let me add a few more technical remarks on three points
Parallel connections / asynchronous socket communication
You could run several spider programs in parallel processes or threads. But you need about 5000-10000 parallel connections in order to make good use of your network connection. And this amount of parallel processes/threads produces too much overhead.
A better solution is asynchronous input/output: process about 1000 parallel connections in one single thread by opening the sockets in non-blocking mode and use epoll or select to process just those connections that have received data. Since Linux kernel 2.4, Linux has excellent support for scalability (I also recommend that you study memory-mapped files) continuously improved in later versions.
Note: Using asynchronous i/o helps much more than using a "fast language": It's better to write an epoll-driven process for 1000 connections written in Perl than to run 1000 processes written in C. If you do it right, you can saturate a 100Mb connection with processes written in perl.
The down side of this approach is that you will have to implement the HTTP specification yourself in an asynchronous form (I am not aware of a re-usable library that does this). It's much easier to do this with the simpler HTTP/1.0 protocol than the modern HTTP/1.1 protocol. You probably would not benefit from the advantages of HTTP/1.1 for normal browsers anyhow, so this may be a good place to save some development costs.
Partitioning the work over several servers
One computer can't keep up with spidering the whole WWW. You need to distribute your work over several servers and exchange information between them. I suggest to assign certain "ranges of domain names" to each server: keep a central data base of domain names with a reference to a spider computer.
Extract URLs from received web pages in batches: sort them according to their domain names; remove duplicates and send them to the responsible spider computer. On that computer, keep an index of URLs that already are fetched and fetch the remaining URLs.
If you keep a queue of URLs waiting to be fetched on each spider computer, you will have no performance bottlenecks. But it's quite a lot of programming to implement this.
Read the standards
I mentioned several standards (HTTP/1.x, Robots.txt, Cookies). Take your time to read them and implement them. If you just follow examples of sites that you know, you will make mistakes (forget parts of the standard that are not relevant to your samples) and cause trouble for those sites that use these additional features.
It's a pain to read the HTTP/1.1 standard document. But all the little details got added to it because somebody really needs that little detail and now uses it. 


The original link is attached here.

Wednesday, February 22, 2012

“贞操女神”代表一种什么潮流?

最近有个叫涂世友的老姑娘火了,因为她办了一个叫“雅品贞操网”的网站,公开宣传婚前守贞!而且她还被请到电视台,与一些专家学者共同做了几期节目!
从我的个人感情上来说,贞操不贞操的真不是这种老姑娘有资格说的!因为前几天,在知名的天涯论坛上,有另一个帖子,发帖的是个即将步入婚姻殿堂的姑娘!本来这将是她人生最重要的选择,和最幸福的时刻,但她却陷入了深深的矛盾!为什么呢,因为他未婚夫的生殖器很短小!一方面她害怕婚后守活寡;另一方面她害怕不能正常怀孕!这个帖子好像一个石头,在一个暗流涌动的水面上激起了层层波浪!很多已婚的大哥大姐出来劝她,给她出主意;也有心智刚刚成熟的小年轻过来看热闹!纵观下面的回帖,劝退的呼声占了大多数,很多人都劝她要以个人幸福为重,并以“婚后后悔为时已晚”为切入点,劝她未雨绸缪!我觉得这些回帖的人都是真性情,而且讲了真话!如果日后夫妻生活不和谐,肯定将大大影响婚姻的稳定性,而且对两家伤害都更大!
为什么这么一个浅显而直接的因果利害,在现实得不到声援呢?而轻松的让一个所谓的贞操女神轻易的夺取了主动权呢?我个人认为,还是封建流毒的残留在作祟!我们先来看看这老姑娘是怎么看婚前不守贞的:

  1. 婚前不守贞,是意外怀孕,性疾病的根源,这些对女性伤害很大;
  2. 婚前不守贞,婚后会被老公翻旧账;
  3. 婚前守贞是传统美德;
  4. 这种美德需要宣传;
  5. ......
上面几点是我个人总结的,可能不是涂女士的本意,但这几点可以说是被公众,或主流,认可的。那我就从这几点谈谈我的看法。

对于第一条,我只能说“无稽之谈”!所谓的意外怀孕是保护措施不到位造成的,如果说要追究责任,那首先是这个社会的教育,没有告诉我们的孩子,该如何保护自己和对方!现在的避孕套有百分之九十几的可靠性,配合女性避孕药,怀孕的可能性真的很低!至于性病什么的,正确的保护措施能降低感染概率,而且性病的传播跟婚姻没关系,即所谓滥交不能和婚前性行为等价!而且所谓的贞操,也不是用来防止性病的,而且禁锢女性!因为她都在强调婚前守贞,那婚后呢,婚后不检点的性病风险他们考虑了么?
第二条嘛,就有明显的“自残”倾向了!一位学者说的好,怎么找靠谱的男人,你自己独立了,周围的男人都靠谱了!这句话说的很好,因为一个女人怕男的翻旧账,其实就是依附于一个男人的表现!如果你不依附他,你追求你自己的幸福,你怕什么!而且这种所谓翻旧账的男人,一般都是心理阴暗,极端自私,而且很尖酸刻薄的人!如果你丈夫用这么下流的手段攻击你,这人我看你是白嫁了!因为他要的是所谓的处女这个名词,而不是你!
第三条,我只能用放屁形容!当年的缠足,裹胸,不让女性外出上学和工作等等,都是老祖宗恪守了几百年的东西,你能说这是美德么?所以说,传统的,不全是美德,还有糟粕!晚清和民国时期,中国人学习西方,解放妇女,比如天足运动,天乳运动,兴办女学,同工同酬等等,这些现在看来都是理所应当的事!但在当时,这些都是付出过血的代价的!既然物质上禁锢女性的枷锁已经被打开了,那精神上的枷锁为什么不能也考虑打开呢?而且,这个枷锁是不平等的,是“欺负”和针对女性的,即使我们现在没能力打开,也要正视它为糟粕!
第四点,我觉得以第三点为基础,我们可以知道,婚前守贞纯粹是一种习惯,我们不管它是陋习还是美德,但至少它还有争议!既然他有争议,不确定,我们就不能因为自己的好恶,给它强加道德标签,进而攻击另一方!郭德纲有段相声说的好,喝咖啡和吃大蒜,没有高低贵贱之分,但喝咖啡的跳出来说吃大蒜的低俗,就不对了!对婚前守贞这是,我倾向于好坏都给他们和她们摆清楚,然后根据自己的选择行事。任何人和组织不能歧视和攻击任何一方!

最后想起一个笑话,一个中国人鄙视美国人,说“我真不敢相信,你们见一次面就能上床”;那美国人反驳,道“我也不敢相信,你们没上过床就敢结婚”!
这个笑话告诉我们,婚后的不和谐,对婚姻,家庭和社会的坏处更大,性作为引起不和谐的一个因素,我们为什么不避重就轻?其实很多人都是这么做的,但这层窗户纸,没人愿意点破罢了!

Monday, January 30, 2012

After 23 hours of flight traveling and 4 hours of train traveling, I have been home yesterday. This vacation in China is not as good as I supposed. The main reason is that my visa is posted to an administrative procedure, which means I could not get the visa on the interview day. Although it is impossible to reject normal F1 visa in the past, my concerning is that it may take longer time during Chinese Spring Festival and make me miss the  beginning of the new semester. It really makes me uncomfortable and anxious. My whole holiday is ruined.
Fortunately, my father support me to reassign the return the flight and extend the return date for one week. So I enjoyed the Spring Festival in China with my parents, have the great diner with my maternal grandfather and grandmother, and the warm chat with two friends since high school.
Another byproduct of this fight changing is that I could stay in Japan for 6 hours. Although I cannot go out, it is still interesting to walk around in the airport and shopping in the duty-free shop for local products. The airport is not as big as Beijing`s, but it is really carefully design and operated. There are mini capsule hotel, traditional bath and massage, a great number of duty free products which are not limited to Japan`s. Another interesting thing is that they hired some shopping guides from Taiwan, which could provide guidance service in Chinese to promote sales. Hereby,  I have to concede that the internationalization in Japan, and Taiwan, is much deeper, and more effective, than China, as they have learnt how to open their mind to work for the people from anywhere around the world.

Saturday, December 10, 2011

What I should do during the winter vacation

The last class was over today. There are three finals and a project need to do. I think it is necessary to make a plan before the beginning of the vacation, which could make the plan more aggressive and ambitious. Now what is in my mind is as followed:

  1. do some ACM-ICPC problems. The best way to do it may be not by “Interview street”, as Lanbo told me that the problem there is a little complicated. I think I could find some problem from those blogs. I think I could do 30 or 40 and ensure the answers are correct;
  2. Get more familiar with CUDA, OpenCV, numerical calculation and matrix computation;

There may be something more, to be continued.

Thursday, December 8, 2011

How to verify the unique min-cut

How to verify that there is only one min-cut in a graph G(V, E), which have one sink, t, and a source, s. The first idea is as followed:
  • run the max-flow algorithm, such as Ford-Fulkerson or Edmonds-Karp, and keep the residual graph as G`.
  • run DFS, from s and t, on G` and find out a reachable set R1 and R2.
  • if and only if R1 + R2 = E, then return true, otherwise, return false.
This method uses a constraint of min-cut that all the edges through a min-cut in a max-flow graph have 0 capacity. So, if there are two min-cut, there must be another “bottleneck” that does not allow DFS to reach the node.
For a directed graph G(V, E). We could do the following to verify the unique or not.
  • run the max-flow algorithm;
  • for each edge e though the min cut, increase the capacity;
  • if the max flow does not change, the min cut is not unique; otherwise, it is unique.