本文整理汇总了Python中sklearn.utils.gen_even_slices函数的典型用法代码示例。如果您正苦于以下问题:Python gen_even_slices函数的具体用法?Python gen_even_slices怎么用?Python gen_even_slices使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gen_even_slices函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_gen_even_slices
def test_gen_even_slices():
# check that gen_even_slices contains all samples
some_range = range(10)
joined_range = list(chain(*[some_range[slice] for slice in gen_even_slices(10, 3)]))
assert_array_equal(some_range, joined_range)
# check that passing negative n_chunks raises an error
slices = gen_even_slices(10, -1)
assert_raises_regex(ValueError, "gen_even_slices got n_packs=-1, must be" " >=1", next, slices)
示例2: _sequential_learning
def _sequential_learning(self, X, Y, w):
n_samples = len(X)
objective, positive_slacks = 0, 0
if self.batch_size in [None, 1]:
# online learning
for x, y in zip(X, Y):
y_hat, delta_psi, slack, loss = find_constraint(self.model, x, y, w)
objective += slack
if slack > 0:
positive_slacks += 1
self._solve_subgradient(delta_psi, n_samples, w)
else:
# mini batch learning
if self.batch_size == -1:
slices = [slice(0, len(X)), None]
else:
n_batches = int(np.ceil(float(len(X)) / self.batch_size))
slices = gen_even_slices(n_samples, n_batches)
for batch in slices:
X_b = X[batch]
Y_b = Y[batch]
Y_hat = self.model.batch_loss_augmented_inference(X_b, Y_b, w, relaxed=True)
delta_psi = self.model.batch_psi(X_b, Y_b) - self.model.batch_psi(X_b, Y_hat)
loss = np.sum(self.model.batch_loss(Y_b, Y_hat))
violation = np.maximum(0, loss - np.dot(w, delta_psi))
objective += violation
positive_slacks += self.batch_size
self._solve_subgradient(delta_psi / len(X_b), n_samples, w)
return objective, positive_slacks, w
示例3: _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
示例4: train
def train(self, X):
n_samples, n_features = X.shape
for i in range(self.numepochs):
kk = np.random.permutation(m)
err = 0
for l in gen_even_slices(n_samples, self.batchsize):
batch = x[l, :]
v1 = batch # n_samples X n_visible
h1 = sigmrnd(np.dot(v1, self.W.T) + self.c) # n_samples X n_hidden
v2 = sigmrnd(np.dot(h1, self.W) + self.b) # n_samples X n_visible
h2 = sigm(np.dot(v2, self.W.T) + self.c) # n_samples X n_hidden
c1 = np.dot(h1.T, v1) # n_hidden X n_visible
c2 = np.dot(h2.T, v2) # n_hidden X n_visible
self.vW = self.momentum * self.vW + self.alpha * (c1 - c2) / self.batchsize # n_hidden X n_visible
self.vb = self.momentum * self.vb + self.alpha * np.sum(v1 - v2, axis=0) / self.batchsize # n_visible X 1
self.vc = self.momentum * self.vc + self.alpha * np.sum(h1 - h2, axis=0) / self.batchsize # n_hidden X 1
self.W = self.W + self.vW # n_hidden X n_visible
self.b = self.b + self.vb # n_visible X 1
self.c = self.c + self.vc # n_visible X 1
err = err + np.sum(np.power(v1 - v2), 2) / self.batchsize
print 'epoch '+ str(i) + '/' + str(self.numepochs) + '. Average reconstruction error is: ' + str(err / numbatches)
示例5: fit
def fit(self, X, y=None):
"""Fit the model to the data X.
Parameters
----------
X : {array-like, sparse matrix} shape (n_samples, n_features)
Training data.
Returns
-------
self : BernoulliRBM
The fitted model.
"""
X, = check_arrays(X, sparse_format='csr', dtype=np.float)
n_samples = X.shape[0]
rng = check_random_state(self.random_state)
self.components_ = np.asarray(
rng.normal(0, 0.01, (self.n_components, X.shape[1])),
order='fortran')
self.intercept_hidden_ = np.zeros(self.n_components, )
self.intercept_visible_ = np.zeros(X.shape[1], )
self.h_samples_ = np.zeros((self.batch_size, self.n_components))
n_batches = int(np.ceil(float(n_samples) / self.batch_size))
batch_slices = list(gen_even_slices(n_batches * self.batch_size,
n_batches, n_samples))
for iteration in xrange(1, self.n_iter + 1):
for batch_slice in batch_slices:
self._fit(X[batch_slice], rng)
return self
示例6: _mean_of_squares
def _mean_of_squares(signals, n_batches=20):
"""Compute mean of squares for each signal.
This function is equivalent to
var = np.copy(signals)
var **= 2
var = var.mean(axis=0)
but uses a lot less memory.
Parameters
==========
signals : numpy.ndarray, shape (n_samples, n_features)
signal whose mean of squares must be computed.
n_batches : int, optional
number of batches to use in the computation. Tweaking this value
can lead to variation of memory usage and computation time. The higher
the value, the lower the memory consumption.
"""
# No batching for small arrays
if signals.shape[1] < 500:
n_batches = 1
# Fastest for C order
var = np.empty(signals.shape[1])
for batch in gen_even_slices(signals.shape[1], n_batches):
tvar = np.copy(signals[:, batch])
tvar **= 2
var[batch] = tvar.mean(axis=0)
return var
示例7: _mini_batch_compute
def _mini_batch_compute(self, n_samples):
'''
Compute equal sized minibatches ( indexes )
This method is taken from sklearn/neural_network/rbm.py
'''
n_batches = int(np.ceil(float(n_samples) / self.batch_size))
batch_slices = list(gen_even_slices(n_batches * self.batch_size,n_batches, n_samples))
return batch_slices
示例8: fit
def fit(self, data):
num_examples = data.shape[0]
self.h_samples_ = np.zeros((self.batch_size, self.num_hidden))
n_batches = int(np.ceil(float(num_examples) / self.batch_size))
batch_slices = list(gen_even_slices(n_batches * self.batch_size,
n_batches, num_examples))
for iteration in xrange(1, self.max_epochs + 1):
for batch_slice in batch_slices:
self._fit(data[batch_slice])
示例9: check_gen_even_slices
def check_gen_even_slices():
even = csr_matrix((1032,1030))
odd = csr_matrix((1033,1033))
batch_size = 100
even_batches = int(np.ceil(float(even.shape[0]) / batch_size))
odd_batches = int(np.ceil(float(odd.shape[0]) / batch_size))
odd_slices = list(gen_even_slices(odd_batches * batch_size,
odd_batches, odd.shape[0]))
even_slices = list(gen_even_slices(even_batches * batch_size,
even_batches, even.shape[0]))
assert slices_bounds_check(even,even_slices) == "passes", "Fails on Even number of rows"
assert slices_bounds_check(odd,odd_slices) == "passes", "Fails on Odd number of rows"
print("OK")
示例10: _detrend
def _detrend(signals, inplace=False, type="linear", n_batches=10):
"""Detrend columns of input array.
Signals are supposed to be columns of `signals`.
This function is significantly faster than scipy.signal.detrend on this
case and uses a lot less memory.
Parameters
==========
signals : numpy.ndarray
This parameter must be two-dimensional.
Signals to detrend. A signal is a column.
inplace : bool, optional
Tells if the computation must be made inplace or not (default
False).
type : str, optional
Detrending type ("linear" or "constant").
See also scipy.signal.detrend.
n_batches : int, optional
number of batches to use in the computation. Tweaking this value
can lead to variation of memory usage and computation time. The higher
the value, the lower the memory consumption.
Returns
=======
detrended_signals: numpy.ndarray
Detrended signals. The shape is that of 'signals'.
"""
if not inplace:
signals = signals.copy()
signals -= np.mean(signals, axis=0)
if type == "linear":
# Keeping "signals" dtype avoids some type conversion further down,
# and can save a lot of memory if dtype is single-precision.
regressor = np.arange(signals.shape[0], dtype=signals.dtype)
regressor -= regressor.mean()
std = np.sqrt((regressor ** 2).sum())
# avoid numerical problems
if not std < np.finfo(np.float).eps:
regressor /= std
regressor = regressor[:, np.newaxis]
# No batching for small arrays
if signals.shape[1] < 500:
n_batches = 1
# This is fastest for C order.
for batch in gen_even_slices(signals.shape[1], n_batches):
signals[:, batch] -= np.dot(regressor[:, 0], signals[:, batch]
) * regressor
return signals
示例11: load_data
def load_data(name, partition_id, n_partitions):
"""load partition of data into global var `name`"""
from sklearn.datasets import fetch_20newsgroups_vectorized
from sklearn.utils import gen_even_slices
dataset = fetch_20newsgroups_vectorized('test')
size = dataset.data.shape[0]
slices = list(gen_even_slices(size, n_partitions))
part = dataset.data[slices[partition_id]]
# put it in globals
globals().update({name : part})
return part.shape
示例12: _parallel_inner_prod
def _parallel_inner_prod(X, Y, func, n_jobs, **kwds):
"""Break the pairwise matrix in n_jobs even slices
and compute them in parallel"""
if n_jobs < 0:
n_jobs = max(cpu_count() + 1 + n_jobs, 1)
if Y is None:
Y = X
ret = Parallel(n_jobs=n_jobs, verbose=0)(
delayed(func)(X[s], Y, **kwds)
for s in gen_even_slices(len(X), n_jobs))
return np.hstack(ret)
示例13: fit
def fit(self, X):
"""Fit SGVB to the data
Parameters
----------
X : array-like, shape (N, n_features)
The data that the SGVB needs to fit on
Returns
-------
list_lowerbound : list of int
list of lowerbound over time
"""
X, = check_arrays(X, sparse_format='csr', dtype=np.float)
[N, dimX] = X.shape
rng = check_random_state(self.random_state)
self._initParams(dimX, rng)
list_lowerbound = np.array([])
n_batches = int(np.ceil(float(N) / self.batch_size))
batch_slices = list(gen_even_slices(n_batches * self.batch_size,
n_batches, N))
if self.verbose:
print "Initializing gradients for AdaGrad"
for i in xrange(10):
self._initH(X[batch_slices[i]], rng)
begin = time.time()
for iteration in xrange(1, self.n_iter + 1):
iteration_lowerbound = 0
for batch_slice in batch_slices:
lowerbound = self._updateParams(X[batch_slice], N, rng)
iteration_lowerbound += lowerbound
if self.verbose:
end = time.time()
print("[%s] Iteration %d, lower bound = %.2f,"
" time = %.2fs"
% (self.__class__.__name__, iteration,
iteration_lowerbound / N, end - begin))
begin = end
list_lowerbound = np.append(
list_lowerbound, iteration_lowerbound / N)
return list_lowerbound
示例14: fit
def fit(self, X, y=None):
"""Fit the model to the data X.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Training data.
Returns
-------
self : BernoulliRBM
The fitted model.
"""
X, = check_arrays(X, sparse_format='csc', dtype=np.float)
n_samples = X.shape[0]
rng = check_random_state(self.random_state)
self.components_ = np.asarray(
rng.normal(0, 0.01, (self.n_components, X.shape[1])),
order='fortran')
self.intercept_hidden_ = np.zeros(self.n_components, )
self.intercept_visible_ = np.zeros(X.shape[1], )
self.h_samples_ = np.zeros((self.batch_size, self.n_components))
n_batches = int(np.ceil(float(n_samples) / self.batch_size))
batch_slices = list(gen_even_slices(n_batches * self.batch_size,
n_batches))
verbose = self.verbose
for iteration in xrange(self.n_iter):
pl = 0.
if verbose:
begin = time.time()
for batch_slice in batch_slices:
pl_batch = self._fit(X[batch_slice], rng)
if verbose:
pl += pl_batch.sum()
if verbose:
pl /= n_samples
end = time.time()
print("Iteration %d, pseudo-likelihood = %.2f, time = %.2fs"
% (iteration, pl, end - begin))
return self
示例15: fit
def fit(self, X, y, max_epochs, shuffle_data, staged_sample=None, verbose=0):
# get all sizes
n_samples, n_features = X.shape
if y.shape[0] != n_samples:
raise ValueError("Shapes of X and y don't fit.")
self.n_outs = y.shape[1]
# n_batches = int(np.ceil(float(n_samples) / self.batch_size))
n_batches = n_samples / self.batch_size
if n_samples % self.batch_size != 0:
warnings.warn("Discarding some samples: \
sample size not divisible by chunk size.")
n_iterations = int(max_epochs * n_batches)
if shuffle_data:
X, y = shuffle(X, y)
# generate batch slices
batch_slices = list(
gen_even_slices(n_batches * self.batch_size, n_batches))
# generate weights.
# TODO: smart initialization
self.weights1_ = np.random.uniform(
size=(n_features, self.n_hidden)) / np.sqrt(n_features)
self.bias1_ = np.zeros(self.n_hidden)
self.weights2_ = np.random.uniform(
size=(self.n_hidden, self.n_outs)) / np.sqrt(self.n_hidden)
self.bias2_ = np.zeros(self.n_outs)
# preallocate memory
x_hidden = np.empty((self.batch_size, self.n_hidden))
delta_h = np.empty((self.batch_size, self.n_hidden))
x_output = np.empty((self.batch_size, self.n_outs))
delta_o = np.empty((self.batch_size, self.n_outs))
self.oo_score = []
# main loop
for i, batch_slice in izip(xrange(n_iterations), cycle(batch_slices)):
self._forward(i, X, batch_slice, x_hidden, x_output, testing=False)
self._backward(
i, X, y, batch_slice, x_hidden, x_output, delta_o, delta_h)
if staged_sample is not None:
self.oo_score.append(self.predict(staged_sample))
return self