Beispiel #1
0
def line_search(dt, p, gamma, M, y, C):
    """
    returns dt
    """
    # see class notes
    dt = 2.0 * dt

    # initial energy and initial gamma do not change throughout
    E = _energy(gamma, M, y, C)

    while True:
        gamma_new = project(gamma - dt * p, y, C)

        E_new = _energy(gamma_new, M, y, C)

        if E >= E_new + .001 * (gamma - gamma_new).T.dot(gamma - gamma_new):
            return dt
        else:
            dt = dt / 2  # take a smaller timestep and try again
Beispiel #2
0
def kernel_classify(M, y, C, tol=.001):
    """
    input M, y, C
    returns γ
   
    NOTE
    for hard margin just pass C = numpy.inf
    gradient descent w/ log functions
    """

    # please get to the bottom of the dimensionality issues from before
    #if len(y.shape) == 1:
    #    numpy.expand_dims(y, axis=0)

    gamma = numpy.zeros((M.shape[0], 1))

    dt, itmax = .001, 20000
    it = 1

    while True:

        it += 1

        p = gradient(gamma, M, y)
        dt = line_search(dt, p, gamma, M, y, C)

        gamma_new = project(gamma - dt * p, y, C)

        q = (gamma_new - gamma) / dt

        if (norm(q) < tol):
            break
        elif (it > itmax):
            break
        else:
            gamma = gamma_new

    return gamma_new
Beispiel #3
0
argv = sys.argv
arg = sys.argv[1]

if arg == 'project':
    input_file = argv[2]
    k = int(argv[3])

    A = _parser(input_file, identifier='A')
    b = _parser(input_file, identifier='b')

    logging.debug(f"Projecting with k: {k}")
    logging.debug(f"A : {A}")
    logging.debug(f"b : {b}")
    n = len(A[0])
    A, b = project(A, b, k, n)

    logging.debug(f"Result from project: ")
    logging.debug(f"\t A: {A}")
    logging.debug(f"\t b : {b}")
    output_file = argv[4]

    _write(output_file, **{'A': A, 'b': b})

elif arg == 'image':
    input_file_1 = argv[2]
    input_file_2 = argv[3]
    output_file = argv[4]

    A = _parser(input_file_1, identifier='A')
    b = _parser(input_file_1, identifier='b')