本文整理汇总了Python中six.moves.xrange方法的典型用法代码示例。如果您正苦于以下问题:Python moves.xrange方法的具体用法?Python moves.xrange怎么用?Python moves.xrange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves
的用法示例。
在下文中一共展示了moves.xrange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_images
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def load_images(input_dir, metadata_file_path, batch_shape):
"""Retrieve numpy arrays of images and labels, read from a directory."""
num_images = batch_shape[0]
with open(metadata_file_path) as input_file:
reader = csv.reader(input_file)
header_row = next(reader)
rows = list(reader)
row_idx_image_id = header_row.index('ImageId')
row_idx_true_label = header_row.index('TrueLabel')
images = np.zeros(batch_shape)
labels = np.zeros(num_images, dtype=np.int32)
for idx in xrange(num_images):
row = rows[idx]
filepath = os.path.join(input_dir, row[row_idx_image_id] + '.png')
with tf.gfile.Open(filepath, 'rb') as f:
image = np.array(
Image.open(f).convert('RGB')).astype(np.float) / 255.0
images[idx, :, :, :] = image
labels[idx] = int(row[row_idx_true_label])
return images, labels
示例2: jacobian_graph
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def jacobian_graph(predictions, x, nb_classes):
"""
Create the Jacobian graph to be ran later in a TF session
:param predictions: the model's symbolic output (linear output,
pre-softmax)
:param x: the input placeholder
:param nb_classes: the number of classes the model has
:return:
"""
# This function will return a list of TF gradients
list_derivatives = []
# Define the TF graph elements to compute our derivatives for each class
for class_ind in xrange(nb_classes):
derivatives, = tf.gradients(predictions[:, class_ind], x)
list_derivatives.append(derivatives)
return list_derivatives
示例3: topsort
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def topsort(nodes):
n = len(nodes)
deg = [0]*n
g = [[] for _ in xrange(n)]
for i,node in enumerate(nodes):
if 'inputs' in node:
for j in node['inputs']:
deg[i] += 1
g[j[0]].append(i)
from collections import deque
q = deque([i for i in xrange(n) if deg[i]==0])
res = []
for its in xrange(n):
i = q.popleft()
res.append(nodes[i])
for j in g[i]:
deg[j] -= 1
if deg[j] == 0:
q.append(j)
new_ids=dict([(node['name'],i) for i,node in enumerate(res)])
for node in res:
if 'inputs' in node:
for j in node['inputs']:
j[0]=new_ids[nodes[j[0]]['name']]
return res
示例4: run_scenario
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def run_scenario(result_text, rounds, num_requests, num_chunks, parallel_lz4,
use_async, async_arctic_pool_workers=None):
aclz4.enable_parallel_lz4(parallel_lz4)
if async_arctic_pool_workers is not None:
ASYNC_ARCTIC.reset(pool_size=int(async_arctic_pool_workers), timeout=10)
measurements = []
for curr_round in xrange(rounds):
# print("Running round {}".format(curr_round))
clean_lib()
start = time.time()
if use_async:
async_bench(num_requests, num_chunks)
else:
serial_bench(num_requests, num_chunks)
measurements.append(time.time() - start)
print("{}: async={}, chunks/write={}, writes/round={}, rounds={}, "
"parallel_lz4={}, async_arctic_pool_workers={}: {}".format(
result_text, use_async, num_chunks, num_requests, rounds, parallel_lz4, async_arctic_pool_workers,
["{:.3f}".format(x) for x in get_stats(measurements[1:] if len(measurements) > 1 else measurements)]))
示例5: train
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def train(self, dataset):
log.infov("Training Starts!")
pprint(self.batch_train)
max_steps = 2500000
output_save_step = 1000
for s in xrange(max_steps):
step, summary, loss, loss_pair, loss_unpair, step_time = \
self.run_single_step(self.batch_train, dataset, step=s, is_train=True)
if s % 10 == 0:
self.log_step_message(step, loss, loss_pair, loss_unpair, step_time)
self.summary_writer.add_summary(summary, global_step=step)
if s % output_save_step == 0:
log.infov("Saved checkpoint at %d", s)
save_path = self.saver.save(self.session,
os.path.join(self.train_dir, 'model'),
global_step=step)
示例6: sparse_tuple_from
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def sparse_tuple_from(sequences, dtype=np.int32):
"""Create a sparse representention of x.
Args:
sequences: a list of lists of type dtype where each element is a sequence
Returns:
A tuple with (indices, values, shape)
"""
indices = []
values = []
for n, seq in enumerate(sequences):
indices.extend(zip([n]*len(seq), range(len(seq))))
values.extend(seq)
indices = np.asarray(indices, dtype=np.int64)
values = np.asarray(values, dtype=dtype)
shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1]+1], dtype=np.int64)
return indices, values, shape
示例7: _step_and_skip
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def _step_and_skip(self, actions):
# TODO - allow for goal reward substitution for multi-goal envs
if self.frameskip is None:
# Frames kipping is unset or set as env property.
return self.gym_env.step(actions)
else:
# Do frameskip loop in our wrapper class.
step_reward = 0.0
terminal = None
info = None
for i in range_(self.frameskip):
state, reward, terminal, info = self.gym_env.step(actions)
if i == self.frameskip - 2:
self.state_buffer[0] = state
if i == self.frameskip - 1:
self.state_buffer[1] = state
step_reward += reward
if terminal:
break
max_frame = self.state_buffer.max(axis=0)
return max_frame, step_reward, terminal, info
示例8: render_txt
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def render_txt(self):
actor = "X"
if self.action_type == "ftj":
actor = "^" if self.orientation == 0 else ">" if self.orientation == 90 else "v" if \
self.orientation == 180 else "<"
# paints itself
txt = ""
for row in range_(len(self.world)):
for col, val in enumerate(self.world[row]):
if self.x == col and self.y == row:
txt += actor
else:
txt += val
txt += "\n"
txt += "\n"
return txt
示例9: update_cam_pixels
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def update_cam_pixels(self):
# Init camera?
if self.camera_pixels is None:
self.camera_pixels = np.zeros(shape=(self.n_row, self.n_col, 3), dtype=np.int32)
self.camera_pixels[:, :, :] = 0 # reset everything
# 1st channel -> Walls (127) and goal (255).
# 2nd channel -> Dangers (fire=127, holes=255)
# 3rd channel -> Actor position (255).
for row in range_(self.n_row):
for col in range_(self.n_col):
field = self.world[row, col]
if field == "F":
self.camera_pixels[row, col, 0] = 127
elif field == "H":
self.camera_pixels[row, col, 0] = 255
elif field == "W":
self.camera_pixels[row, col, 1] = 127
elif field == "G":
self.camera_pixels[row, col, 1] = 255 # will this work (goal==2x wall)?
# Overwrite player's position.
self.camera_pixels[self.y, self.x, 2] = 255
示例10: test_complex_space_sampling_and_check_via_contains
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def test_complex_space_sampling_and_check_via_contains(self):
"""
Tests a complex Space on sampling and `contains` functionality.
"""
space = Dict(
a=dict(aa=float, ab=bool),
b=dict(ba=float),
c=float,
d=IntBox(low=0, high=1),
e=IntBox(5),
f=FloatBox(shape=(2, 2)),
g=Tuple(float, FloatBox(shape=())),
add_batch_rank=True
)
samples = space.sample(size=100, horizontal=True)
for i in range_(len(samples)):
self.assertTrue(space.contains(samples[i]))
示例11: _apply_gradients
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def _apply_gradients(self, grads, x, optim_state):
new_x = [None] * len(x)
for i in xrange(len(x)):
new_x[i] = x[i] - self._lr * grads[i]
return new_x, optim_state
示例12: random_targets
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def random_targets(gt, nb_classes):
"""
Take in an array of correct labels and randomly select a different label
for each label in the array. This is typically used to randomly select a
target class in targeted adversarial examples attacks (i.e., when the
search algorithm takes in both a source class and target class to compute
the adversarial example).
:param gt: the ground truth (correct) labels. They can be provided as a
1D vector or 2D array of one-hot encoded labels.
:param nb_classes: The number of classes for this task. The random class
will be chosen between 0 and nb_classes such that it
is different from the correct class.
:return: A numpy array holding the randomly-selected target classes
encoded as one-hot labels.
"""
# If the ground truth labels are encoded as one-hot, convert to labels.
if len(gt.shape) == 2:
gt = np.argmax(gt, axis=1)
# This vector will hold the randomly selected labels.
result = np.zeros(gt.shape, dtype=np.int32)
for class_ind in xrange(nb_classes):
# Compute all indices in that class.
in_cl = gt == class_ind
size = np.sum(in_cl)
# Compute the set of potential targets for this class.
potential_targets = other_classes(nb_classes, class_ind)
# Draw with replacement random targets among the potential targets.
result[in_cl] = np.random.choice(potential_targets, size=size)
# Encode vector of random labels as one-hot labels.
result = to_categorical(result, nb_classes)
result = result.astype(np.int32)
return result
示例13: grid_visual
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def grid_visual(data):
"""
This function displays a grid of images to show full misclassification
:param data: grid data of the form;
[nb_classes : nb_classes : img_rows : img_cols : nb_channels]
:return: if necessary, the matplot figure to reuse
"""
import matplotlib.pyplot as plt
# Ensure interactive mode is disabled and initialize our graph
plt.ioff()
figure = plt.figure()
figure.canvas.set_window_title('Cleverhans: Grid Visualization')
# Add the images to the plot
num_cols = data.shape[0]
num_rows = data.shape[1]
num_channels = data.shape[4]
current_row = 0
for y in xrange(num_rows):
for x in xrange(num_cols):
figure.add_subplot(num_rows, num_cols, (x + 1) + (y * num_cols))
plt.axis('off')
if num_channels == 1:
plt.imshow(data[x, y, :, :, 0], cmap='gray')
else:
plt.imshow(data[x, y, :, :, :])
# Draw the plot and return
plt.show()
return figure
示例14: clip_eta
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def clip_eta(eta, ord, eps):
"""
Helper function to clip the perturbation to epsilon norm ball.
:param eta: A tensor with the current perturbation.
:param ord: Order of the norm (mimics Numpy).
Possible values: np.inf, 1 or 2.
:param eps: Epilson, bound of the perturbation.
"""
# Clipping perturbation eta to self.ord norm ball
if ord not in [np.inf, 1, 2]:
raise ValueError('ord must be np.inf, 1, or 2.')
reduc_ind = list(xrange(1, len(eta.get_shape())))
avoid_zero_div = 1e-12
if ord == np.inf:
eta = tf.clip_by_value(eta, -eps, eps)
else:
if ord == 1:
norm = tf.maximum(avoid_zero_div,
reduce_sum(tf.abs(eta),
reduc_ind, keepdims=True))
elif ord == 2:
# avoid_zero_div must go inside sqrt to avoid a divide by zero
# in the gradient through this operation
norm = tf.sqrt(tf.maximum(avoid_zero_div,
reduce_sum(tf.square(eta),
reduc_ind,
keepdims=True)))
# We must *clip* to within the norm ball, not *normalize* onto the
# surface of the ball
factor = tf.minimum(1., eps / norm)
eta = eta * factor
return eta
示例15: _FillBucketInputQueue
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import xrange [as 别名]
def _FillBucketInputQueue(self):
"""Fill bucketed batches into the bucket_input_queue."""
while True:
inputs = []
for _ in xrange(self._hps.batch_size * BUCKET_CACHE_BATCH):
inputs.append(self._input_queue.get())
if self._bucketing:
inputs = sorted(inputs, key=lambda inp: inp.enc_len)
batches = []
for i in xrange(0, len(inputs), self._hps.batch_size):
batches.append(inputs[i:i+self._hps.batch_size])
shuffle(batches)
for b in batches:
self._bucket_input_queue.put(b)