def _max_pool_grad_grad(dy, x, y, ksize, strides, padding, argmax=None):
"""Gradients of MaxPoolGrad."""
if argmax is None:
_, argmax = tf.nn.max_pool_with_argmax(x, ksize, strides, padding)
grad = dy
grad_flat = tf.reshape(grad, [-1])
argmax_flat = tf.reshape(argmax, [-1])
x_shape = tf.cast(tf.shape(x), argmax.dtype)
batch_dim = tf.reshape(
tf.range(
x_shape[0], dtype=argmax.dtype), [-1, 1, 1, 1])
nelem = tf.reduce_prod(x_shape[1:])
batch_dim *= nelem
y_zero = tf.zeros_like(y, dtype=argmax.dtype)
batch_dim += y_zero
batch_dim = tf.reshape(batch_dim, [-1])
argmax_flat += batch_dim
grad_input = tf.gather(grad_flat, argmax_flat)
grad_input = tf.reshape(grad_input, tf.shape(y))
return grad_input
python类gather()的实例源码
def ternary_decoder(encoded_data, scaler, shape):
"""Decoding the signs to float format """
a = tf.cast(encoded_data, tf.int32)
a_split1 = tf.mod(a,4)
a_split2 = tf.to_int32(tf.mod(a/4,4))
a_split3 = tf.to_int32(tf.mod(a/16,4))
a_split4 = tf.to_int32(tf.mod(a/64,4))
a = tf.concat([a_split1, a_split2, a_split3, a_split4], 0)
real_size = tf.reduce_prod(shape)
a = tf.to_float(a)
a = tf.gather(a, tf.range(0,real_size))
a = tf.reshape(a, shape)
a = tf.subtract(a,1)
decoded = a*scaler
return decoded
def bboxes_nms(scores, bboxes, nms_threshold=0.5, keep_top_k=200, scope=None):
"""Apply non-maximum selection to bounding boxes. In comparison to TF
implementation, use classes information for matching.
Should only be used on single-entries. Use batch version otherwise.
Args:
scores: N Tensor containing float scores.
bboxes: N x 4 Tensor containing boxes coordinates.
nms_threshold: Matching threshold in NMS algorithm;
keep_top_k: Number of total object to keep after NMS.
Return:
classes, scores, bboxes Tensors, sorted by score.
Padded with zero if necessary.
"""
with tf.name_scope(scope, 'bboxes_nms_single', [scores, bboxes]):
# Apply NMS algorithm.
idxes = tf.image.non_max_suppression(bboxes, scores,
keep_top_k, nms_threshold)
scores = tf.gather(scores, idxes)
bboxes = tf.gather(bboxes, idxes)
# Pad results.
scores = tfe_tensors.pad_axis(scores, 0, keep_top_k, axis=0)
bboxes = tfe_tensors.pad_axis(bboxes, 0, keep_top_k, axis=0)
return scores, bboxes
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def combinations(s_data, subset_size, total_size=None, name=None):
assert isinstance(subset_size, int)
assert subset_size > 0
if total_size is None:
total_size = s_data.get_shape().as_list()[0]
if total_size is None:
raise ValueError(
"tensor size on axis 0 is unknown,"
" please supply 'total_size'")
else:
assert isinstance(total_size, int)
assert subset_size <= total_size
c_combs = tf.constant(
list(itertools.combinations(range(total_size), subset_size)),
dtype=hparams.INTX,
name=('combs' if name is None else name))
return tf.gather(s_data, c_combs)
def calc (self, xs):
xs = tf.transpose(xs, [1, 0, 2])
print "xs: " + str(xs)
mlp_out = []
for i in range(self.lstm_steps_count):
v = self.mlp (tf.gather(xs, i))
mlp_out.append (v)
mlp_out = tf.transpose(tf.pack (mlp_out), [1, 0, 2])
val, state = tf.nn.dynamic_rnn(tf.nn.rnn_cell.MultiRNNCell(self.layers, state_is_tuple=True), mlp_out, dtype=tf.float32)
val = tf.transpose(val, [1, 0, 2])
results = []
for i in range(self.lstm_steps_count):
v = self.out_mlp (tf.gather(val, i))
results.append (v)
return tf.transpose(tf.pack (results), [1, 0, 2])
def sparse_hermitian_product(emb, tuples):
"""
Compute the Hermitian inner product between selected complex embeddings
This corresponds to the usual dot product applied on the conjugate of the first vector: <conj(x), y>
where conj is the complex conjugate (obtained by inverting the imaginary part)
We consider that the embedding dimension is twice the rank, where the first part is in embeddings[:,:rk] and
the imaginary part is in embeddings[:,rk:].
It computes
S[i] = <conj(E[I[i,1]], E[I[i,2]]>
Usage:
S = sparse_hermitian_product(E, I):
:param emb: embedding matrix of size [n_emb, 2 * r] containing float numbers where r is the complex rank
:param tuples: tuple matrix of size [n_t, 2] containing integers that correspond to the indices of the embeddings
:return: a pair containing the real and imaginary parts of the Hermitian dot products
"""
rk = emb.get_shape()[1].value // 2
emb_re = emb[:, :rk]
emb_im = emb[:, rk:]
emb_sel_a_re = tf.gather(emb_re, tuples[:, 0])
emb_sel_a_im = tf.gather(emb_im, tuples[:, 0])
emb_sel_b_re = tf.gather(emb_re, tuples[:, 1])
emb_sel_b_im = tf.gather(emb_im, tuples[:, 1])
pred_re = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_re) + tf.mul(emb_sel_a_im, emb_sel_b_im), 1)
pred_im = tf.reduce_sum(tf.mul(emb_sel_a_re, emb_sel_b_im) - tf.mul(emb_sel_a_im, emb_sel_b_re), 1)
return pred_re, pred_im
def test_matrix_factorization(verbose=False):
np.random.seed(1)
n, m, rank = 7, 6, 3
mat = np.random.randn(n, rank).dot(np.random.randn(rank, m))
tuples = [([i, n + j], mat[i, j]) for i in range(n) for j in range(m)]
tuple_iterable = data_to_batches(tuples, minibatch_size=n * m)
sampler, (x, y) = feed_dict_sampler(tuple_iterable, types=[np.int64, np.float32])
emb_var = tf.Variable(tf.cast(np.random.randn(n + m, rank), 'float32'))
offset = tf.Variable(tf.cast(1.0, 'float32'))
loss_op = tf.reduce_mean(tf.square(tf.reduce_sum(tf.reduce_prod(tf.gather(emb_var, x), 1), 1) + offset - y))
emb, offset_val = learn(loss_op, sampler, max_epochs=200, variables=[emb_var, offset])
mat_est = emb[:n, :].dot(emb[n:, :].T)
if verbose:
print(np.linalg.norm(mat_est - mat) ** 2) # we should have recovered the low-rank matrix
else:
assert (np.linalg.norm(mat_est - mat) < 1e-3)
def reader(self, context=None, emb0=None):
if emb0 is None: # by default, can use the initial embedding
emb0 = self.emb0
if context is None: # empty contexts are not read
return emb0
context_inputs, context_ouputs = context
context_embs = tf.gather(emb0, context_inputs[:, :, 1])
preds = tf.reshape(tf.matmul(
tf.reshape(context_embs, (self.n_data * self.n_features, self.rank)),
tf.reshape(emb0[0, :], [self.rank, 1])),
(self.n_data, self.n_features))
update_strength = tf.tile(tf.reshape(loss_quadratic_grad(preds, context_ouputs),
(self.n_data, self.n_features, 1)), (1, 1, self.rank))
embs_after_reading = tf.tile(tf.reshape(emb0[0, :], (1, self.rank)), (self.n_data, 1)) \
- tf.reduce_sum(context_embs * update_strength, 1) * self.step_size
return embs_after_reading # size of the output: (n_data, rank)
def lookup_last_idx(a, inds, name=None):
"""
Looks up indices in a. e.g. a[[1, 2, 3]] = [a[1], a[2], a[3]]
a is a d1 x d2 ... dn tensor
inds is a d1 x d2 ... d(n-1) tensor of integers
returns the tensor
out[i_1,...,i_{n-1}] = a[i_1,...,i_{n-1}, inds[i_1,...,i_{n-1}]]
"""
with tf.op_scope([a, inds], name, 'lookup_last_idx') as scope:
a = tf.convert_to_tensor(a, name='a')
inds = tf.convert_to_tensor(inds, name='inds')
# Flatten the arrays
ashape, indsshape = tf.shape(a), tf.shape(inds)
aflat, indsflat = tf.reshape(a, [-1]), tf.reshape(inds, [-1])
# Compute the indices corresponding to inds in the flattened array
# TODO Causes UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape.
delta = tf.gather(ashape, tf.size(ashape) - 1) # i.e. delta = ashape[-1],
aflatinds = tf.range(0, limit=tf.size(a), delta=delta) + indsflat
# Look up the desired elements in the flattened array, and reshape
# to the original shape
return tf.reshape(tf.gather(aflat, aflatinds), indsshape, name=scope)
def batch_gather(tensor, indices):
"""Gather in batch from a tensor of arbitrary size.
In pseduocode this module will produce the following:
output[i] = tf.gather(tensor[i], indices[i])
Args:
tensor: Tensor of arbitrary size.
indices: Vector of indices.
Returns:
output: A tensor of gathered values.
"""
shape = get_shape(tensor)
flat_first = tf.reshape(tensor, [shape[0] * shape[1]] + shape[2:])
indices = tf.convert_to_tensor(indices)
offset_shape = [shape[0]] + [1] * (indices.shape.ndims - 1)
offset = tf.reshape(tf.range(shape[0]) * shape[1], offset_shape)
output = tf.gather(flat_first, indices + offset)
return output
def assign_boxes(gt_boxes, tensors, layers, scope='AssignGTBoxes'):
with tf.name_scope(scope) as sc:
min_k = layers[0]
max_k = layers[-1]
assigned_layers = \
tf.py_func(assign.assign_boxes,
[ gt_boxes, min_k, max_k ],
tf.int32)
assigned_layers = tf.reshape(assigned_layers, [-1])
assigned_tensors = []
for t in tensors:
split_tensors = []
for l in layers:
tf.cast(l, tf.int32)
inds = tf.where(tf.equal(assigned_layers, l))
inds = tf.reshape(inds, [-1])
split_tensors.append(tf.gather(t, inds))
assigned_tensors.append(split_tensors)
return assigned_tensors + [assigned_layers]
def slice_2d(x, inds0, inds1):
# assume that a path have 1000 vector, then ncols=action dims, inds0=1000,inds1=
inds0 = tf.cast(inds0, tf.int64)
inds1 = tf.cast(inds1, tf.int64)
shape = tf.cast(tf.shape(x), tf.int64)
ncols = shape[1]
x_flat = tf.reshape(x, [-1])
return tf.gather(x_flat, inds0 * ncols + inds1)
# def linesearch(f, x, fullstep, expected_improve_rate):
# accept_ratio = .1
# max_backtracks = 10
# fval, old_kl, entropy = f(x)
# for (_n_backtracks, stepfrac) in enumerate(.5**np.arange(max_backtracks)):
# xnew = x + stepfrac * fullstep
# newfval, new_kl, new_ent= f(xnew)
# # actual_improve = newfval - fval # minimize target object
# # expected_improve = expected_improve_rate * stepfrac
# # ratio = actual_improve / expected_improve
# # if ratio > accept_ratio and actual_improve > 0:
# # return xnew
# if newfval<fval and new_kl<=pms.max_kl:
# return xnew
# return x
def _sample(self, *params, **kwargs):
""" Returns the mean of the most probable Gaussian distribution """
# Get identifier of the most probable component
mixings, sigma, mean = params
batch_size = mixings.get_shape()[0]
id_mix = tf.cast(tf.argmax(mixings, axis=1), tf.int32)
# Extracted from https://github.com/tensorflow/tensorflow/issues/418
# Get mean of corresponding component
sample = tf.gather(
params=tf.reshape(mean, [-1]),
indices=tf.range(batch_size) * tf.shape(mean)[1] + id_mix
)
# Small workaround
if sample.get_shape().ndims < 2:
sample = tf.expand_dims(sample, axis=1)
return sample
def build_eval_graph(self):
"""Build the eval graph."""
# Eval graph
# Normalized word embeddings of shape [vocab_size, emb_dim].
nemb = tf.nn.l2_normalize(self._emb, 1)
# Nodes for computing neighbors for a given word according to
# their cosine distance.
nearby_word = tf.placeholder(dtype=tf.int32) # word id
nearby_emb = tf.gather(nemb, nearby_word)
nearby_dist = tf.matmul(nearby_emb, nemb, transpose_b=True)
nearby_val, nearby_idx = tf.nn.top_k(nearby_dist,
min(1000, self._options.vocab_size))
self._nearby_word = nearby_word
self._nearby_val = nearby_val
self._nearby_idx = nearby_idx
def _slice(self, X, X2):
"""
Slice the correct dimensions for use in the kernel, as indicated by
`self.active_dims`.
:param X: Input 1 (NxD).
:param X2: Input 2 (MxD), may be None.
:return: Sliced X, X2, (Nxself.input_dim).
"""
if isinstance(self.active_dims, slice):
X = X[:, self.active_dims]
if X2 is not None:
X2 = X2[:, self.active_dims]
else:
X = tf.transpose(tf.gather(tf.transpose(X), self.active_dims))
if X2 is not None:
X2 = tf.transpose(tf.gather(tf.transpose(X2), self.active_dims))
input_dim_shape = tf.shape(X)[1]
input_dim = tf.convert_to_tensor(self.input_dim, dtype=settings.tf_int)
with tf.control_dependencies([tf.assert_equal(input_dim_shape, input_dim)]):
X = tf.identity(X)
return X, X2
def _slice_cov(self, cov):
"""
Slice the correct dimensions for use in the kernel, as indicated by
`self.active_dims` for covariance matrices. This requires slicing the
rows *and* columns. This will also turn flattened diagonal
matrices into a tensor of full diagonal matrices.
:param cov: Tensor of covariance matrices (NxDxD or NxD).
:return: N x self.input_dim x self.input_dim.
"""
cov = tf.cond(tf.equal(tf.rank(cov), 2), lambda: tf.matrix_diag(cov), lambda: cov)
if isinstance(self.active_dims, slice):
cov = cov[..., self.active_dims, self.active_dims]
else:
cov_shape = tf.shape(cov)
covr = tf.reshape(cov, [-1, cov_shape[-1], cov_shape[-1]])
gather1 = tf.gather(tf.transpose(covr, [2, 1, 0]), self.active_dims)
gather2 = tf.gather(tf.transpose(gather1, [1, 0, 2]), self.active_dims)
cov = tf.reshape(tf.transpose(gather2, [2, 0, 1]),
tf.concat([cov_shape[:-2], [len(self.active_dims), len(self.active_dims)]], 0))
return cov
def bboxes_nms(scores, bboxes, nms_threshold=0.5, keep_top_k=200, scope=None):
"""Apply non-maximum selection to bounding boxes. In comparison to TF
implementation, use classes information for matching.
Should only be used on single-entries. Use batch version otherwise.
Args:
scores: N Tensor containing float scores.
bboxes: N x 4 Tensor containing boxes coordinates.
nms_threshold: Matching threshold in NMS algorithm;
keep_top_k: Number of total object to keep after NMS.
Return:
classes, scores, bboxes Tensors, sorted by score.
Padded with zero if necessary.
"""
with tf.name_scope(scope, 'bboxes_nms_single', [scores, bboxes]):
# Apply NMS algorithm.
idxes = tf.image.non_max_suppression(bboxes, scores,
keep_top_k, nms_threshold)
scores = tf.gather(scores, idxes)
bboxes = tf.gather(bboxes, idxes)
# Pad results.
scores = tfe_tensors.pad_axis(scores, 0, keep_top_k, axis=0)
bboxes = tfe_tensors.pad_axis(bboxes, 0, keep_top_k, axis=0)
return scores, bboxes
def _precision_recall(n_gbboxes, n_detections, scores, tp, fp, scope=None):
"""Compute precision and recall from scores, true positives and false
positives booleans arrays
"""
# Sort by score.
with tf.name_scope(scope, 'prec_rec', [n_gbboxes, scores, tp, fp]):
# Sort detections by score.
scores, idxes = tf.nn.top_k(scores, k=n_detections, sorted=True)
tp = tf.gather(tp, idxes)
fp = tf.gather(fp, idxes)
# Computer recall and precision.
dtype = tf.float64
tp = tf.cumsum(tf.cast(tp, dtype), axis=0)
fp = tf.cumsum(tf.cast(fp, dtype), axis=0)
recall = _safe_div(tp, tf.cast(n_gbboxes, dtype), 'recall')
precision = _safe_div(tp, tp + fp, 'precision')
return tf.tuple([precision, recall])
def scheduled_sample(ground_truth_x, generated_x, batch_size, num_ground_truth):
"""Sample batch with specified mix of ground truth and generated data_files points.
Args:
ground_truth_x: tensor of ground-truth data_files points.
generated_x: tensor of generated data_files points.
batch_size: batch size
num_ground_truth: number of ground-truth examples to include in batch.
Returns:
New batch with num_ground_truth sampled from ground_truth_x and the rest
from generated_x.
"""
idx = tf.random_shuffle(tf.range(int(batch_size)))
ground_truth_idx = tf.gather(idx, tf.range(num_ground_truth))
generated_idx = tf.gather(idx, tf.range(num_ground_truth, int(batch_size)))
ground_truth_examps = tf.gather(ground_truth_x, ground_truth_idx)
generated_examps = tf.gather(generated_x, generated_idx)
return tf.dynamic_stitch([ground_truth_idx, generated_idx],
[ground_truth_examps, generated_examps])