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'): 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
def _daal4py_fit_enet(self, X, y_, check_input): # appropriate checks _daal4py_check(self, X, y_, check_input) X = make2d(X) y = make2d(y_) _fptype = getFPType(X) # only for dual_gap computation, it is not required for Intel(R) oneAPI # Data Analytics Library self._X = X self._y = y penalty_L1 = np.asarray(self.alpha * self.l1_ratio, dtype=X.dtype) penalty_L2 = np.asarray(self.alpha * (1.0 - self.l1_ratio), dtype=X.dtype) if (penalty_L1.size != 1 or penalty_L2.size != 1): raise ValueError("alpha or l1_ratio length is wrong") penalty_L1 = penalty_L1.reshape((1, -1)) penalty_L2 = penalty_L2.reshape((1, -1)) #normalizing and centering X_offset = np.zeros(X.shape[1], dtype=X.dtype) X_scale = np.ones(X.shape[1], dtype=X.dtype) if y.ndim == 1: y_offset = X.dtype.type(0) else: y_offset = np.zeros(y.shape[1], dtype=X.dtype) if self.fit_intercept: X_offset = np.average(X, axis=0) if self.normalize: if self.copy_X: X = np.copy(X) - X_offset else: X -= X_offset X, X_scale = normalize(X, axis=0, copy=False, return_norm=True) y_offset = np.average(y, axis=0) y = y - y_offset # only for compliance with Sklearn if isinstance(self.precompute, np.ndarray) and ( self.fit_intercept and not np.allclose(X_offset, np.zeros(X.shape[1])) or self.normalize and not np.allclose(X_scale, np.ones(X.shape[1]))): warnings.warn( "Gram matrix was provided but X was centered" " to fit intercept, " "or X was normalized : recomputing Gram matrix.", UserWarning) mse_alg = daal4py.optimization_solver_mse(numberOfTerms=X.shape[0], fptype=_fptype, method='defaultDense') mse_alg.setup(X, y, None) cd_solver = daal4py.optimization_solver_coordinate_descent( function=mse_alg, fptype=_fptype, method='defaultDense', selection=self.selection, seed=0 if (self.random_state is None) else self.random_state, nIterations=self.max_iter, positive=self.positive, accuracyThreshold=self.tol) # set warm_start if self.warm_start and hasattr(self, "coef_") and \ isinstance(self.coef_, np.ndarray): n_rows = y.shape[1] n_cols = X.shape[1] + 1 inputArgument = np.zeros((n_rows, n_cols), dtype=_fptype) for i in range(n_rows): inputArgument[i][0] = self.intercept_ if ( n_rows == 1) else self.intercept_[i] inputArgument[i][1:] = self.coef_[:].copy( order='C') if (n_rows == 1) else self.coef_[i, :].copy( order='C') cd_solver.setup(inputArgument) elastic_net_alg = daal4py.elastic_net_training( fptype=_fptype, method='defaultDense', interceptFlag=(self.fit_intercept is True), dataUseInComputation='doUse' if ((self.copy_X is False) or (self.fit_intercept and self.normalize and self.copy_X)) else 'doNotUse', penaltyL1=penalty_L1, penaltyL2=penalty_L2, optimizationSolver=cd_solver) try: if isinstance(self.precompute, np.ndarray): elastic_net_res = elastic_net_alg.compute( data=X, dependentVariables=y, gramMatrix=self.precompute) else: elastic_net_res = elastic_net_alg.compute(data=X, dependentVariables=y) except RuntimeError: return None # set coef_ and intersept_ results elastic_net_model = elastic_net_res.model self.daal_model_ = elastic_net_model # update coefficients if normalizing and centering if self.fit_intercept and self.normalize: elastic_net_model.Beta[:, 1:] = elastic_net_model.Beta[:, 1:] / X_scale elastic_net_model.Beta[:, 0] = ( y_offset - np.dot(X_offset, elastic_net_model.Beta[:, 1:].T)).T coefs = elastic_net_model.Beta self.intercept_ = coefs[:, 0].copy(order='C') self.coef_ = coefs[:, 1:].copy(order='C') # only for compliance with Sklearn if y.shape[1] == 1: self.coef_ = np.ravel(self.coef_) self.intercept_ = np.ravel(self.intercept_) if self.intercept_.shape[0] == 1: self.intercept_ = self.intercept_[0] # set n_iter_ n_iter = cd_solver.__get_result__().nIterations[0][0] if y.shape[1] == 1: self.n_iter_ = n_iter else: self.n_iter_ = np.full(y.shape[1], n_iter) # only for compliance with Sklearn if (self.max_iter == n_iter + 1): warnings.warn( "Objective did not converge. You might want to " "increase the number of iterations.", ConvergenceWarning) return self
def _daal4py_fit_enet(self, X, y_, check_input): #appropriate checks _daal4py_check(self, X, y_, check_input) X = make2d(X) y = make2d(y_) _fptype = getFPType(X) penalty_L1 = np.asarray(self.alpha * self.l1_ratio, dtype=X.dtype) penalty_L2 = np.asarray(self.alpha * (1.0 - self.l1_ratio), dtype=X.dtype) if (penalty_L1.size != 1 or penalty_L2.size != 1): raise ValueError("alpha or l1_ratio length is wrong") penalty_L1 = penalty_L1.reshape((1, -1)) penalty_L2 = penalty_L2.reshape((1, -1)) mse_alg = daal4py.optimization_solver_mse(numberOfTerms=X.shape[0], fptype=_fptype, method='defaultDense') mse_alg.setup(X, y, None) cd_solver = daal4py.optimization_solver_coordinate_descent( function=mse_alg, fptype=_fptype, method='defaultDense', selection=self.selection, seed=0 if (self.random_state == None) else self.random_state, nIterations=self.max_iter, positive=self.positive, accuracyThreshold=self.tol) #set warm_start if (self.warm_start and hasattr(self, "coef_") and isinstance(self.coef_, np.ndarray)): n_rows = y.shape[1] n_cols = X.shape[1] + 1 inputArgument = np.zeros((n_rows, n_cols), dtype=_fptype) for i in range(n_rows): inputArgument[i][0] = self.intercept_ if ( n_rows == 1) else self.intercept_[i] inputArgument[i][1:] = self.coef_[:].copy( order='C') if (n_rows == 1) else self.coef_[i, :].copy( order='C') cd_solver.setup(inputArgument) elastic_net_alg = daal4py.elastic_net_training( fptype=_fptype, method='defaultDense', interceptFlag=(self.fit_intercept is True), dataUseInComputation='doUse' if (self.copy_X == False) else 'doNotUse', penaltyL1=penalty_L1, penaltyL2=penalty_L2, optimizationSolver=cd_solver) try: if isinstance(self.precompute, np.ndarray): elastic_net_res = elastic_net_alg.compute( data=X, dependentVariables=y, gramMatrix=self.precompute) else: elastic_net_res = elastic_net_alg.compute(data=X, dependentVariables=y) except RuntimeError: return None #set coef_ and intersept_ results elastic_net_model = elastic_net_res.model self.daal_model_ = elastic_net_model coefs = elastic_net_model.Beta self.intercept_ = coefs[:, 0].copy(order='C') self.coef_ = coefs[:, 1:].copy(order='C') #only for compliance with Sklearn if y.shape[1] == 1: self.coef_ = np.ravel(self.coef_) self.intercept_ = np.ravel(self.intercept_) if self.intercept_.shape[0] == 1: self.intercept_ = self.intercept_[0] #set n_iter_ n_iter = cd_solver.__get_result__().nIterations[0][0] if y.shape[1] == 1: self.n_iter_ = n_iter else: self.n_iter_ = np.full(y.shape[1], n_iter) #only for compliance with Sklearn if (self.max_iter == n_iter + 1): warnings.warn( "Objective did not converge. You might want to " "increase the number of iterations.", ConvergenceWarning) #only for dual_gap computation, it is not required for DAAL self._X = X self._y = y return self