本文整理汇总了Python中numpy.random.permutation方法的典型用法代码示例。如果您正苦于以下问题:Python random.permutation方法的具体用法?Python random.permutation怎么用?Python random.permutation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.random
的用法示例。
在下文中一共展示了random.permutation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __iter__
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def __iter__(self):
if cfg.TRAIN.ASPECT_GROUPING:
# indices for aspect grouping awared permutation
n, rem = divmod(self.num_data, cfg.TRAIN.IMS_PER_BATCH)
round_num_data = n * cfg.TRAIN.IMS_PER_BATCH
indices = np.arange(round_num_data)
npr.shuffle(indices.reshape(-1, cfg.TRAIN.IMS_PER_BATCH)) # inplace shuffle
if rem != 0:
indices = np.append(indices, np.arange(round_num_data, round_num_data + rem))
ratio_index = self.ratio_index[indices]
ratio_list_minibatch = self.ratio_list_minibatch[indices]
else:
rand_perm = npr.permutation(self.num_data)
ratio_list = self.ratio_list[rand_perm]
ratio_index = self.ratio_index[rand_perm]
# re-calculate minibatch ratio list
ratio_list_minibatch = cal_minibatch_ratio(ratio_list)
return iter(zip(ratio_index.tolist(), ratio_list_minibatch.tolist()))
示例2: test_large_partial_random
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def test_large_partial_random(self) -> None:
"""Test a random (partial) mapping on a large randomly generated graph"""
size = 100
# Note that graph may have "gaps" in the node counts, i.e. the numbering is noncontiguous.
graph = nx.dense_gnm_random_graph(size, size ** 2 // 10)
graph.remove_edges_from((i, i) for i in graph.nodes) # Remove self-loops.
# Make sure the graph is connected by adding C_n
nodes = list(graph.nodes)
graph.add_edges_from((node, nodes[(i + 1) % len(nodes)]) for i, node in enumerate(nodes))
swapper = ApproximateTokenSwapper(graph) # type: ApproximateTokenSwapper[int]
# Generate a randomized permutation.
rand_perm = random.permutation(graph.nodes())
permutation = dict(zip(graph.nodes(), rand_perm))
mapping = dict(itertools.islice(permutation.items(), 0, size, 2)) # Drop every 2nd element.
out = list(swapper.map(mapping, trials=40))
util.swap_permutation([out], mapping, allow_missing_keys=True)
self.assertEqual({i: i for i in mapping.values()}, mapping)
示例3: read_and_normalize_and_shuffle_train_data
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def read_and_normalize_and_shuffle_train_data(img_rows, img_cols, color_type, random_seed, subtract_mean=True):
np.random.seed(random_seed)
train_data, train_target, train_id = load_train(img_rows, img_cols, color_type)
print('Convert to numpy...')
train_data = np.array(train_data, dtype=np.uint8)
train_target = np.array(train_target, dtype=np.uint8)
train_target_vec = copy.deepcopy(train_target)
train_target = np_utils.to_categorical(train_target, 27) # Transform Categorical Vector to One-Hot-Encoded Vector
train_id = np.array(train_id)
print('Convert to float...')
train_data = train_data.astype('float32')
perm = permutation(len(train_target))
train_data = train_data[perm]
train_target = train_target[perm]
train_target_vec = train_target_vec[perm]
train_id = train_id[perm]
print('Train shape:', train_data.shape)
print(train_data.shape[0], 'train samples')
return train_data, train_target, train_target_vec, train_id
示例4: _reset_iter
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def _reset_iter(self):
if cfg.TRAIN.ASPECT_GROUPING:
# indices for aspect grouping awared permutation
n, rem = divmod(self.num_data, cfg.TRAIN.IMS_PER_BATCH)
round_num_data = n * cfg.TRAIN.IMS_PER_BATCH
indices = np.arange(round_num_data)
npr.shuffle(indices.reshape(-1, cfg.TRAIN.IMS_PER_BATCH)) # inplace shuffle
if rem != 0:
indices = np.append(indices, np.arange(round_num_data, round_num_data + rem))
self._ratio_index = self.ratio_index[indices]
self._ratio_list_minibatch = self.ratio_list_minibatch[indices]
else:
rand_perm = npr.permutation(self.num_data)
ratio_list = self.ratio_list[rand_perm]
self._ratio_index = self.ratio_index[rand_perm]
# re-calculate minibatch ratio list
self._ratio_list_minibatch = cal_minibatch_ratio(ratio_list)
self.iter_counter = 0
self._ratio_index = self._ratio_index.tolist()
self._ratio_list_minibatch = self._ratio_list_minibatch.tolist()
示例5: sparse_rand
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def sparse_rand(shape, frac=0.05, round_up=False):
# generate an input with sparse activation
# in the input dimension for LSTM testing
# frac is the fraction of the matrix elements
# which will be nonzero. Set round_up to
# True to get a binary matrix, i.e. elements
# are either set to 0 or 1
num_el = np.prod(shape)
inds = nprnd.permutation(num_el)[0:int(frac * num_el)]
# draw frac*num_el random numbers
vals = nprnd.random(inds.size)
if round_up:
vals = np.ceil(vals)
out = np.zeros(shape)
out.flat[inds] = vals
return (out, inds)
示例6: shuffle
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def shuffle(self):
batch = self.FLAGS.batch
data = self.parse()
size = len(data)
print('Dataset of {} instance(s)'.format(size))
if batch > size: self.FLAGS.batch = batch = size
batch_per_epoch = int(size / batch)
for i in range(self.FLAGS.epoch):
shuffle_idx = perm(np.arange(size))
for b in range(batch_per_epoch):
# yield these
x_batch = list()
feed_batch = dict()
for j in range(b*batch, b*batch+batch):
train_instance = data[shuffle_idx[j]]
try:
inp, new_feed = self._batch(train_instance)
except ZeroDivisionError:
print("This image's width or height are zeros: ", train_instance[0])
print('train_instance:', train_instance)
print('Please remove or fix it then try again.')
raise
if inp is None: continue
x_batch += [np.expand_dims(inp, 0)]
for key in new_feed:
new = new_feed[key]
old_feed = feed_batch.get(key,
np.zeros((0,) + new.shape))
feed_batch[key] = np.concatenate([
old_feed, [new]
])
x_batch = np.concatenate(x_batch, 0)
yield x_batch, feed_batch
print('Finish {} epoch(es)'.format(i + 1))
示例7: shuffle
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def shuffle(self):
batch = self.FLAGS.batch
data = self.parse()
size = len(data)
print('Dataset of {} instance(s)'.format(size))
if batch > size: self.FLAGS.batch = batch = size
batch_per_epoch = int(size / batch)
for i in range(self.FLAGS.epoch):
shuffle_idx = perm(np.arange(size))
for b in range(batch_per_epoch):
# yield these
x_batch = list()
feed_batch = dict()
for j in range(b*batch, b*batch+batch):
train_instance = data[shuffle_idx[j]]
inp, new_feed = self._batch(train_instance)
if inp is None: continue
x_batch += [np.expand_dims(inp, 0)]
for key in new_feed:
new = new_feed[key]
old_feed = feed_batch.get(key,
np.zeros((0,) + new.shape))
feed_batch[key] = np.concatenate([
old_feed, [new]
])
x_batch = np.concatenate(x_batch, 0)
yield x_batch, feed_batch
print('Finish {} epoch(es)'.format(i + 1))
示例8: init_centroids
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def init_centroids(X, K):
"""Computes centroids from the mean of its cluster's members.
Args:
X (numpy.array): Features' dataset
idx (numpy.array): Column vector of assigned centroids' indices.
K (int): Number of centroids.
Returns:
numpy.array: Column vector of centroids randomly picked from dataset
"""
centroids = permutation(X)
centroids = centroids[0:K, :]
return centroids
示例9: fit
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def fit(self, X):
"""Sample a training set.
Parameters
----------
X: array-like
training set to sample observations from.
Returns
----------
self: obj
fitted instance with stored sample.
"""
self.train_shape = X.shape
sample_idx = {}
for i in range(2):
dim_size = min(X.shape[i], self.size)
sample_idx[i] = permutation(X.shape[i])[:dim_size]
sample = X[ix_(sample_idx[0], sample_idx[1])]
self.sample_idx_ = sample_idx
self.sample_ = sample
return self
示例10: enqueue
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def enqueue(self):
perm = random.permutation(self.M)
self.A = multinomial(self.W, M=self.M)[perm]
示例11: test_simple
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def test_simple(self) -> None:
"""Test a simple permutation on a path graph of size 4."""
graph = nx.path_graph(4)
permutation = {0: 0, 1: 3, 3: 1, 2: 2}
swapper = ApproximateTokenSwapper(graph) # type: ApproximateTokenSwapper[int]
out = list(swapper.map(permutation))
self.assertEqual(3, len(out))
util.swap_permutation([out], permutation)
self.assertEqual({i: i for i in range(4)}, permutation)
示例12: test_small
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def test_small(self) -> None:
"""Test an inverting permutation on a small path graph of size 8"""
graph = nx.path_graph(8)
permutation = {i: 7 - i for i in range(8)}
swapper = ApproximateTokenSwapper(graph) # type: ApproximateTokenSwapper[int]
out = list(swapper.map(permutation))
util.swap_permutation([out], permutation)
self.assertEqual({i: i for i in range(8)}, permutation)
示例13: test_bug1
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def test_bug1(self) -> None:
"""Tests for a bug that occured in happy swap chains of length >2."""
graph = nx.Graph()
graph.add_edges_from([(0, 1), (0, 2), (0, 3), (0, 4),
(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (3, 6)])
permutation = {0: 4, 1: 0, 2: 3, 3: 6, 4: 2, 6: 1}
swapper = ApproximateTokenSwapper(graph) # type: ApproximateTokenSwapper[int]
out = list(swapper.map(permutation))
util.swap_permutation([out], permutation)
self.assertEqual({i: i for i in permutation}, permutation)
示例14: test_partial_small
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def test_partial_small(self) -> None:
"""Test an partial inverting permutation on a small path graph of size 5"""
graph = nx.path_graph(4)
permutation = {i: 3 - i for i in range(2)}
swapper = ApproximateTokenSwapper(graph) # type: ApproximateTokenSwapper[int]
out = list(swapper.map(permutation))
self.assertEqual(5, len(out))
util.swap_permutation([out], permutation, allow_missing_keys=True)
self.assertEqual({i: i for i in permutation.values()}, permutation)
示例15: _sample_rois
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import permutation [as 别名]
def _sample_rois(roidb, num_classes):
"""Generate a random sample of RoIs"""
labels = roidb['gt_classes']
rois = roidb['boxes']
if cfg.TRAIN.BATCH_SIZE_PER_IM > 0:
batch_size = cfg.TRAIN.BATCH_SIZE_PER_IM
else:
batch_size = np.inf
if batch_size < rois.shape[0]:
rois_inds = npr.permutation(rois.shape[0])[:batch_size]
rois = rois[rois_inds, :]
return labels.reshape(1, -1), rois