def main(readcsv=read_csv, method='defaultDense'): infile = "./data/batch/lbfgs.csv" # Read the data, let's have 10 independent variables data = readcsv(infile, range(10)) dep_data = readcsv(infile, range(10,11)) nVectors = data.shape[0] # configure a MSE object mse_algo = d4p.optimization_solver_mse(nVectors) mse_algo.setup(data, dep_data) # configure an LBFGS object sls = np.array([[1.0e-4]], dtype=np.double) niters = 1000 lbfgs_algo = d4p.optimization_solver_lbfgs(mse_algo, stepLengthSequence=sls, nIterations=niters) # finally do the computation inp = np.array([[100]]*11, dtype=np.double) res = lbfgs_algo.compute(inp) # The LBFGS result provides minimum and nIterations assert res.minimum.shape == inp.shape and res.nIterations[0][0] <= niters return res
def main(readcsv=read_csv, method='defaultDense'): nFeatures = 6 nClasses = 5 nIterations = 1000 stepLength = 1.0e-4 infile = "./data/batch/logreg_train.csv" # Read the data data = readcsv(infile, range(nFeatures)) dep_data = readcsv(infile, range(nFeatures, nFeatures + 1)) nVectors = data.shape[0] # configure a function func = d4p.optimization_solver_cross_entropy_loss(nClasses, nVectors, interceptFlag=True) func.setup(data, dep_data, None) # configure a algorithm stepLengthSequence = np.array([[stepLength]], dtype=np.double) alg = d4p.optimization_solver_lbfgs(func, stepLengthSequence=stepLengthSequence, nIterations=nIterations) # do the computation nParameters = nClasses * (nFeatures + 1) initialPoint = np.full((nParameters, 1), 0.001, dtype=np.double) res = alg.compute(initialPoint) # result provides minimum and nIterations assert res.minimum.shape == (nParameters, 1) assert res.nIterations[0][0] <= nIterations return res
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