Пример #1
0
def objective_soft_max_old(X, W, C):
    l = C.shape[0]
    m = X.shape[1]
    objective_value = 0
    eta = find_eta(X, W)
    XT_W = X.T @ W - eta
    weighted_sum = sum(exp(XT_W), axis=1)
    wighted_sum_matrix = diag(1 / weighted_sum)
    for k in range(l):
        objective_value = objective_value + C[k, :].T @ log(
            wighted_sum_matrix @ exp(XT_W[:, k]))
    return -objective_value / m
Пример #2
0
def objective_soft_max_gradient_W(X, W, C, WT_X=None):
    m = C.shape[1]
    if WT_X is None:
        XT_W = X.T @ W
    else:
        XT_W = WT_X.T
    eta = find_eta(X, W, XT_W)
    XT_W -= eta
    weighted_sum = sum(exp(XT_W), axis=1)
    gradient = (1 / m) * X @ (divide(
        exp(XT_W), weighted_sum.reshape(weighted_sum.shape[0], 1)) - C.T)
    return gradient
Пример #3
0
def objective_soft_max_gradient_W_old_but_gold(X, W, C):
    l = C.shape[0]
    n = X.shape[0]
    m = X.shape[1]
    eta = find_eta(X, W)
    XT_W = X.T @ W - eta
    weighted_sum = sum(exp(XT_W), axis=1)
    wighted_sum_matrix = np.diag(1 / weighted_sum)
    grad = np.zeros((n, l))
    for p in range(l):
        grad[:, p] = (1 / m) * X @ (wighted_sum_matrix @ exp(XT_W[:, p]) -
                                    C[p, :])
    return grad
Пример #4
0
def objective_soft_max_gradient_W2(X, W, C):
    l = C.shape[0]
    n = X.shape[0]
    m = X.shape[1]

    eta = find_eta(X, W)
    XT_W = X.T @ W - eta
    weighted_sum = sum(exp(XT_W), axis=1)
    grad = np.zeros((n, l))
    for p in range(l):
        grad[:, p] = (1 / m) * X @ (divide(exp(XT_W[:, p]), weighted_sum) -
                                    C[p, :])
    return grad
Пример #5
0
def objective_soft_max_Wj(X, W, C):
    """
    :param X:
    :param W: shape #features x #labels
    :param C: shape #labels x #samples
    :return:
    """
    l = C.shape[0]
    m = X.shape[1]
    objective_value = 0
    eta = find_eta(X, W)
    XT_W = X.T @ W - eta
    weighted_sum = sum(exp(XT_W), axis=1)
    for k in range(l):
        objective_value = objective_value + C[k, :].T @ log(
            divide(exp(XT_W[:, k]), weighted_sum))
    return -objective_value / m
Пример #6
0
def objective_soft_max(X, W, C, WT_X=None):
    """
    :param X:
    :param W: shape #features x #labels
    :param C: shape #labels x #samples
    :return:
    """
    l, m = C.shape
    objective_value = 0
    if WT_X is None:
        XT_W = X.T @ W
    else:
        XT_W = WT_X.T
    eta = find_eta(X, W, XT_W)
    XT_W -= eta
    weighted_sum = sum(exp(XT_W), axis=1)
    for k in range(l):
        objective_value = objective_value + C[k, :].T @ log(
            divide(exp(XT_W[:, k]), weighted_sum))
    return -objective_value / m
Пример #7
0
def objective_soft_max_gradient_Wp(X, W, C, p):
    m = X.shape[1]
    eta = find_eta(X, W)
    XT_W = X.T @ W - eta
    weighted_sum = sum(exp(XT_W), axis=1)
    return (1 / m) * X @ (divide(exp(XT_W[:, p]), weighted_sum) - C[p, :])
Пример #8
0
# Script 23 on page 55
from numpy.core._multiarray_umath import exp

nums = [10, 20, 30, 40, 50]
np_exp = exp(nums)
print(np_exp)
Пример #9
0
def objective_soft_max_gradient_X(X, W, C, WT_X=None):
    if WT_X is None:
        WT_X = W.T @ X
    m = C.shape[1]
    return (1 / m) * W @ (divide(exp(WT_X), sum(exp(WT_X), axis=0)) - C)