Esempio n. 1
0
def main(readcsv=read_csv, method='defaultDense'):
    infile = "./data/batch/mse.csv"
    # Read the data, let's have 3 independent variables
    data = readcsv(infile, range(3))
    dep_data = readcsv(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)

    # configure an AdaGrad object
    lr = np.array([[1.0]], dtype=np.double)
    niters = 1000
    sgd_algo = d4p.optimization_solver_adagrad(mse_algo,
                                               learningRate=lr,
                                               accuracyThreshold=0.0000001,
                                               nIterations=niters,
                                               batchSize=1)

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

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

    return res
Esempio n. 2
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