def seg_num_of_error(self):
mistakes = tf.not_equal(
tf.argmax(self._target, 2), tf.argmax(self.seg_prediction, 2))
mistakes = tf.cast(mistakes, tf.float32)
mask = tf.sign(tf.reduce_max(self.target, reduction_indices=2))
mistakes *= mask
# Average over actual sequence lengths.
mistakes = tf.reduce_sum(mistakes)
return mistakes
python类not_equal()的实例源码
def pos_num_of_error(self):
mistakes = tf.not_equal(
tf.argmax(self._pos, 2), tf.argmax(self.pos_prediction, 2))
mistakes = tf.cast(mistakes, tf.float32)
mask = tf.sign(tf.reduce_max(self._pos, reduction_indices=2))
mistakes *= mask
# Average over actual sequence lengths.
mistakes = tf.reduce_sum(mistakes, reduction_indices=1)
return mistakes
def compute_error(self, test_state, target, s=None):
prediction = self.predict(test_state, s)
incorrects = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1)) # always 1?
return tf.reduce_mean(tf.cast(incorrects, tf.float32)) # what is reduce_mean?
def compute_error(self, test_state, target, s=None):
prediction = self.predict(test_state, s)
incorrects = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1)) # always 1?
return tf.reduce_mean(tf.cast(incorrects, tf.float32)) # what is reduce_mean?
def compute_error(self, test_state, target, s=None):
prediction = self.predict(test_state, s)
incorrects = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1)) # always 1?
return tf.reduce_mean(tf.cast(incorrects, tf.float32)) # what is reduce_mean?
def char_accuracy(predictions, targets, rej_char, streaming=False):
"""Computes character level accuracy.
Both predictions and targets should have the same shape
[batch_size x seq_length].
Args:
predictions: predicted characters ids.
targets: ground truth character ids.
rej_char: the character id used to mark an empty element (end of sequence).
streaming: if True, uses the streaming mean from the slim.metric module.
Returns:
a update_ops for execution and value tensor whose value on evaluation
returns the total character accuracy.
"""
with tf.variable_scope('CharAccuracy'):
predictions.get_shape().assert_is_compatible_with(targets.get_shape())
targets = tf.to_int32(targets)
const_rej_char = tf.constant(rej_char, shape=targets.get_shape())
weights = tf.to_float(tf.not_equal(targets, const_rej_char))
correct_chars = tf.to_float(tf.equal(predictions, targets))
accuracy_per_example = tf.div(tf.reduce_sum(tf.multiply(
correct_chars, weights), 1), tf.reduce_sum(weights, 1))
if streaming:
return tf.contrib.metrics.streaming_mean(accuracy_per_example)
else:
return tf.reduce_mean(accuracy_per_example)
def sequence_accuracy(predictions, targets, rej_char, streaming=False):
"""Computes sequence level accuracy.
Both input tensors should have the same shape: [batch_size x seq_length].
Args:
predictions: predicted character classes.
targets: ground truth character classes.
rej_char: the character id used to mark empty element (end of sequence).
streaming: if True, uses the streaming mean from the slim.metric module.
Returns:
a update_ops for execution and value tensor whose value on evaluation
returns the total sequence accuracy.
"""
with tf.variable_scope('SequenceAccuracy'):
predictions.get_shape().assert_is_compatible_with(targets.get_shape())
targets = tf.to_int32(targets)
const_rej_char = tf.constant(
rej_char, shape=targets.get_shape(), dtype=tf.int32)
include_mask = tf.not_equal(targets, const_rej_char)
include_predictions = tf.to_int32(
tf.where(include_mask, predictions, tf.zeros_like(predictions) + rej_char))
correct_chars = tf.to_float(tf.equal(include_predictions, targets))
correct_chars_counts = tf.cast(
tf.reduce_sum(correct_chars, reduction_indices=[1]), dtype=tf.int32)
target_length = targets.get_shape().dims[1].value
target_chars_counts = tf.constant(
target_length, shape=correct_chars_counts.get_shape())
accuracy_per_example = tf.to_float(
tf.equal(correct_chars_counts, target_chars_counts))
if streaming:
return tf.contrib.metrics.streaming_mean(accuracy_per_example)
else:
return tf.reduce_mean(accuracy_per_example)
def decode_sparse(self, include_stop_tokens=True):
dense_symbols, logprobs = self.decode_dense()
mask = tf.not_equal(dense_symbols, self.stop_token)
if include_stop_tokens:
mask = tf.concat(1, [tf.ones_like(mask[:, :1]), mask[:, :-1]])
return sparse_boolean_mask(dense_symbols, mask), logprobs
def setUp(self):
super(CoreBinaryOpsTest, self).setUp()
self.x_probs_broadcast_tensor = tf.reshape(
self.x_probs_lt.tensor, [self.x_size, 1, self.probs_size])
self.channel_probs_broadcast_tensor = tf.reshape(
self.channel_probs_lt.tensor, [1, self.channel_size, self.probs_size])
# == and != are not element-wise for tf.Tensor, so they shouldn't be
# elementwise for LabeledTensor, either.
self.ops = [
('add', operator.add, tf.add, core.add),
('sub', operator.sub, tf.sub, core.sub),
('mul', operator.mul, tf.mul, core.mul),
('div', operator.truediv, tf.div, core.div),
('mod', operator.mod, tf.mod, core.mod),
('pow', operator.pow, tf.pow, core.pow_function),
('equal', None, tf.equal, core.equal),
('less', operator.lt, tf.less, core.less),
('less_equal', operator.le, tf.less_equal, core.less_equal),
('not_equal', None, tf.not_equal, core.not_equal),
('greater', operator.gt, tf.greater, core.greater),
('greater_equal', operator.ge, tf.greater_equal, core.greater_equal),
]
self.test_lt_1 = self.x_probs_lt
self.test_lt_2 = self.channel_probs_lt
self.test_lt_1_broadcast = self.x_probs_broadcast_tensor
self.test_lt_2_broadcast = self.channel_probs_broadcast_tensor
self.broadcast_axes = [self.a0, self.a1, self.a3]
def not_equal(x, y):
'''Element-wise inequality between two tensors.
Returns a bool tensor.
'''
return tf.not_equal(x, y)
def w2v_error(self):
# mistakes = tf.not_equal(tf.argmax(self.target, 1), tf.argmax(self.encoder, 1))
# return tf.reduce_mean(tf.cast(mistakes, tf.float32))
y_true = tf.nn.l2_normalize(self.target, dim=-1)
y_pred = tf.nn.l2_normalize(self.w2v_predictor, dim=-1)
return -tf.reduce_mean(y_true * y_pred)
def retrieve_seq_length_op3(data, pad_val=0): # HangSheng: return tensor for sequence length, if input is tf.string
data_shape_size = data.get_shape().ndims
if data_shape_size == 3:
return tf.reduce_sum(tf.cast(tf.reduce_any(tf.not_equal(data, pad_val), axis=2), dtype=tf.int32), 1)
elif data_shape_size == 2:
return tf.reduce_sum(tf.cast(tf.not_equal(data, pad_val), dtype=tf.int32), 1)
elif data_shape_size == 1:
raise ValueError("retrieve_seq_length_op3: data has wrong shape!")
else:
raise ValueError("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % (data_shape_size))
def target_mask_op(data, pad_val=0): # HangSheng: return tensor for mask,if input is tf.string
data_shape_size = data.get_shape().ndims
if data_shape_size == 3:
return tf.cast(tf.reduce_any(tf.not_equal(data, pad_val), axis=2), dtype=tf.int32)
elif data_shape_size == 2:
return tf.cast(tf.not_equal(data, pad_val), dtype=tf.int32)
elif data_shape_size == 1:
raise ValueError("target_mask_op: data has wrong shape!")
else:
raise ValueError("target_mask_op: handling data_shape_size %s hasn't been implemented!" % (data_shape_size))
# Dynamic RNN
def errors(self, y):
return tf.reduce_mean(tf.cast(tf.not_equal(self.y_pred, tf.arg_max(y,1)), dtype=tf.float32))
def __init__(self, logdir, experiment, threads):
# Construct the graph
with tf.name_scope("inputs"):
self.images = tf.placeholder(tf.float32, [None, WIDTH, HEIGHT, 1], name="images")
self.labels = tf.placeholder(tf.int64, [None], name="labels")
flattened_images = layers.flatten(self.images)
hidden_layer = layers.fully_connected(flattened_images, num_outputs=HIDDEN, activation_fn=tf.nn.relu, scope="hidden_layer")
output_layer = layers.fully_connected(hidden_layer, num_outputs=LABELS, activation_fn=None, scope="output_layer")
loss = losses.sparse_softmax_cross_entropy(output_layer, self.labels, scope="loss")
self.training = layers.optimize_loss(loss, None, None, tf.train.AdamOptimizer(), summaries=['loss', 'gradients', 'gradient_norm'], name='training')
with tf.name_scope("accuracy"):
predictions = tf.argmax(output_layer, 1, name="predictions")
accuracy = metrics.accuracy(predictions, self.labels)
tf.scalar_summary("training/accuracy", accuracy)
with tf.name_scope("confusion_matrix"):
confusion_matrix = metrics.confusion_matrix(predictions, self.labels, weights=tf.not_equal(predictions, self.labels), dtype=tf.float32)
confusion_image = tf.reshape(confusion_matrix, [1, LABELS, LABELS, 1])
# Summaries
self.summaries = {'training': tf.merge_all_summaries() }
for dataset in ["dev", "test"]:
self.summaries[dataset] = tf.merge_summary([tf.scalar_summary(dataset + "/accuracy", accuracy),
tf.image_summary(dataset + "/confusion_matrix", confusion_image)])
# Create the session
self.session = tf.Session(config=tf.ConfigProto(inter_op_parallelism_threads=threads,
intra_op_parallelism_threads=threads))
self.session.run(tf.initialize_all_variables())
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
self.summary_writer = tf.train.SummaryWriter("{}/{}-{}".format(logdir, timestamp, experiment), graph=self.session.graph, flush_secs=10)
self.steps = 0
def __call__(self, embed, train_labels):
with tf.name_scope("negative_sampling"):
# mask out skip or OOV
# if switched on, this yields ...
# UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
# mask = tf.greater(train_labels, NegativeSampling.IGNORE_LABEL_MAX)
# # mask = tf.not_equal(train_labels, NegativeSampling.IGNORE_LABEL)
# embed = tf.boolean_mask(embed, mask)
# train_labels = tf.expand_dims(tf.boolean_mask(train_labels, mask), -1)
train_labels = tf.expand_dims(train_labels, -1)
# Compute the average NCE loss for the batch.
# tf.nce_loss automatically draws a new sample of the negative labels each
# time we evaluate the loss.
# By default this uses a log-uniform (Zipfian) distribution for sampling
# and therefore assumes labels are sorted - which they are!
sampler = (self.freqs if self.freqs is None # default to unigram
else tf.nn.fixed_unigram_candidate_sampler(
train_labels, num_true=1, num_sampled=self.sample_size,
unique=True, range_max=self.vocab_size,
#num_reserved_ids=2, # skip or OoV
# ^ only if not in unigrams
distortion=self.power, unigrams=list(self.freqs)))
loss = tf.reduce_mean(
tf.nn.nce_loss(self.nce_weights, self.nce_biases,
embed, # summed doc and context embedding
train_labels, self.sample_size, self.vocab_size,
sampled_values=sampler), # log-unigram if not specificed
name="nce_batch_loss")
# TODO negative sampling versus NCE
# TODO uniform vs. Zipf with exponent `distortion` param
#https://www.tensorflow.org/versions/r0.12/api_docs/python/nn.html#log_uniform_candidate_sampler
return loss
def retrieve_seq_length_op3(data, pad_val=0): # HangSheng: return tensor for sequence length, if input is tf.string
data_shape_size = data.get_shape().ndims
if data_shape_size == 3:
return tf.reduce_sum(tf.cast(tf.reduce_any(tf.not_equal(data, pad_val), axis=2), dtype=tf.int32), 1)
elif data_shape_size == 2:
return tf.reduce_sum(tf.cast(tf.not_equal(data, pad_val), dtype=tf.int32), 1)
elif data_shape_size == 1:
raise ValueError("retrieve_seq_length_op3: data has wrong shape!")
else:
raise ValueError("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % (data_shape_size))
def target_mask_op(data, pad_val=0): # HangSheng: return tensor for mask,if input is tf.string
data_shape_size = data.get_shape().ndims
if data_shape_size == 3:
return tf.cast(tf.reduce_any(tf.not_equal(data, pad_val), axis=2), dtype=tf.int32)
elif data_shape_size == 2:
return tf.cast(tf.not_equal(data, pad_val), dtype=tf.int32)
elif data_shape_size == 1:
raise ValueError("target_mask_op: data has wrong shape!")
else:
raise ValueError("target_mask_op: handling data_shape_size %s hasn't been implemented!" % (data_shape_size))
# Dynamic RNN
def accuracy(self):
a = tf.equal(self.hard,self.targetPlaceholder)
for decoder in self.decoders:
if decoder.token != STOP:
vector = decoder.accuracyVector()
if vector != True:
a = tf.logical_and(a,
tf.logical_or(vector, tf.not_equal(self.hard,decoder.token)))
return tf.reduce_mean(tf.cast(a, tf.float32))
def add_embedding(self):
#embed=np.load('glove{0}_uniform.npy'.format(self.emb_dim))
with tf.variable_scope("Embed",regularizer=None):
embedding=tf.get_variable('embedding',[self.num_emb,
self.emb_dim]
,initializer=tf.random_uniform_initializer(-0.05,0.05),trainable=True,regularizer=None)
ix=tf.to_int32(tf.not_equal(self.input,-1))*self.input
emb_tree=tf.nn.embedding_lookup(embedding,ix)
emb_tree=emb_tree*(tf.expand_dims(
tf.to_float(tf.not_equal(self.input,-1)),2))
return emb_tree