def any(x, axis=None, keepdims=False):
return tf.reduce_any(x, axis=axis, keep_dims=keepdims)
python类reduce_any()的实例源码
def any(x, axis=None, keepdims=False):
'''Bitwise reduction (logical OR).
Returns an uint8 tensor (0s and 1s).
'''
axis = _normalize_axis(axis, ndim(x))
x = tf.cast(x, tf.bool)
x = tf.reduce_any(x, reduction_indices=axis, keep_dims=keepdims)
return tf.cast(x, tf.uint8)
def test_Any(self):
t = tf.reduce_any(self.random(3, 4, 5), reduction_indices=[0, 1], keep_dims=True)
self.check(t)
if td._tf_version[:3] >= (0, 12, 0):
t = tf.reduce_any(self.random(3, 4, 5), axis=[0, 1], keep_dims=True)
self.check(t)
#
# segmentation
#
def prune_outside_window(boxlist, window, scope=None):
"""Prunes bounding boxes that fall outside a given window.
This function prunes bounding boxes that even partially fall outside the given
window. See also clip_to_window which only prunes bounding boxes that fall
completely outside the window, and clips any bounding boxes that partially
overflow.
Args:
boxlist: a BoxList holding M_in boxes.
window: a float tensor of shape [4] representing [ymin, xmin, ymax, xmax]
of the window
scope: name scope.
Returns:
pruned_corners: a tensor with shape [M_out, 4] where M_out <= M_in
valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes
in the input tensor.
"""
with tf.name_scope(scope, 'PruneOutsideWindow'):
y_min, x_min, y_max, x_max = tf.split(
value=boxlist.get(), num_or_size_splits=4, axis=1)
win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window)
coordinate_violations = tf.concat([
tf.less(y_min, win_y_min), tf.less(x_min, win_x_min),
tf.greater(y_max, win_y_max), tf.greater(x_max, win_x_max)
], 1)
valid_indices = tf.reshape(
tf.where(tf.logical_not(tf.reduce_any(coordinate_violations, 1))), [-1])
return gather(boxlist, valid_indices), valid_indices
def prune_completely_outside_window(boxlist, window, scope=None):
"""Prunes bounding boxes that fall completely outside of the given window.
The function clip_to_window prunes bounding boxes that fall
completely outside the window, but also clips any bounding boxes that
partially overflow. This function does not clip partially overflowing boxes.
Args:
boxlist: a BoxList holding M_in boxes.
window: a float tensor of shape [4] representing [ymin, xmin, ymax, xmax]
of the window
scope: name scope.
Returns:
pruned_corners: a tensor with shape [M_out, 4] where M_out <= M_in
valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes
in the input tensor.
"""
with tf.name_scope(scope, 'PruneCompleteleyOutsideWindow'):
y_min, x_min, y_max, x_max = tf.split(
value=boxlist.get(), num_or_size_splits=4, axis=1)
win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window)
coordinate_violations = tf.concat([
tf.greater_equal(y_min, win_y_max), tf.greater_equal(x_min, win_x_max),
tf.less_equal(y_max, win_y_min), tf.less_equal(x_max, win_x_min)
], 1)
valid_indices = tf.reshape(
tf.where(tf.logical_not(tf.reduce_any(coordinate_violations, 1))), [-1])
return gather(boxlist, valid_indices), valid_indices
def any(x, axis=None, keepdims=False):
'''Bitwise reduction (logical OR).
Return array of uint8 (0s and 1s).
'''
axis = normalize_axis(axis, ndim(x))
x = tf.cast(x, tf.bool)
x = tf.reduce_any(x, reduction_indices=axis, keep_dims=keepdims)
return tf.cast(x, tf.uint8)
def kMeans(iterations, labelledSet, columnPrefix="cluster"):
X = labelledSet.as_matrix()
start_pos = tf.Variable(X[np.random.randint(X.shape[0], size=iterations),:], dtype=tf.float32)
centroids = tf.Variable(start_pos.initialized_value(), "S", dtype=tf.float32)
points = tf.Variable(X, 'X', dtype=tf.float32)
ones_like = tf.ones((points.get_shape()[0], 1))
prev_assignments = tf.Variable(tf.zeros((points.get_shape()[0], ), dtype=tf.int64))
p1 = tf.matmul(
tf.expand_dims(tf.reduce_sum(tf.square(points), 1), 1),
tf.ones(shape=(1, iterations))
)
p2 = tf.transpose(tf.matmul(
tf.reshape(tf.reduce_sum(tf.square(centroids), 1), shape=[-1, 1]),
ones_like,
transpose_b=True
))
distance = tf.sqrt(tf.add(p1, p2) - 2 * tf.matmul(points, centroids, transpose_b=True))
point_to_centroid_assignment = tf.argmin(distance, axis=1)
total = tf.unsorted_segment_sum(points, point_to_centroid_assignment, iterations)
count = tf.unsorted_segment_sum(ones_like, point_to_centroid_assignment, iterations)
means = total / count
is_continue = tf.reduce_any(tf.not_equal(point_to_centroid_assignment, prev_assignments))
with tf.control_dependencies([is_continue]):
loop = tf.group(centroids.assign(means), prev_assignments.assign(point_to_centroid_assignment))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
has_changed, cnt = True, 0
while has_changed and cnt < 300:
cnt += 1
has_changed, _ = sess.run([is_continue, loop])
res = sess.run(point_to_centroid_assignment)
return pandas.DataFrame(res, columns=[columnPrefix + "_" + str(iterations)])
def split_proposals(proposals, proposals_num, gt, gt_num, iou, scores, cross_boundary_mask):
'''Generate batches from proposals and ground truth boxes
Idea is to drastically reduce number of proposals to evaluate. So, we find those
proposals that have IoU > 0.7 with _any_ ground truth and mark them as positive samples.
Proposals with IoU < 0.3 with _all_ ground truth boxes are considered negative. All
other proposals are discarded.
We generate batch with at most half of examples being positive. We also pad them with negative
have we not enough positive proposals.
proposals: N x 4 tensor
proposal_num: N
gt: M x 4 tensor
gt_num: M
iou: N x M tensor of IoU between every proposal and ground truth
scores: N x 2 tensor with scores object/not-object
cross_boundary_mask: N x 1 Tensor masking out-of-image proposals
'''
# now let's get rid of non-positive and non-negative samples
# Sample is considered positive if it has IoU > 0.7 with _any_ ground truth box
# XXX: maximal IoU ground truth proposal should be treated as positive
positive_mask = tf.reduce_any(tf.greater(iou, 0.7), axis=1) & cross_boundary_mask
# Sample would be considered negative if _all_ ground truch box
# have iou less than 0.3
negative_mask = tf.reduce_all(tf.less(iou, 0.3), axis=1) & cross_boundary_mask
# Select only positive boxes and their corresponding predicted scores
positive_boxes = tf.boolean_mask(proposals, positive_mask)
positive_scores = tf.boolean_mask(scores, positive_mask)
positive_labels = tf.reduce_mean(tf.ones_like(positive_scores), axis=1)
# Same for negative
negative_boxes = tf.boolean_mask(proposals, negative_mask)
negative_scores = tf.boolean_mask(scores, negative_mask)
negative_labels = tf.reduce_mean(tf.zeros_like(negative_scores), axis=1)
return (
(positive_boxes, positive_scores, positive_labels),
(negative_boxes, negative_scores, negative_labels)
)
def merge(tensors_list, mode, axis=1, name='merge', outputs_collections=None, **kwargs):
"""
Merge op
Args:
tensor_list: A list `Tensors` to merge
mode: str, available modes are
['concat', 'elemwise_sum', 'elemwise_mul', 'sum',
'mean', 'prod', 'max', 'min', 'and', 'or']
name: a optional scope/name of the layer
outputs_collections: The collections to which the outputs are added.
Returns:
A `Tensor` representing the results of the repetition operation.
Raises:
ValueError: If 'kernel_size' is not a 2-D list
"""
assert len(tensors_list) > 1, "Merge required 2 or more tensors."
with tf.name_scope(name):
tensors = [l for l in tensors_list]
if mode == 'concat':
output = tf.concat(tensors, axis=axis)
elif mode == 'elemwise_sum':
output = tensors[0]
for i in range(1, len(tensors)):
output = tf.add(output, tensors[i])
elif mode == 'elemwise_mul':
output = tensors[0]
for i in range(1, len(tensors)):
output = tf.multiply(output, tensors[i])
elif mode == 'sum':
output = tf.reduce_sum(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'mean':
output = tf.reduce_mean(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'prod':
output = tf.reduce_prod(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'max':
output = tf.reduce_max(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'min':
output = tf.reduce_min(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'and':
output = tf.reduce_all(tf.concat(tensors, axis=axis), axis=axis)
elif mode == 'or':
output = tf.reduce_any(tf.concat(tensors, axis=axis), axis=axis)
else:
raise Exception("Unknown merge mode", str(mode))
return _collect_named_outputs(outputs_collections, name, output)
return output
def inference_step(self,batch_mask, prob_compare,prob,counter, state, premise, hypothesis, acc_states):
if self.config.keep_prob < 1.0 and self.is_training:
premise = tf.nn.dropout(premise, self.config.keep_prob)
hypothesis = tf.nn.dropout(hypothesis,self.config.keep_prob)
hyp_attn = self.attention(state, hypothesis, "hyp_attn")
state_for_premise = tf.concat(1, [state, hyp_attn])
prem_attn = self.attention(state_for_premise, premise, "prem_attn")
new_state = tf.concat(1, [hyp_attn ,prem_attn])
with tf.variable_scope('sigmoid_activation_for_pondering'):
p = tf.squeeze(tf.sigmoid(tf.nn.rnn_cell._linear(new_state, 1, True)))
new_batch_mask = tf.logical_and(tf.less(prob + p,self.one_minus_eps),batch_mask)
new_float_mask = tf.cast(new_batch_mask, tf.float32)
prob += p * new_float_mask
prob_compare += p * tf.cast(batch_mask, tf.float32)
def use_remainder():
remainder = tf.constant(1.0, tf.float32,[self.batch_size]) - prob
remainder_expanded = tf.expand_dims(remainder,1)
tiled_remainder = tf.tile(remainder_expanded,[1,2*self.rep_size])
acc_state = (new_state * tiled_remainder) + acc_states
return acc_state
def normal():
p_expanded = tf.expand_dims(p * new_float_mask,1)
tiled_p = tf.tile(p_expanded,[1,2*self.rep_size])
acc_state = (new_state * tiled_p) + acc_states
return acc_state
counter += tf.constant(1.0,tf.float32,[self.batch_size]) * new_float_mask
counter_condition = tf.less(counter,self.N)
condition = tf.reduce_any(tf.logical_and(new_batch_mask,counter_condition))
acc_state = tf.cond(condition, normal, use_remainder)
return (new_batch_mask, prob_compare,prob,counter, new_state, premise, hypothesis, acc_state)
def inference_step(self,batch_mask, prob_compare,prob,counter, state, premise, hypothesis, acc_states):
if self.config.keep_prob < 1.0 and self.is_training:
premise = tf.nn.dropout(premise, self.config.keep_prob)
hypothesis = tf.nn.dropout(hypothesis,self.config.keep_prob)
hyp_attn = self.attention(state, hypothesis, "hyp_attn")
state_for_premise = tf.concat(1, [state, hyp_attn])
prem_attn = self.attention(state_for_premise, premise, "prem_attn")
state_for_gates = tf.concat(1, [state, hyp_attn ,prem_attn, prem_attn * hyp_attn])
hyp_gate = self.gate_mechanism(state_for_gates, "hyp_gate")
prem_gate = self.gate_mechanism(state_for_gates, "prem_gate")
input = tf.concat(1, [hyp_gate * hyp_attn, prem_gate * prem_attn])
output, new_state = self.inference_cell(input,state)
with tf.variable_scope('sigmoid_activation_for_pondering'):
p = tf.squeeze(tf.sigmoid(tf.nn.rnn_cell._linear(new_state, 1, True)))
new_batch_mask = tf.logical_and(tf.less(prob + p,self.one_minus_eps),batch_mask)
new_float_mask = tf.cast(new_batch_mask, tf.float32)
prob += p * new_float_mask
prob_compare += p * tf.cast(batch_mask, tf.float32)
def use_remainder():
remainder = tf.constant(1.0, tf.float32,[self.batch_size]) - prob
remainder_expanded = tf.expand_dims(remainder,1)
tiled_remainder = tf.tile(remainder_expanded,[1,self.config.inference_size])
acc_state = (new_state * tiled_remainder) + acc_states
return acc_state
def normal():
p_expanded = tf.expand_dims(p * new_float_mask,1)
tiled_p = tf.tile(p_expanded,[1,self.config.inference_size])
acc_state = (new_state * tiled_p) + acc_states
return acc_state
counter += tf.constant(1.0,tf.float32,[self.batch_size]) * new_float_mask
counter_condition = tf.less(counter,self.N)
condition = tf.reduce_any(tf.logical_and(new_batch_mask,counter_condition))
acc_state = tf.cond(condition, normal, use_remainder)
return (new_batch_mask, prob_compare,prob,counter, new_state, premise, hypothesis, acc_state)
def inference_step(self,batch_mask, prob_compare,prob,counter, state, premise, hypothesis, acc_states):
if self.config.keep_prob < 1.0 and self.is_training:
premise = tf.nn.dropout(premise, self.config.keep_prob)
hypothesis = tf.nn.dropout(hypothesis,self.config.keep_prob)
hyp_attn = self.attention(state, hypothesis, "hyp_attn")
state_for_premise = tf.concat(1, [state, hyp_attn])
prem_attn = self.attention(state_for_premise, premise, "prem_attn")
state_for_gates = tf.concat(1, [state, hyp_attn ,prem_attn, prem_attn * hyp_attn])
hyp_gate = self.gate_mechanism(state_for_gates, "hyp_gate")
prem_gate = self.gate_mechanism(state_for_gates, "prem_gate")
input = tf.concat(1, [hyp_gate * hyp_attn, prem_gate * prem_attn])
output, new_state = self.inference_cell(input,state)
with tf.variable_scope('sigmoid_activation_for_pondering'):
p = tf.squeeze(tf.sigmoid(tf.nn.rnn_cell._linear(new_state, 1, True)))
new_batch_mask = tf.logical_and(tf.less(prob + p,self.one_minus_eps),batch_mask)
new_float_mask = tf.cast(new_batch_mask, tf.float32)
prob += p * new_float_mask
prob_compare += p * tf.cast(batch_mask, tf.float32)
def use_remainder():
remainder = tf.constant(1.0, tf.float32,[self.batch_size]) - prob
remainder_expanded = tf.expand_dims(remainder,1)
tiled_remainder = tf.tile(remainder_expanded,[1,self.config.inference_size])
acc_state = (new_state * tiled_remainder) + acc_states
return acc_state
def normal():
p_expanded = tf.expand_dims(p * new_float_mask,1)
tiled_p = tf.tile(p_expanded,[1,self.config.inference_size])
acc_state = (new_state * tiled_p) + acc_states
return acc_state
counter += tf.constant(1.0,tf.float32,[self.batch_size]) * new_float_mask
counter_condition = tf.less(counter,self.N)
condition = tf.reduce_any(tf.logical_and(new_batch_mask,counter_condition))
acc_state = tf.cond(condition, normal, use_remainder)
return (new_batch_mask, prob_compare,prob,counter, new_state, premise, hypothesis, acc_state)
def do_inference_steps(self, initial_state, premise, hypothesis):
self.one_minus_eps = tf.constant(1.0 - self.config.eps, tf.float32,[self.batch_size])
self.N = tf.constant(self.config.max_computation, tf.float32,[self.batch_size])
prob = tf.constant(0.0,tf.float32,[self.batch_size], name="prob")
prob_compare = tf.constant(0.0,tf.float32,[self.batch_size], name="prob_compare")
counter = tf.constant(0.0, tf.float32,[self.batch_size], name="counter")
i = tf.constant(0, tf.int32, name="index")
acc_states = tf.zeros_like(initial_state, tf.float32, name="state_accumulator")
batch_mask = tf.constant(True, tf.bool,[self.batch_size])
# Tensor arrays to collect information about the run:
array_probs = tf.TensorArray(tf.float32,0, dynamic_size=True)
premise_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
hypothesis_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
incremental_states = tf.TensorArray(tf.float32,0, dynamic_size=True)
# While loop stops when this predicate is FALSE.
# Ie all (probability < 1-eps AND counter < N) are false.
pred = lambda i ,incremental_states, array_probs, premise_attention, hypothesis_attention, batch_mask,prob_compare,prob,\
counter,state,premise, hypothesis ,acc_state:\
tf.reduce_any(
tf.logical_and(
tf.less(prob_compare,self.one_minus_eps),
tf.less(counter,self.N)))
# only stop if all of the batch have passed either threshold
# Do while loop iterations until predicate above is false.
i,incremental_states, array_probs,premise_attention,hypothesis_attention,_,_,remainders,iterations,_,_,_,state = \
tf.while_loop(pred,self.inference_step,
[i,incremental_states, array_probs, premise_attention, hypothesis_attention,
batch_mask,prob_compare,prob,
counter,initial_state,premise, hypothesis, acc_states])
self.ACTPROB = array_probs.pack()
self.ACTPREMISEATTN = premise_attention.pack()
self.ACTHYPOTHESISATTN = hypothesis_attention.pack()
self.incremental_states = incremental_states.pack()
return state, remainders, iterations
def do_inference_steps(self, initial_state, premise, hypothesis):
self.one_minus_eps = tf.constant(1.0 - self.config.eps, tf.float32,[self.batch_size])
self.N = tf.constant(self.config.max_computation, tf.float32,[self.batch_size])
prob = tf.constant(0.0,tf.float32,[self.batch_size], name="prob")
prob_compare = tf.constant(0.0,tf.float32,[self.batch_size], name="prob_compare")
counter = tf.constant(0.0, tf.float32,[self.batch_size], name="counter")
i = tf.constant(0, tf.int32, name="index")
acc_states = tf.zeros_like(initial_state, tf.float32, name="state_accumulator")
batch_mask = tf.constant(True, tf.bool,[self.batch_size])
# Tensor arrays to collect information about the run:
array_probs = tf.TensorArray(tf.float32,0, dynamic_size=True)
premise_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
hypothesis_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
# While loop stops when this predicate is FALSE.
# Ie all (probability < 1-eps AND counter < N) are false.
pred = lambda i ,array_probs, premise_attention, hypothesis_attention, batch_mask,prob_compare,prob,\
counter,state,premise, hypothesis ,acc_state:\
tf.reduce_any(
tf.logical_and(
tf.less(prob_compare,self.one_minus_eps),
tf.less(counter,self.N)))
# only stop if all of the batch have passed either threshold
# Do while loop iterations until predicate above is false.
i,array_probs,premise_attention,hypothesis_attention,_,_,remainders,iterations,_,_,_,state = \
tf.while_loop(pred,self.inference_step,
[i,array_probs, premise_attention, hypothesis_attention,
batch_mask,prob_compare,prob,
counter,initial_state,premise, hypothesis, acc_states])
self.ACTPROB = array_probs.pack()
self.ACTPREMISEATTN = premise_attention.pack()
self.ACTHYPOTHESISATTN = hypothesis_attention.pack()
return state, remainders, iterations
def do_act_steps(self, premise, hypothesis):
self.rep_size = premise.get_shape()[-1].value
self.one_minus_eps = tf.constant(1.0 - self.config.eps, tf.float32,[self.batch_size])
self.N = tf.constant(self.config.max_computation, tf.float32,[self.batch_size])
prob = tf.constant(0.0,tf.float32,[self.batch_size], name="prob")
prob_compare = tf.constant(0.0,tf.float32,[self.batch_size], name="prob_compare")
counter = tf.constant(0.0, tf.float32,[self.batch_size], name="counter")
initial_state = tf.zeros([self.batch_size, 2*self.rep_size], tf.float32, name="state")
i = tf.constant(0, tf.int32, name="index")
acc_states = tf.zeros_like(initial_state, tf.float32, name="state_accumulator")
batch_mask = tf.constant(True, tf.bool,[self.batch_size])
# Tensor arrays to collect information about the run:
array_probs = tf.TensorArray(tf.float32,0, dynamic_size=True)
premise_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
hypothesis_attention = tf.TensorArray(tf.float32,0, dynamic_size=True)
# While loop stops when this predicate is FALSE.
# Ie all (probability < 1-eps AND counter < N) are false.
pred = lambda i ,array_probs, premise_attention, hypothesis_attention, batch_mask,prob_compare,prob,\
counter,state,premise, hypothesis ,acc_state:\
tf.reduce_any(
tf.logical_and(
tf.less(prob_compare,self.one_minus_eps),
tf.less(counter,self.N)))
# only stop if all of the batch have passed either threshold
# Do while loop iterations until predicate above is false.
i,array_probs,premise_attention,hypothesis_attention,_,_,remainders,iterations,_,_,_,state = \
tf.while_loop(pred,self.inference_step,
[i,array_probs, premise_attention, hypothesis_attention,
batch_mask,prob_compare,prob,
counter,initial_state,premise, hypothesis, acc_states])
self.ACTPROB = array_probs.pack()
self.ACTPREMISEATTN = premise_attention.pack()
self.ACTHYPOTHESISATTN = hypothesis_attention.pack()
return state, remainders, iterations
def decode(self, enc_outputs, enc_final_state):
with tf.variable_scope(self.decoder.scope):
def condition(time, all_outputs: tf.TensorArray, inputs, states):
def check_outputs_ends():
def has_end_word(t):
return tf.reduce_any(tf.equal(t, ANSWER_MAX))
output_label = tf.arg_max(all_outputs.stack(), 2)
output_label = tf.Print(output_label, [output_label], "Output Labels: ")
# The outputs are time-major, which means time is the first
# dimension. Here I need to check whether all the generated
# answers are ends with "</s>", so we need to transpose it
# to batch-major. Because `map_fn` only map function by the
# first dimension.
batch_major_outputs = tf.transpose(output_label, (1, 0))
all_outputs_ends = tf.reduce_all(tf.map_fn(has_end_word, batch_major_outputs, dtype=tf.bool))
return all_outputs_ends
# If the TensorArray has 0 size, stack() will trigger error,
# so I have to use condition function to check whether the
# size is 0.
all_ends = tf.cond(tf.equal(all_outputs.size(), 0),
lambda: tf.constant(False, tf.bool),
check_outputs_ends)
condition_result = tf.logical_and(tf.logical_not(all_ends), tf.less(time, ANSWER_MAX))
return condition_result
def body(time, all_outputs, inputs, state):
dec_outputs, dec_state, output_logits, next_input = self.decoder.step(inputs, state)
all_outputs = all_outputs.write(time, output_logits)
return time + 1, all_outputs, next_input, dec_state
output_ta = tensor_array_ops.TensorArray(dtype=tf.float32,
size=0,
dynamic_size=True,
element_shape=(None, config.DEC_VOCAB),
clear_after_read=False)
# with time-major data input, the batch size is the second dimension
batch_size = tf.shape(enc_outputs)[1]
zero_input = tf.ones(tf.expand_dims(batch_size, axis=0), dtype=tf.int32) * ANSWER_START
res = control_flow_ops.while_loop(
condition,
body,
loop_vars=[0, output_ta, self.decoder.zero_input(zero_input), enc_final_state],
)
final_outputs = res[1].stack()
final_outputs = tf.Print(final_outputs, [final_outputs], "Final Output: ")
final_state = res[3]
return final_outputs, final_state