MAchine Learning for LanguagE Toolkit ### Optimization

MALLET includes methods for numerically optimizing functions. The primary use for numerical optimization in machine learning is to find parameters that maximize a log-likelihood function given observed data. The code, however, is very general and can be used for arbitrary problems.
The cc.mallet.optimize package centers around two interfaces, Optimizable and Optimizer. Optimizable has several sub-interfaces that define different classes of methods for optimization. Optimizable classes are stateful; they must store the current values of all parameters. The most commonly used optimizable sub-interface is Optimizable.ByGradientValue. Classes that implement this interface must provide methods for calculating the value of a specific function at the current parameter settings and for calculating the gradient of the function, with respect to the current parameter values. Note that this interface does not require the Hessian matrix of second derivatives, so the interface is appropriate for numerical methods that approximate the Hessian.
The following is an example of a class implementing Optimizable.ByGradientValue. This class implements a very simple quadratic function in two variables.
```public class OptimizerExample implements Optimizable.ByGradientValue {

// Optimizables encapsulate all state variables,
//  so a single Optimizer object can be used to optimize
//  several functions.

double[] parameters;

public OptimizerExample(double x, double y) {
parameters = new double;
parameters = x;
parameters = y;
}

public double getValue() {

double x = parameters;
double y = parameters;

return -3*x*x - 4*y*y + 2*x - 4*y + 18;

}

public void getValueGradient(double[] gradient) {

gradient = -6 * parameters + 2;
gradient = -8 * parameters - 4;

}

// The following get/set methods satisfy the Optimizable interface

public int getNumParameters() { return 2; }
public double getParameter(int i) { return parameters[i]; }
public void getParameters(double[] buffer) {
buffer = parameters;
buffer = parameters;
}

public void setParameter(int i, double r) {
parameters[i] = r;
}
public void setParameters(double[] newParameters) {
parameters = newParameters;
parameters = newParameters;
}
}
```
Now that we have a class representing an optimizable function, we can pass it to an optimizer that takes this sub-interface. The ByGradientValue sub-interface is compatible with the Limited Memory BFGS optimizer, a quasi-Newton method that does not require computation of a Hessian matrix.
In this example, we create an optimizable object, pass it to a new optimizer, and optimize the parameters.
```        OptimizerExample optimizable = new OptimizerExample(0, 0);
Optimizer optimizer = new LimitedMemoryBFGS(optimizable);

boolean converged = false;

try {
converged = optimizer.optimize();
} catch (IllegalArgumentException e) {
// This exception may be thrown if L-BFGS
//  cannot step in the current direction.
// This condition does not necessarily mean that
//  the optimizer has failed, but it doesn't want
//  to claim to have succeeded...
}

System.out.println(optimizable.getParameter(0) + ", " +
optimizable.getParameter(1));
```
As expected, the result is close to 0.33 and -0.5.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5269533,"math_prob":0.92863476,"size":3227,"snap":"2019-35-2019-39","text_gpt3_token_len":728,"char_repetition_ratio":0.19205709,"word_repetition_ratio":0.0,"special_character_ratio":0.22528665,"punctuation_ratio":0.1352381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.991316,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-08-21T03:44:38Z\",\"WARC-Record-ID\":\"<urn:uuid:eec86fa8-ba1f-47fe-8dae-414dbd368f1c>\",\"Content-Length\":\"5776\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:91f86775-d2c1-4017-970c-c912d6c97454>\",\"WARC-Concurrent-To\":\"<urn:uuid:247918f4-2557-41ca-a1df-88d032537d15>\",\"WARC-IP-Address\":\"128.119.246.70\",\"WARC-Target-URI\":\"http://mallet.cs.umass.edu/optimization.php\",\"WARC-Payload-Digest\":\"sha1:DH67NAO7THFEBRCSKBSTHN4UTZF25E2H\",\"WARC-Block-Digest\":\"sha1:3MTDH2GOF77Z6VGJKMBLQH7WBGA5D6WA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-35/CC-MAIN-2019-35_segments_1566027315750.62_warc_CC-MAIN-20190821022901-20190821044901-00249.warc.gz\"}"}