Java implementation of maximum entropy

Source: Internet
Author: User

This is a concise Java implementation of maximum entropy, which provides training and prediction interfaces. Training using GIS training algorithm, with sample training set. The purpose of this paper is to introduce the principle, classification and implementation of maximum entropy, which does not involve formula derivation or other training algorithms, please feel free to eat.

Introduction to Maximum entropy theory

The maximum entropy belongs to the identification model, which satisfies all the known constraints and makes no undue assumptions about the unknown information.

What do you call a known constraint? This article does not confuse you with obscure terminology, see an example:

Your friends "go out" or "self-house" every day, both of which are affected by the "weather", "mood", "humidity" (because she is a girl), we can call it characteristics.

We then collected some examples of "activities <-> features" from her microblog history, such as:

    1. "It's a nice day, I'm happy, so I went shopping."
    2. "Too dry, I want to sleep a beauty sleep!" ”
    3. "My spare tire number 2 is stepping on my Weibo homepage again, I'm mad!" The rain of the magic is cold to death, simply watch the United States play forget! "
    4. "God asked me to go shopping, even if the weather is bad, I also want to!" ”
    5. ......

We can intuitively feel that this is a ... Green tea ( ̄_ ̄| | |) off the topic ... We can intuitively feel that "good weather" on the "out" is a positive correlation, "good mood" is the same, the mood is negative correlation, but this is not absolute, it may only be "not dry" in the case of the establishment.

Maximum entropy can digitize our intuition as a feature (or feature function) and calculate how important each feature is. Constraint refers to the distribution of the predicted results to meet the probability of the characteristics of the statistics, and these probabilities are evenly distributed, the final result is that the system has the largest entropy.

The maximum entropy does not assume that "weather" and "mood" are distributed independently, nor does it recognize that "weather" has an effect on "mood", perhaps it does have an effect, but the maximum entropy only guarantees that the final result conforms to the probability constraint.

If you have deep mathematical accomplishment and enough time, you can choose to read the papers in the appendix and the derivation process, where you will get a strict description with the formula derivation.

Classification

The maximum entropy model can be divided into 2 probability estimates according to the sample information: joint maximum entropy model and conditional maximum entropy model. Assuming that a is an event and B is the environment (or context) in which event a occurs, the union probability of A and B is recorded as P (A, B). Generally, the set of all possible events is set to a, the set of all environments is B, and the joint maximum entropy model for any given a∈a, b∈b, probability P (A, B) must be established. To calculate the probability that event a will occur under the condition of B, i.e. probability p (A | b), the conditional maximum entropy model must be established.

The maximum entropy model implemented in this paper belongs to the conditional maximum entropy model.

Realize

Already open the project to Https://github.com/hankcs/MaxEnt, check out the Java code and proceed to the next step.

(PS: Main code from "csdn thick", please refer to reference)

Training set

If we create a computer-readable data set Data/train.txt (already included in the Open Source project), the micro-blog we collected above is:

Outdoor Sunny happyoutdoor Sunny Happy dryoutdoor Sunny Happy humidoutdoor Sunny sad dryoutdoor Sunny sad Humidoutdoor Clo Udy Happy Humidoutdoor Cloudy Happy Humidoutdoor Cloudy sad Humidoutdoor cloudy sad humidindoor Rainy Happy Humidindoor Ra Iny Happy dryindoor Rainy sad dryindoor Rainy sad Humidindoor Cloudy sad humidindoor cloudy sad humid

We see that the data has a maximum of 4 columns, the first column of each row represents the day's activity, and the rest represents the day's environment.

Training

The purpose of training is actually to calculate a set of optimal Lagrange multipliers, which correspond to how important each characteristic function is.

GIS algorithm

Define λi as the Lagrange multiplier for feature I, C for the maximum number of features per event, and the numerator and denominator in log represent the expectation of empirical distribution and the estimation of the model.

The GIS algorithm uses the nth iteration model to estimate the distribution of each feature in the training data. If the actual (fraction is less than 1,log get negative), the corresponding parameter will be smaller (plus the negative number will be smaller). Otherwise, make them larger. The optimal parameters are obtained when the characteristic distribution of the training sample is the same as the characteristic distribution of the model.

This formula is described in Java as follows:

         for (int i = 0; i < maxit; + +i)        {            Computemodee (modele);              for (int w = 0; w < weight.length; w++)            {                = weight[w];                 + = 1.0/c * Math.log (empiricale[w]/ modele[w])            ;            } if  Break ;        }
Forecast

Finally, the most exciting moment, as a spare tire 2nd, you are going to ask her to see the film tomorrow. You know from the weather forecast that the weather is fine tomorrow and the humidity is good. So what's the odds of her agreeing to go out?

        String Path = "Data/train.txt";         New MaxEnt ();        Maxent.loaddata (path);        Maxent.train ($);        ListNew arraylist<string>();        Fieldlist.add (//  if sunny        //  and moist        pair<string, Double >[] result = Maxent.predict (fieldlist);  // predicting the probability        of going out and dwelling System.out.println (arrays.tostring (result));

Output
    1. [Outdoor=0.9747657631914007, Indoor=0.025234236808599233]

It seems that the probability of going out is up to 97%.

Transferred from: http://www.hankcs.com/nlp/maximum-entropy-java-implementation.html

Java implementation of maximum entropy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.