本文整理汇总了Python中sklearn.externals.joblib.cpu_count函数的典型用法代码示例。如果您正苦于以下问题:Python cpu_count函数的具体用法?Python cpu_count怎么用?Python cpu_count使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cpu_count函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_n_jobs
def check_n_jobs(n_jobs):
"""Check and adjust the number of CPUs that can work in parallel.
Parameters
----------
n_jobs : int,
Number of parallel workers, specified according to joblib's conventions:
If 0 is provided, all CPUs are used.
A negative number indicates that all the CPUs except (|n_jobs| - 1) ones
will be used.
Returns
-------
n_jobs : int,
Actual number of CPUs that will be used according to their availability.
"""
if n_jobs == 0: # invalid according to joblib's conventions
raise ValueError(
"'n_jobs == 0' is not a valid choice. "
"Please provide a positive number of CPUs, or -1 "
"for all CPUs, or a negative number (-i) for "
"'all but (i-1)' CPUs (joblib conventions)."
)
elif n_jobs < 0:
n_jobs = max(1, joblib.cpu_count() + n_jobs + 1)
else:
n_jobs = min(n_jobs, joblib.cpu_count())
return n_jobs
示例2: _parallel_learning
def _parallel_learning(self, X, Y, w):
n_samples = len(X)
objective, positive_slacks = 0, 0
verbose = max(0, self.verbose - 3)
if self.batch_size is not None:
raise ValueError("If n_jobs != 1, batch_size needs to" "be None")
# generate batches of size n_jobs
# to speed up inference
if self.n_jobs == -1:
n_jobs = cpu_count()
else:
n_jobs = self.n_jobs
n_batches = int(np.ceil(float(len(X)) / n_jobs))
slices = gen_even_slices(n_samples, n_batches)
for batch in slices:
X_b = X[batch]
Y_b = Y[batch]
candidate_constraints = Parallel(n_jobs=self.n_jobs, verbose=verbose)(
delayed(find_constraint)(self.model, x, y, w) for x, y in zip(X_b, Y_b)
)
dpsi = np.zeros(self.model.size_psi)
for x, y, constraint in zip(X_b, Y_b, candidate_constraints):
y_hat, delta_psi, slack, loss = constraint
if slack > 0:
objective += slack
dpsi += delta_psi
positive_slacks += 1
w = self._solve_subgradient(dpsi, n_samples, w)
return objective, positive_slacks, w
示例3: _get_n_jobs
def _get_n_jobs(n_jobs):
"""Get number of jobs for the computation.
See sklearn/utils/__init__.py for more information.
This function reimplements the logic of joblib to determine the actual
number of jobs depending on the cpu count. If -1 all CPUs are used.
If 1 is given, no parallel computing code is used at all, which is useful
for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used.
Thus for n_jobs = -2, all CPUs but one are used.
Parameters
----------
n_jobs : int
Number of jobs stated in joblib convention.
Returns
-------
n_jobs : int
The actual number of jobs as positive integer.
Examples
--------
>>> from sklearn.utils import _get_n_jobs
>>> _get_n_jobs(4)
4
>>> jobs = _get_n_jobs(-2)
>>> assert jobs == max(cpu_count() - 1, 1)
>>> _get_n_jobs(0)
Traceback (most recent call last):
...
ValueError: Parameter n_jobs == 0 has no meaning.
"""
if n_jobs < 0:
return max(cpu_count() + 1 + n_jobs, 1)
elif n_jobs == 0:
raise ValueError('Parameter n_jobs == 0 has no meaning.')
else:
return n_jobs
示例4: _fit_multiclass_task
def _fit_multiclass_task(self, X, y, sample_weight, params):
if params['init_model'] is not None:
max_digits = len(str(len(self._classes)))
init_model_filenames = ['{}.{}'.format(params['init_model'],
str(i + 1).zfill(max_digits)) for i in range(self._n_classes)]
ovr_list = [None] * self._n_classes
for i, cls_num in enumerate(self._classes):
if params['init_model'] is not None:
params['init_model'] = init_model_filenames[i]
self._classes_map[i] = cls_num
ovr_list[i] = (y == cls_num).astype(int)
self._estimators[i] = RGFExecuter(**params)
n_jobs = self.n_jobs if self.n_jobs > 0 else cpu_count() + self.n_jobs + 1
substantial_n_jobs = max(n_jobs, self.n_classes_)
if substantial_n_jobs < n_jobs and self.verbose:
print('n_jobs = {0}, but RGFClassifier uses {1} CPUs because '
'classes_ is {2}'.format(n_jobs, substantial_n_jobs,
self.n_classes_))
self._estimators = Parallel(n_jobs=self.n_jobs)(delayed(utils.fit_ovr_binary)(self._estimators[i],
X,
ovr_list[i],
sample_weight)
for i in range(self._n_classes))
示例5: get_split_scores
def get_split_scores(factory,thresholds,formula,
metric = None,#p.e. usability entropy
use_joblib = False,
joblib_backend = 'threading',
n_jobs = -1,
min_events_fraction_leaf = 0.,verbose = False):
if metric == None:
metric = penalized_usability_entropy
if min_events_fraction_leaf <=1:
min_events_fraction_leaf = int(min_events_fraction_leaf*sum(factory.weights))
if verbose:
print min_events_fraction_leaf, sum(factory.weights)
if not use_joblib:
scores = np.repeat(float("inf"),len(thresholds))
for i,(feature,cut,_) in enumerate(thresholds):
predicate = (factory.events[:,feature] > cut)
#skip the edge cases... (inf penalty)
if np.all(predicate) or (not np.any(predicate)):
#if this split does not split, fuggedaboutit
continue
if min_events_fraction_leaf>0:
#get rid of too uneven a cuts
sum_weight = np.sum(factory.weights)
true_weight = np.sum(factory.weights[predicate])
false_weight = sum_weight - true_weight
if true_weight < min_events_fraction_leaf or false_weight < min_events_fraction_leaf:
if verbose: print "t:",true_weight,"f:",false_weight, "discarded"
continue
if verbose: print "t:",true_weight,"f:",false_weight, "passed"
#compute score
subFactories = factory.split_by(predicate)
scores[i] = metric(formula,*subFactories)
else:
if n_jobs < 0:
n_jobs = joblib.cpu_count() +1 - n_jobs
indices = [0]+[len(thresholds)*(i+1)/n_jobs for i in range(n_jobs)]
thresholdSections = [thresholds[indices[i]:indices[i+1]] for i in range(n_jobs)]
if joblib_backend == 'threading':
factory = [deepcopy(factory) for i in range(n_jobs)]
formula = [deepcopy(formula) for i in range(n_jobs)]
metric = [deepcopy(metric) for i in range(n_jobs)] #in case it has some internal data
jobs = (joblib.delayed(get_split_scores)(factory[i],thresholdSection, formula[i],
metric=metric[i],use_joblib = False,
min_events_fraction_leaf = min_events_fraction_leaf,
verbose = verbose)
for i,thresholdSection in enumerate(thresholdSections))
else:
jobs = (joblib.delayed(get_split_scores)(factory,thresholdSection, formula,
metric=metric,use_joblib = False,
min_events_fraction_leaf = min_events_fraction_leaf,
verbose = verbose)
for thresholdSection in thresholdSections)
scores = np.hstack(joblib.Parallel(n_jobs = n_jobs, backend = joblib_backend)(jobs))
return scores
示例6: test_multi_output_classification_partial_fit_parallelism
def test_multi_output_classification_partial_fit_parallelism():
sgd_linear_clf = SGDClassifier(loss='log', random_state=1, max_iter=5)
mor = MultiOutputClassifier(sgd_linear_clf, n_jobs=-1)
mor.partial_fit(X, y, classes)
est1 = mor.estimators_[0]
mor.partial_fit(X, y)
est2 = mor.estimators_[0]
if cpu_count() > 1:
# parallelism requires this to be the case for a sane implementation
assert_false(est1 is est2)
示例7: fit
def fit(self, X, y=None, groups=None):
"""Run fit on the estimator with randomly drawn parameters.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples] or [n_samples, n_output]
Target relative to X for classification or regression;
groups : array-like, with shape (n_samples,), optional
Group labels for the samples used while splitting the dataset into
train/test set.
"""
# check if the list of parameter spaces is provided. If not, then
# only step in manual mode can be used.
if len(self.search_spaces_) == 0:
raise ValueError(
"Please provide search space using `add_spaces` first before"
"calling fit method."
)
n_jobs = self.n_jobs
# account for case n_jobs < 0
if n_jobs < 0:
n_jobs = max(1, cpu_count() + n_jobs + 1)
for space_id in sorted(self.search_spaces_.keys()):
elem = self.search_spaces_[space_id]
# if not provided with search subspace, n_iter is taken as
# self.n_iter
if isinstance(elem, tuple):
space, n_iter = elem
else:
n_iter = self.n_iter
# do the optimization for particular search space
while n_iter > 0:
# when n_iter < n_jobs points left for evaluation
n_jobs_adjusted = min(n_iter, self.n_jobs)
self.step(
X, y, space_id,
groups=groups, n_jobs=n_jobs_adjusted
)
n_iter -= n_jobs
示例8: _partition_X
def _partition_X(X, n_jobs):
"""Private function used to partition X between jobs."""
n_nodes = X.shape[1]
# Compute the number of jobs
n_jobs = min(cpu_count() if n_jobs == -1 else n_jobs, n_nodes)
# Partition estimators between jobs
n_node_per_job = (n_nodes // n_jobs) * np.ones(n_jobs, dtype=np.int)
n_node_per_job[:n_nodes % n_jobs] += 1
starts = np.cumsum(n_node_per_job)
return n_jobs, [0] + starts.tolist()
示例9: _partition_estimators
def _partition_estimators(ensemble):
"""Private function used to partition estimators between jobs."""
# Compute the number of jobs
if ensemble.n_jobs == -1:
n_jobs = min(cpu_count(), ensemble.n_estimators)
else:
n_jobs = min(ensemble.n_jobs, ensemble.n_estimators)
# Partition estimators between jobs
n_estimators = (ensemble.n_estimators // n_jobs) * np.ones(n_jobs,
dtype=np.int)
n_estimators[:ensemble.n_estimators % n_jobs] += 1
starts = np.cumsum(n_estimators)
return n_jobs, n_estimators.tolist(), [0] + starts.tolist()
示例10: _partition_estimators
def _partition_estimators(n_estimators, n_jobs):
"""Private function used to partition estimators between jobs."""
# Compute the number of jobs
if n_jobs == -1:
n_jobs = min(cpu_count(), n_estimators)
else:
n_jobs = min(n_jobs, n_estimators)
# Partition estimators between jobs
n_estimators_per_job = (n_estimators // n_jobs) * np.ones(n_jobs,
dtype=np.int)
n_estimators_per_job[:n_estimators % n_jobs] += 1
starts = np.cumsum(n_estimators_per_job)
return n_jobs, n_estimators_per_job.tolist(), [0] + starts.tolist()
示例11: try_add1_bfs
def try_add1_bfs(allTrees,factory,learning_rate,
loss,breadth,y_pred,regularizer = 0.,
use_joblib = False,n_jobs = -1):
'''
select best tree to add (1 step)
'''
if factory.__class__ is BinaryClassificationFactory:
y_sign = factory.labels_sign
margin = y_sign*y_pred
elif factory.__class__ is RegressionFactory:
margin = factory.labels - y_pred
else:
raise Exception("Factory type not supported")
if use_joblib:
if n_jobs < 0:
n_jobs = joblib.cpu_count() + 1 - n_jobs
indices = [0]+[len(allTrees)*(i+1)/n_jobs for i in range(n_jobs)]
treeSections = [allTrees[indices[i]:indices[i+1]] for i in range(n_jobs)]
tasks = [joblib.delayed(_inthread_try_add)(
treeSection,
factory,
loss,
margin,
y_pred,
learning_rate,
regularizer) for treeSection in treeSections]
_res = joblib.Parallel(n_jobs = n_jobs,
backend = "multiprocessing")(tasks)
triples = reduce(lambda a,b:a+b, _res)
else:
triples = [_try_add(tree,factory,loss,margin,y_pred,learning_rate,regularizer) for tree in allTrees]
triples.sort(key = lambda el: el[0])
return ([triple[1] for triple in triples[:breadth]],
[triple[0] for triple in triples[:breadth]],
[triple[2] for triple in triples[:breadth]])
示例12: computePartition
def computePartition(self, nbTasks, dataSize):
"""
Compute data partitioning for parallel computation :
min(nbTasks, dataSize)
Parameters
----------
nbTasks : int (!=0)
If >0 : the parallelization factor.
If <0 : nbTasks = #cpu+nbTasks+1 (-1 -> nbTasks = #cpu)
dataSize : int > 0
The size of the data to process
Return
------
triplet = (nbTasks, counts, starts)
nbTasks : int
The final parallelization factor. It is computed as
min(#cpu/nbTasks, dataSize)
counts : list of int
The number of data pieces for each parallel task
starts : list of int
The start indexes of the data for each parallel task
"""
if nbTasks < 0:
cpu = cpu_count()+nbTasks+1
if cpu <= 0:
cpu = 1
nbTasks = min(cpu, dataSize)
else:
if nbTasks == 0:
nbTasks = 1
nbTasks = min(nbTasks, dataSize)
counts = [dataSize / nbTasks] * nbTasks
for i in xrange(dataSize % nbTasks):
counts[i] += 1
starts = [0] * (nbTasks + 1)
for i in xrange(1, nbTasks + 1):
starts[i] = starts[i - 1] + counts[i - 1]
return nbTasks, counts, starts
示例13: _partition_clips
def _partition_clips(n_jobs, n_clips):
if n_jobs == -1:
n_jobs = min(cpu_count(), n_clips)
else:
n_jobs = min(n_jobs, n_clips)
counts = [n_clips / n_jobs] * n_jobs
for i in xrange(n_clips % n_jobs):
counts[i] += 1
starts = [0] * (n_jobs + 1)
for i in xrange(1, n_jobs + 1):
starts[i] = starts[i - 1] + counts[i - 1]
return n_jobs, counts, starts
示例14: _set_params_with_dependencies
def _set_params_with_dependencies(self):
if self.max_bin is None:
if self._is_sparse_train_X:
self._max_bin = 200
else:
self._max_bin = 65000
else:
self._max_bin = self.max_bin
if isinstance(self.min_samples_leaf, utils.FLOATS):
self._min_samples_leaf = ceil(self.min_samples_leaf * self._n_samples)
else:
self._min_samples_leaf = self.min_samples_leaf
if self.n_jobs == -1:
self._n_jobs = 0
elif self.n_jobs < 0:
self._n_jobs = cpu_count() + self.n_jobs + 1
else:
self._n_jobs = self.n_jobs
self._set_target_and_loss()
示例15: _e_step
def _e_step(self, X, cal_delta):
"""
E-step
set `cal_delta == True` when we need to run _m_step
for inference, set it to False
"""
# parell run e-step
if self.n_jobs == -1:
n_jobs = cpu_count()
else:
n_jobs = self.n_jobs
results = Parallel(n_jobs=n_jobs, verbose=self.verbose)(
delayed(_update_gamma)
(X[idx_slice, :], self.expElogbeta, self.alpha,
self.rng, 100, self.mean_change_tol, cal_delta)
for idx_slice in gen_even_slices(X.shape[0], n_jobs))
# merge result
gammas, deltas = zip(*results)
gamma = np.vstack(gammas)
if cal_delta:
# This step finishes computing the sufficient statistics for the
# M step, so that
# sstats[k, w] = \sum_d n_{dw} * phi_{dwk}
# = \sum_d n_{dw} * exp{Elogtheta_{dk} + Elogbeta_{kw}} / phinorm_{dw}.
delta_component = np.zeros(self.components_.shape)
for delta in deltas:
delta_component += delta
delta_component *= self.expElogbeta
else:
delta_component = None
return (gamma, delta_component)