本文整理汇总了Python中random.shuffle方法的典型用法代码示例。如果您正苦于以下问题:Python random.shuffle方法的具体用法?Python random.shuffle怎么用?Python random.shuffle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类random
的用法示例。
在下文中一共展示了random.shuffle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_train_test_sets
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def make_train_test_sets(pos_graphs, neg_graphs,
test_proportion=.3, random_state=2):
"""make_train_test_sets."""
random.seed(random_state)
random.shuffle(pos_graphs)
random.shuffle(neg_graphs)
pos_dim = len(pos_graphs)
neg_dim = len(neg_graphs)
tr_pos_graphs = pos_graphs[:-int(pos_dim * test_proportion)]
te_pos_graphs = pos_graphs[-int(pos_dim * test_proportion):]
tr_neg_graphs = neg_graphs[:-int(neg_dim * test_proportion)]
te_neg_graphs = neg_graphs[-int(neg_dim * test_proportion):]
tr_graphs = tr_pos_graphs + tr_neg_graphs
te_graphs = te_pos_graphs + te_neg_graphs
tr_targets = [1] * len(tr_pos_graphs) + [0] * len(tr_neg_graphs)
te_targets = [1] * len(te_pos_graphs) + [0] * len(te_neg_graphs)
tr_graphs, tr_targets = paired_shuffle(tr_graphs, tr_targets)
te_graphs, te_targets = paired_shuffle(te_graphs, te_targets)
return (tr_graphs, np.array(tr_targets)), (te_graphs, np.array(te_targets))
示例2: setup
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def setup(self, bottom, top):
layer_params = yaml.load(self.param_str)
self._layer_params = layer_params
# default batch_size = 256
self._batch_size = int(layer_params.get('batch_size', 256))
self._resize = layer_params.get('resize', -1)
self._mean_file = layer_params.get('mean_file', None)
self._source_type = layer_params.get('source_type', 'CSV')
self._shuffle = layer_params.get('shuffle', False)
# read image_mean from file and preload all data into memory
# will read either file or array into self._mean
self.set_mean()
self.preload_db()
self._compressed = self._layer_params.get('compressed', True)
if not self._compressed:
self.decompress_data()
示例3: preload_db
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def preload_db(self):
"""Read all images in and all labels
Implemenation relies on DataManager Classes
"""
print("Preloading Data...")
if self._source_type == 'BCF':
self._data_manager = BCFDataManager(self._layer_params)
elif self._source_type == 'CSV':
self._data_manager = CSVDataManager(self._layer_params)
elif self._source_type == 'LMDB':
self._data_manager = LMDBDataManager(self._layer_params)
# read all data
self._data, self._label = self._data_manager.load_all()
self._sample_count = len(self._data)
if self._shuffle:
self.shuffle()
示例4: shuffle_sequences
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def shuffle_sequences(self, split="train", keys=None):
if keys is None:
# print(type(self.data))
# print(type(self.data.keys()))
keys = self.data[split].keys()
for key in keys:
if key in ["positive", "negative"]:
continue
idxs = list(range(len(self.data[split][key])))
random.shuffle(idxs)
self.sequences[split][key] = \
self.sequences[split][key].index_select(
0, torch.LongTensor(idxs))
temp = [self.data[split][key][i] for i in idxs]
self.data[split][key] = temp
temp = [self.masks[split][key][i] for i in idxs]
self.masks[split][key] = temp
示例5: shuffle_sequences
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def shuffle_sequences(self, split="train", keys=None):
if keys is None:
# print(type(self.data))
# print(type(self.data.keys()))
keys = self.data[split].keys()
for key in keys:
idxs = list(range(len(self.data[split][key])))
random.shuffle(idxs)
self.sequences[split][key] = \
self.sequences[split][key].index_select(
0, torch.LongTensor(idxs))
temp = [self.data[split][key][i] for i in idxs]
self.data[split][key] = temp
temp = [self.masks[split][key][i] for i in idxs]
self.masks[split][key] = temp
示例6: encode_turing_machine_rules
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def encode_turing_machine_rules(rules, starting_state=None, story=None):
if story is None:
story = graph_tools.Story()
graph = story.graph
if starting_state is None:
starting_state = random.choice(len(rules))
the_edges = [(cstate, read, write, nstate, direc)
for (cstate, stuff) in enumerate(rules)
for (read, (write, nstate, direc)) in enumerate(stuff)]
random.shuffle(the_edges)
for cstate, read, write, nstate, direc in the_edges:
source = graph.make_unique('state_{}'.format(cstate))
dest = graph.make_unique('state_{}'.format(nstate))
edge_type = "rule_{}_{}_{}".format(read,write,direc)
source[edge_type] = dest
story.add_line("rule {} {} {} {} {}".format(source.type, read, write, dest.type, direc))
head = graph.make_unique('head')
head.state = graph.make_unique('state_{}'.format(starting_state))
story.add_line("start {}".format(head.state.type))
return story
示例7: show
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def show(limit, shuffle=True):
td = TrainingData(LABEL_FILE, img_root=IMAGES_ROOT, mean_image_file=MEAN_IMAGE_FILE, image_property=IMAGE_PROP)
_limit = limit if limit > 0 else 5
iterator = td.generate()
if shuffle:
import random
shuffled = list(iterator)
random.shuffle(shuffled)
iterator = iter(shuffled)
i = 0
for arr, im in iterator:
restored = td.data_to_image(arr, im.label, raw=True)
print(im.path)
restored.image.show()
i += 1
if i >= _limit:
break
示例8: __init__
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def __init__(self, path, shuffle, config):
"""
:param path: data path list
:param shuffle: shuffle bool
:param config: config
"""
#
print("Loading Data......")
self.data_list = []
self.max_count = config.max_count
self.path = path
self.shuffle = shuffle
# char feature
self.pad_char = [char_pad, char_pad]
# self.pad_char = []
self.max_char_len = config.max_char_len
示例9: dataLoader
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def dataLoader(self):
"""
:return:
"""
path = self.path
shuffle = self.shuffle
assert isinstance(path, list), "Path Must Be In List"
print("Data Path {}".format(path))
for id_data in range(len(path)):
print("Loading Data Form {}".format(path[id_data]))
insts = self._Load_Each_Data(path=path[id_data], shuffle=shuffle)
random.shuffle(insts)
self._write_shuffle_inst_to_file(insts, path=path[id_data])
self.data_list.append(insts)
# return train/dev/test data
if len(self.data_list) == 3:
return self.data_list[0], self.data_list[1], self.data_list[2]
elif len(self.data_list) == 2:
return self.data_list[0], self.data_list[1]
示例10: get_loader_single
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def get_loader_single(data_name, split, root, json, vocab, transform,
batch_size=100, shuffle=True,
num_workers=2, ids=None, collate_fn=collate_fn):
"""Returns torch.utils.data.DataLoader for custom coco dataset."""
if 'coco' in data_name:
# COCO custom dataset
dataset = CocoDataset(root=root,
json=json,
vocab=vocab,
transform=transform, ids=ids)
elif 'f8k' in data_name or 'f30k' in data_name:
dataset = FlickrDataset(root=root,
split=split,
json=json,
vocab=vocab,
transform=transform)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
pin_memory=True,
num_workers=num_workers,
collate_fn=collate_fn)
return data_loader
示例11: load_data_fashion_mnist
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def load_data_fashion_mnist(batch_size, resize=None, root='./data'):
"""Download the fashion mnist dataset and then load into memory."""
trans = []
if resize:
trans.append(torchvision.transforms.Resize(size=resize))
trans.append(torchvision.transforms.ToTensor())
transform = torchvision.transforms.Compose(trans)
mnist_train = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=transform)
mnist_test = torchvision.datasets.FashionMNIST(root=root, train=False, download=True, transform=transform)
if sys.platform.startswith('win'):
num_workers = 0 # 0表示不用额外的进程来加速读取数据
else:
num_workers = 4
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=num_workers)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False, num_workers=num_workers)
return train_iter, test_iter
示例12: data_iter_random
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def data_iter_random(corpus_indices, batch_size, num_steps, device=None):
# 减1是因为输出的索引x是相应输入的索引y加1
num_examples = (len(corpus_indices) - 1) // num_steps
epoch_size = num_examples // batch_size
example_indices = list(range(num_examples))
random.shuffle(example_indices)
# 返回从pos开始的长为num_steps的序列
def _data(pos):
return corpus_indices[pos: pos + num_steps]
if device is None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for i in range(epoch_size):
# 每次读取batch_size个随机样本
i = i * batch_size
batch_indices = example_indices[i: i + batch_size]
X = [_data(j * num_steps) for j in batch_indices]
Y = [_data(j * num_steps + 1) for j in batch_indices]
yield torch.tensor(X, dtype=torch.float32, device=device), torch.tensor(Y, dtype=torch.float32, device=device)
示例13: data_iter_random
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def data_iter_random(corpus_indices, batch_size, num_steps, device=None):
# 减1是因为输出的索引x是相应输入的索引y加1
num_examples = (len(corpus_indices) - 1) // num_steps
epoch_size = num_examples // batch_size
example_indices = list(range(num_examples))
random.shuffle(example_indices)
# 返回从pos开始的长为num_steps的序列
def _data(pos):
return corpus_indices[pos: pos + num_steps]
if device is None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for i in range(epoch_size):
# 每次读取batch_size个随机样本
i = i * batch_size
batch_indices = example_indices[i: i + batch_size]
X = [_data(j * num_steps) for j in batch_indices]
Y = [_data(j * num_steps + 1) for j in batch_indices]
yield torch.tensor(X, dtype=torch.float32, device=device), torch.tensor(Y, dtype=torch.float32, device=device)
示例14: move
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def move(self, coordinates):
"""
Move coordinates.
:Parameters:
#. coordinates (np.ndarray): The coordinates on which to apply
the transformation.
:Returns:
#. coordinates (np.ndarray): The new coordinates after applying
the transformation.
"""
indexes = range(len(self.__combination))
if self.__shuffle:
shuffle( indexes )
# create the move combination
for idx in indexes:
coordinates = self.__combination[idx].move(coordinates)
return coordinates
示例15: _load_image_set_index
# 需要导入模块: import random [as 别名]
# 或者: from random import shuffle [as 别名]
def _load_image_set_index(self, shuffle):
"""
get total number of images, init indices
Parameters
----------
shuffle : bool
whether to shuffle the initial indices
"""
self.num_images = 0
for db in self.imdbs:
self.num_images += db.num_images
indices = list(range(self.num_images))
if shuffle:
random.shuffle(indices)
return indices