Пример #1
0
    def __init__(self,
                 n_components=2,
                 algorithm="arpack",
                 n_iter=5,
                 random_state=None,
                 tol=0.,
                 verbose=False,
                 backend='auto'):
        self.algorithm = algorithm
        self.n_components = n_components
        self.n_iter = n_iter
        self.random_state = random_state
        self.tol = tol

        import os
        _backend = os.environ.get('H2O4GPU_BACKEND', None)
        if _backend is not None:
            backend = _backend

        # Fall back to Sklearn
        # Can remove if fully implement sklearn functionality
        self.do_sklearn = False
        if backend == 'auto':
            params_string = ['algorithm', 'n_iter', 'random_state', 'tol']
            params = [algorithm, n_iter, random_state, tol]
            params_default = ['arpack', 5, None, 0.]

            i = 0
            for param in params:
                if param != params_default[i]:
                    self.do_sklearn = True
                    if verbose:
                        print("WARNING:"
                              " The sklearn parameter " + params_string[i] +
                              " has been changed from default to " +
                              str(param) + ". Will run Sklearn TruncatedSVD.")
                    self.do_sklearn = True
                i = i + 1
        elif backend == 'sklearn':
            self.do_sklearn = True
        elif backend == 'h2o4gpu':
            self.do_sklearn = False
        if self.do_sklearn:
            self.backend = 'sklearn'
        else:
            self.backend = 'h2o4gpu'

        from h2o4gpu.decomposition.truncated_svd import TruncatedSVDSklearn
        self.model_sklearn = TruncatedSVDSklearn(
            n_components=n_components,
            algorithm=algorithm,
            n_iter=n_iter,
            random_state=random_state,
            tol=tol)
        self.model_h2o4gpu = TruncatedSVDH2O(n_components=n_components)

        if self.do_sklearn:
            self.model = self.model_sklearn
        else:
            self.model = self.model_h2o4gpu
Пример #2
0
    def __init__(self,
                 n_components=2,
                 algorithm="power",
                 n_iter=100,
                 random_state=None,
                 tol=1E-5,
                 verbose=False,
                 backend='auto',
                 n_gpus=1,
                 gpu_id=0):
        if isinstance(algorithm, list):
            self.algorithm = algorithm[0]
        else:
            self.algorithm = algorithm
        self.n_components = n_components
        if isinstance(n_iter, list):
            self.n_iter = n_iter[0]
        else:
            self.n_iter = n_iter
        if random_state is not None:
            self.random_state = random_state
        else:
            self.random_state = np.random.randint(0, 2**31 - 1)
        if isinstance(tol, list):
            self.tol = tol[0]
        else:
            self.tol = tol
        self.verbose = 1 if verbose else 0
        self.n_gpus = n_gpus
        self.gpu_id = gpu_id

        import os
        _backend = os.environ.get('H2O4GPU_BACKEND', None)
        if _backend is not None:
            backend = _backend

        # Fall back to Sklearn
        # Can remove if fully implement sklearn functionality
        self.do_sklearn = False
        self.do_daal = False

        sklearn_algorithm = "arpack"  # Default scikit
        sklearn_n_iter = 5
        sklearn_tol = 1E-5

        if n_gpus == 0:
            # we don't have CPU back-end for SVD yet.
            backend = 'sklearn'
        else:
            backend = 'h2o4gpu'

        if backend in ['auto', 'sklearn']:
            self.do_sklearn = True
            self.backend = 'sklearn'
            params_string = ['algorithm']
            params = [self.algorithm]
            params_gpu = [['cusolver', 'power']]

            i = 0
            for param in params:
                if param not in params_gpu[i]:
                    self.do_sklearn = True
                    if verbose:
                        print("WARNING:"
                              " The parameter " + params_string[i] + "is " +
                              str(param) + " and not supported by GPU." +
                              "Will run Sklearn TruncatedSVD.")
                    self.do_sklearn = True
                i = i + 1

            if isinstance(algorithm, list):
                sklearn_algorithm = algorithm[1]
            if isinstance(n_iter, list):
                sklearn_n_iter = n_iter[1]
            if isinstance(tol, list):
                sklearn_tol = tol[1]

        elif backend == 'h2o4gpu':
            self.backend = 'h2o4gpu'

        elif backend == 'daal':
            from h2o4gpu import DAAL_SUPPORTED
            if DAAL_SUPPORTED:
                from h2o4gpu.solvers.daal_solver.svd import SVD
                self.do_daal = True
                self.backend = 'daal'

                self.model_daal = SVD(n_components=self.n_components,
                                      verbose=self.verbose)
            else:
                import platform
                print("WARNING:"
                      "DAAL is supported only for x86_64, "
                      "architecture detected {}. Sklearn model"
                      "used instead".format(platform.architecture()))
                self.do_sklearn = True
                self.backend = 'sklearn'

        from h2o4gpu.decomposition.truncated_svd import TruncatedSVDSklearn
        self.model_sklearn = TruncatedSVDSklearn(
            n_components=self.n_components,
            algorithm=sklearn_algorithm,
            n_iter=sklearn_n_iter,
            random_state=self.random_state,
            tol=sklearn_tol)

        self.model_h2o4gpu = TruncatedSVDH2O(n_components=self.n_components,
                                             algorithm=self.algorithm,
                                             n_iter=self.n_iter,
                                             random_state=self.random_state,
                                             tol=self.tol,
                                             verbose=self.verbose,
                                             gpu_id=self.gpu_id)

        # select final model type
        if self.do_sklearn:
            self.model = self.model_sklearn
        elif self.do_daal:
            self.model = self.model_daal
        else:
            self.model = self.model_h2o4gpu
Пример #3
0
    def __init__(self,
                 n_components=2,
                 algorithm="power",
                 n_iter=100,
                 random_state=None,
                 tol=1E-5,
                 verbose=False,
                 backend='auto',
                 n_gpus=1,
                 gpu_id=0):
        if isinstance(algorithm, list):
            self.algorithm = algorithm[0]
        else:
            self.algorithm = algorithm
        self.n_components = n_components
        if isinstance(n_iter, list):
            self.n_iter = n_iter[0]
        else:
            self.n_iter = n_iter
        if random_state is not None:
            self.random_state = random_state
        else:
            self.random_state = np.random.randint(0, 2 ** 32 - 1)
        if isinstance(tol, list):
            self.tol = tol[0]
        else:
            self.tol = tol
        self.verbose = 1 if verbose else 0
        self.n_gpus = n_gpus
        self.gpu_id = gpu_id

        import os
        _backend = os.environ.get('H2O4GPU_BACKEND', None)
        if _backend is not None:
            backend = _backend

        # Fall back to Sklearn
        # Can remove if fully implement sklearn functionality
        self.do_sklearn = False
        if backend == 'auto':
            params_string = ['algorithm']
            params = [self.algorithm]
            params_gpu = [['cusolver', 'power']]

            i = 0
            for param in params:
                if param not in params_gpu[i]:
                    self.do_sklearn = True
                    if verbose:
                        print("WARNING:"
                              " The parameter " + params_string[i]
                              + "is "
                              + str(param)
                              + " and not supported by GPU."
                              + "Will run Sklearn TruncatedSVD.")
                    self.do_sklearn = True
                i = i + 1
        elif backend == 'sklearn':
            self.do_sklearn = True
        elif backend == 'h2o4gpu':
            self.do_sklearn = False
        if n_gpus == 0:
            # we don't have CPU back-end for SVD yet.
            self.do_sklearn = True
        if n_gpus != 0:
            # sklearn can't do GPUs
            self.do_sklearn = False

        sklearn_algorithm = "arpack"  # Default scikit
        sklearn_n_iter = 5
        sklearn_tol = 1E-5
        if self.do_sklearn:
            self.backend = 'sklearn'
            if isinstance(algorithm, list):
                sklearn_algorithm = algorithm[1]
            if isinstance(n_iter, list):
                sklearn_n_iter = n_iter[1]
            if isinstance(tol, list):
                sklearn_tol = tol[1]
        else:
            self.backend = 'h2o4gpu'

        from h2o4gpu.decomposition.truncated_svd import TruncatedSVDSklearn
        self.model_sklearn = TruncatedSVDSklearn(
            n_components=self.n_components,
            algorithm=sklearn_algorithm,
            n_iter=sklearn_n_iter,
            random_state=self.random_state,
            tol=sklearn_tol)
        self.model_h2o4gpu = TruncatedSVDH2O(n_components=self.n_components,
                                             algorithm=self.algorithm,
                                             n_iter=self.n_iter,
                                             random_state=self.random_state,
                                             tol=self.tol,
                                             verbose=self.verbose,
                                             gpu_id=self.gpu_id)

        # select final model type
        if self.do_sklearn:
            self.model = self.model_sklearn
        else:
            self.model = self.model_h2o4gpu