示例#1
0
def likelihood_and_dlikelihood_batch(w, instances, model):
    '''
    Batchly calculate the likelihood and gradient of the likelihood

    Parameters
    ----------
    w : float vector
        The parameters
    instances : list of instance
        A list of instance to train the model
    model : crfmodel
        The model

    Returns
    -------
    f, grad : tuple
        A tuple with two objects. F is the likelihood and grad is the
        gradient for likelihood
    '''
    N = len(instances)

    f, grad = 0., zeros(w.shape[0], dtype=float)
    for index, instance in enumerate(instances):
        trace("Calculate batch", index, N)
        delta_f, delta_grad = _likelihood_and_dlikelihood_batch(
            w, instance, model)
        f += delta_f
        grad += delta_grad

    return -(f - ((w**2).sum() / (2 * DELTA))), -(grad - (w / DELTA))
示例#2
0
def likelihood_and_dlikelihood_batch(w, instances, model):
    '''
    Batchly calculate the likelihood and gradient of the likelihood

    Parameters
    ----------
    w : float vector
        The parameters
    instances : list of instance
        A list of instance to train the model
    model : crfmodel
        The model

    Returns
    -------
    f, grad : tuple
        A tuple with two objects. F is the likelihood and grad is the
        gradient for likelihood
    '''
    N = len(instances)

    f, grad = 0., zeros(w.shape[0], dtype=float)
    for index, instance in enumerate(instances):
        trace("Calculate batch", index, N)
        delta_f, delta_grad = _likelihood_and_dlikelihood_batch(w, instance, model)
        f += delta_f
        grad += delta_grad

    return -(f-((w**2).sum()/(2*DELTA))), -(grad-(w/DELTA))
示例#3
0
def dlikelihood(w, instances, model):
    '''
    Calculate the gradient on the overall data set.

    - param[w]  the
    '''
    N = len(instances)

    grad = zeros(w.shape[0], dtype=float)
    for index, instance in enumerate(instances):
        trace("Calculate gradient", index, N)
        grad += _dlikelihood(w, instance, model)

    return -(grad - w / DELTA)
示例#4
0
def dlikelihood(w, instances, model):
    '''
    Calculate the gradient on the overall data set.

    - param[w]  the
    '''
    N = len(instances)

    grad = zeros(w.shape[0], dtype=float)
    for index, instance in enumerate(instances):
        trace("Calculate gradient", index, N)
        grad += _dlikelihood(w, instance, model)

    return -(grad - w / DELTA)
示例#5
0
def likelihood(w, instances, model):
    '''
    Calculate the likelihood of the whole data set.

    - param[in] w           The weight vector {arraylike}
    - param[in] instances   The data set {useless.Instance}
    - param[in] model       The model
    '''
    N = len(instances)
    ret = 0.

    for index, instance in enumerate(instances):
        trace("Calculate likelihood", index, N)
        ret += _likelihood(w, instance, model)

    return -(ret - ((w**2).sum() / (2 * DELTA**2)))
示例#6
0
def likelihood(w, instances, model):
    '''
    Calculate the likelihood of the whole data set.

    - param[in] w           The weight vector {arraylike}
    - param[in] instances   The data set {useless.Instance}
    - param[in] model       The model
    '''
    N = len(instances)
    ret = 0.

    for index, instance in enumerate(instances):
        trace("Calculate likelihood", index, N)
        ret += _likelihood(w, instance, model)

    return -(ret - ((w ** 2).sum() / (2 * DELTA **2)))