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
Beispiel #2
0
def main():
    infile = "./data/batch/mse.csv"
    # Read the data, let's have 3 independent variables
    data = read_csv(infile, range(3))
    dep_data = read_csv(infile, range(3, 4))
    nVectors = data.shape[0]

    # configure a MSE object
    mse_algo = d4p.optimization_solver_mse(nVectors)
    mse_algo.setup(data, dep_data, None)

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

    # finally do the computation
    inp = np.array([[8], [2], [1], [4]], 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
Beispiel #3
0
    def train(self, train_data, train_labels):
        dtype = (np.float64 if self.dtype == "double" else np.float32)
        optSolver = None
        #create a solver
        if self.optSolverParam['solverName'] == 'sgd':
            lrs = np.array([[self.optSolverParam['solverLearningRate']]], dtype=dtype)
            batchSize_ = int(self.optSolverParam['solverBatchSize'])
            method = self.optSolverParam["solverMethod"]
            if method == "defaultDense":
                batchSize_ = 1
            optSolver = d4p.optimization_solver_sgd(function = None, learningRateSequence = lrs,
                                                    method = method,
                                                    accuracyThreshold = dtype(self.optSolverParam['solverAccuracyThreshold']),
                                                    nIterations = int(self.optSolverParam['solverMaxIterations']),
                                                    batchSize = batchSize_
                                                    )
        if self.optSolverParam['solverName'] == 'lbfgs':
            sls = np.array([[self.optSolverParam['solverStepLength']]], dtype=dtype)
            optSolver = d4p.optimization_solver_lbfgs(function = None,
                                                      stepLengthSequence=sls,
                                                      accuracyThreshold = dtype(self.optSolverParam['solverAccuracyThreshold']),
                                                      nIterations = int(self.optSolverParam['solverMaxIterations']),
                                                      batchSize = int(self.optSolverParam['solverBatchSize']),
                                                      correctionPairBatchSize = int(self.optSolverParam['solverCorrectionPairBatchSize']),
                                                      L = int(self.optSolverParam['solverL'])
                                                      )
        if self.optSolverParam['solverName'] == 'adagrad':
            lr = np.array([[self.optSolverParam['solverLearningRate']]], dtype=dtype)
            optSolver = d4p.optimization_solver_adagrad(function = None,
                                                        learningRate=lr,
                                                        accuracyThreshold = dtype(self.optSolverParam['solverAccuracyThreshold']),
                                                        nIterations = int(self.optSolverParam['solverMaxIterations']),
                                                        batchSize = int(self.optSolverParam['solverBatchSize'])
                                                        )

        train_alg = d4p.logistic_regression_training(nClasses      = self.nClasses,
                                                     penaltyL1     = self.penaltyL1,
                                                     penaltyL2     = self.penaltyL2,
                                                     interceptFlag = self.interceptFlag,
                                                     fptype        = self.dtype,
                                                     optimizationSolver = optSolver
                                                     )
        self.trainingResult = train_alg.compute(train_data, train_labels)

        return self