Ejemplo n.º 1
0
def _daal4py_logistic_loss_extra_args(nClasses_unused,
                                      beta,
                                      X,
                                      y,
                                      l1=0.0,
                                      l2=0.0,
                                      fit_intercept=True,
                                      value=True,
                                      gradient=True,
                                      hessian=False):
    X = make2d(X)
    nSamples, nFeatures = X.shape

    y = make2d(y)
    beta = make2d(beta)
    n = X.shape[0]

    results_to_compute = _resultsToCompute_string(value=value,
                                                  gradient=gradient,
                                                  hessian=hessian)

    objective_function_algorithm_instance = daal4py.optimization_solver_logistic_loss(
        numberOfTerms=n,
        fptype=getFPType(X),
        method='defaultDense',
        interceptFlag=fit_intercept,
        penaltyL1=l1 / n,
        penaltyL2=l2 / n,
        resultsToCompute=results_to_compute)
    objective_function_algorithm_instance.setup(X, y, beta)

    return (objective_function_algorithm_instance, X, y, n)
def main(readcsv=read_csv, method='defaultDense'):
    infile = "./data/batch/custom.csv"
    # Read the data, let's have 4 independent variables
    data = readcsv(infile, range(4))
    dep_data = readcsv(infile, range(4, 5))
    nVectors = data.shape[0]

    # configure a logistic loss object
    ll_algo = d4p.optimization_solver_logistic_loss(nVectors, interceptFlag=True)
    ll_algo.setup(data, dep_data)

    # configure a SGD object
    lrs = np.array([[0.01]], dtype=np.double)
    niters = 1000
    sgd_algo = d4p.optimization_solver_sgd(ll_algo,
                                           learningRateSequence=lrs,
                                           accuracyThreshold=0.02,
                                           nIterations=niters)

    # finally do the computation
    inp = np.array([[1], [1], [1], [1], [1]], dtype=np.double)
    res = sgd_algo.compute(inp)

    # The SGD result provides minimum and nIterations
    assert res.minimum.shape == inp.shape and res.nIterations[0][0] <= niters

    return res
Ejemplo n.º 3
0
def main(readcsv=read_csv, method='defaultDense'):
    infile = "./data/batch/XM.csv"
    # Read the data, let's have 3 independent variables
    data = readcsv(infile, range(1))
    dep_data = readcsv(infile, range(1, 2))
    nVectors = data.shape[0]

    # configure a Logistic Loss object
    logloss_algo = d4p.optimization_solver_logistic_loss(
        numberOfTerms=nVectors,
        penaltyL1=0.3,
        penaltyL2=0,
        interceptFlag=True,
        resultsToCompute='gradient')
    logloss_algo.setup(data, dep_data)

    # configure an Saga object
    lr = np.array([[0.01]], dtype=np.double)
    niters = 100000
    saga_algo = d4p.optimization_solver_saga(nIterations=niters,
                                             accuracyThreshold=1e-5,
                                             batchSize=1,
                                             function=logloss_algo,
                                             learningRateSequence=lr,
                                             optionalResultRequired=True)

    # finally do the computation
    inp = np.zeros((2, 1), dtype=np.double)
    res = saga_algo.compute(inp, None)

    # The Saga result provides minimum and nIterations
    assert res.minimum.shape == inp.shape and res.nIterations[0][0] <= niters
    assert np.allclose(res.minimum, [[-0.17663868], [0.35893627]])

    return res