本文整理汇总了Python中numpy.random.shuffle方法的典型用法代码示例。如果您正苦于以下问题:Python random.shuffle方法的具体用法?Python random.shuffle怎么用?Python random.shuffle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.random
的用法示例。
在下文中一共展示了random.shuffle方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_dirs
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def prepare_dirs(delete_train_dir=False):
# Create checkpoint dir (do not delete anything)
if not tf.gfile.Exists(FLAGS.checkpoint_dir):
tf.gfile.MakeDirs(FLAGS.checkpoint_dir)
# Cleanup train dir
if delete_train_dir:
if tf.gfile.Exists(FLAGS.train_dir):
tf.gfile.DeleteRecursively(FLAGS.train_dir)
tf.gfile.MakeDirs(FLAGS.train_dir)
# Return names of training files
if not tf.gfile.Exists(FLAGS.dataset) or \
not tf.gfile.IsDirectory(FLAGS.dataset):
raise FileNotFoundError("Could not find folder `%s'" % (FLAGS.dataset,))
filenames = tf.gfile.ListDirectory(FLAGS.dataset)
filenames = sorted(filenames)
random.shuffle(filenames)
filenames = [os.path.join(FLAGS.dataset, f) for f in filenames]
return filenames
示例2: __iter__
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [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()))
示例3: _reset_iter
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [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()
示例4: _optim
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def _optim(self, xys):
idx = np.arange(len(xys))
self.batch_size = np.ceil(len(xys) / self.nbatches)
batch_idx = np.arange(self.batch_size, len(xys), self.batch_size)
for self.epoch in range(1, self.max_epochs + 1):
# shuffle training examples
self._pre_epoch()
shuffle(idx)
# store epoch for callback
self.epoch_start = timeit.default_timer()
# process mini-batches
for batch in np.split(idx, batch_idx):
# select indices for current batch
bxys = [xys[z] for z in batch]
self._process_batch(bxys)
# check callback function, if false return
for f in self.post_epoch:
if not f(self):
break
示例5: optim
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def optim(self, xys): # Performs actual optimization
idx = np.arange(len(xys)) # Index for every triple in dataset
self.batch_size = int(np.ceil(len(xys) / self.nbatches)) # Calculte batch size (n_obsv / n_batches)
batch_idx = np.arange(self.batch_size, len(xys), self.batch_size) # np.arange(start, stop, step) -> To get split positions (10,50,10) = [10,20,30,40]
for self.epoch in range(1, self.max_epochs + 1): # Running for maximum number of epochs
# shuffle training examples
self.pre_epoch() # Set loss = 0
shuffle(idx) # Shuffle the indexes of triples
# store epoch for callback
self.epoch_start = timeit.default_timer() # Measuring time
# process mini-batches
for batch in np.split(idx, batch_idx): # Get small subset of triples from training data
bxys = [xys[z] for z in batch] # Get triples present in the selected batch
self.process_batch(bxys) # Perform SGD using them
# check callback function, if false return
for f in self.post_epoch: # Perform post epoch operation is specified
if not f(self): break
示例6: test_shuffle_mixed_dimension
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def test_shuffle_mixed_dimension(self):
# Test for trac ticket #2074
for t in [[1, 2, 3, None],
[(1, 1), (2, 2), (3, 3), None],
[1, (2, 2), (3, 3), None],
[(1, 1), 2, 3, None]]:
np.random.seed(12345)
shuffled = list(t)
random.shuffle(shuffled)
assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]])
示例7: test_shuffle_of_array_of_different_length_strings
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def test_shuffle_of_array_of_different_length_strings(self):
# Test that permuting an array of different length strings
# will not cause a segfault on garbage collection
# Tests gh-7710
np.random.seed(1234)
a = np.array(['a', 'a' * 1000])
for _ in range(100):
np.random.shuffle(a)
# Force Garbage Collection - should not segfault.
import gc
gc.collect()
示例8: test_shuffle_of_array_of_objects
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def test_shuffle_of_array_of_objects(self):
# Test that permuting an array of objects will not cause
# a segfault on garbage collection.
# See gh-7719
np.random.seed(1234)
a = np.array([np.arange(1), np.arange(4)])
for _ in range(1000):
np.random.shuffle(a)
# Force Garbage Collection - should not segfault.
import gc
gc.collect()
示例9: create_one_batch
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def create_one_batch(self, ids, x, y):
batch_x = numpy.column_stack( [ x[i] for i in ids ] )
batch_y = numpy.array( [ y[i] for i in ids ] )
return batch_x, batch_y
# shuffle training examples and create mini-batches
示例10: create_batches
# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import shuffle [as 别名]
def create_batches(self, perm, x, y, dropout_p=0, rnd=None):
if dropout_p > 0:
dropout_x = [ ]
for a in x:
b = [ w for w in a if rnd.random() > dropout_p ]
if len(b) == 0:
b.append( a[random.randint(0, len(a)-1)] )
dropout_x.append(b)
x = dropout_x
# sort sequences based on their length
# permutation is necessary if we want different batches every epoch
lst = sorted(perm, key=lambda i: len(x[i]))
batches_x = [ ]
batches_y = [ ]
size = self.args.batch
ids = [ lst[0] ]
for i in lst[1:]:
if len(ids) < size and len(x[i]) == len(x[ids[0]]):
ids.append(i)
else:
bx, by = self.create_one_batch(ids, x, y)
batches_x.append(bx)
batches_y.append(by)
ids = [ i ]
bx, by = self.create_one_batch(ids, x, y)
batches_x.append(bx)
batches_y.append(by)
# shuffle batches
batch_perm = range(len(batches_x))
random.shuffle(batch_perm)
batches_x = [ batches_x[i] for i in batch_perm ]
batches_y = [ batches_y[i] for i in batch_perm ]
return batches_x, batches_y