本文整理汇总了Python中tensorflow.negative方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.negative方法的具体用法?Python tensorflow.negative怎么用?Python tensorflow.negative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.negative方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testGetBackwardOpsSplit
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def testGetBackwardOpsSplit(self):
# a -> b -> c
# \-> d
a = tf.placeholder(tf.float32)
b = tf.exp(a)
c = tf.log(b)
d = tf.negative(b)
self.assertEqual(get_backward_ops([d]), [a.op, b.op, d.op])
self.assertEqual(get_backward_ops([c]), [a.op, b.op, c.op])
self.assertEqual(
get_backward_ops([c, d]), [a.op, b.op, c.op, d.op])
self.assertEqual(get_backward_ops([b, d]), [a.op, b.op, d.op])
self.assertEqual(get_backward_ops([a, d]), [a.op, b.op, d.op])
self.assertEqual(
get_backward_ops([c, d], treat_as_inputs=[b]), [c.op, d.op])
self.assertEqual(
get_backward_ops([c], treat_as_inputs=[d]), [a.op, b.op, c.op])
示例2: knn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def knn(X_test, X_ref, Y_ref, K = 5):
nearest_neighbors=tf.Variable(tf.zeros([K]))
distance = tf.negative(tf.reduce_sum(tf.abs(tf.subtract(X_ref, X_test[0])),axis=1)) #L1
values,indices=tf.nn.top_k(distance,k=K,sorted=False)
nn = []
for k in range(K):
nn.append(tf.argmax(Y_ref[indices[k]], 0))
nearest_neighbors=nn
y, idx, count = tf.unique_with_counts(nearest_neighbors)
preds = tf.slice(y, begin=[tf.argmax(count, 0)], size=tf.constant([1], dtype=tf.int64))[0]
return preds
示例3: _train_body
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def _train_body(self, states, actions, next_states, rewards, done, weights):
with tf.device(self.device):
with tf.GradientTape() as tape:
if self._enable_categorical_dqn:
td_errors = self._compute_td_error_body_distributional(
states, actions, next_states, rewards, done)
q_func_loss = tf.reduce_mean(
huber_loss(tf.negative(td_errors),
delta=self.max_grad) * weights)
else:
td_errors = self._compute_td_error_body(
states, actions, next_states, rewards, done)
q_func_loss = tf.reduce_mean(
huber_loss(td_errors,
delta=self.max_grad) * weights)
q_func_grad = tape.gradient(
q_func_loss, self.q_func.trainable_variables)
self.q_func_optimizer.apply_gradients(
zip(q_func_grad, self.q_func.trainable_variables))
return td_errors, q_func_loss
示例4: node_sequence
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def node_sequence(sequence, width, stride):
"""Normalizes a given sequence to have a fixed width by striding over the
sequence. The returned sequence is padded with -1 if its length is lower
than the requested width.
Args:
sequence: A 1d tensor.
width: The length of the returned sequence.
stride: The distance between two selected nodes.
Returns:
A 1d tensor.
"""
with tf.name_scope('node_sequence', values=[sequence, width, stride]):
# Stride the sequence based on the given stride size.
sequence = tf.strided_slice(sequence, [0], [width*stride], [stride])
# Pad right with -1 if the sequence length is lower than width.
padding = tf.ones([width - tf.shape(sequence)[0]], dtype=tf.int32)
padding = tf.negative(padding)
sequence = tf.concat(0, [sequence, padding])
return sequence
示例5: chk_idx_out_of_bounds_along_axis
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def chk_idx_out_of_bounds_along_axis(cls, data, axis, indices):
""" Check indices out of bounds for ScatterElement
In Tensorflow GPU version, if an out of bound index is found,
the index is ignored for ScatterND/TensorScatterNDUpdate.
But ONNX spec state that it is an error if any index values
are out of bounds. Therefore the converter need to run this
function to verify all the indices are in bounds along the
axis before send it to Tensoflow. If out of bound is detected
then the caller of this function need to throw
InvalidArgumentError exception.
"""
data_shape = tf.cast(tf_shape(data), indices.dtype)
limit = data_shape[axis]
cond1 = tf.greater_equal(indices, tf.negative(limit))
cond2 = tf.less(indices, limit)
return tf.logical_and(cond1, cond2)
示例6: chk_pos_in_bounds
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def chk_pos_in_bounds(cls, input_seq, pos):
"""
Check the position is in-bounds with respect to the sequence.
Accepted range for 'position' is in [-n, n - 1], where n is the
number of tensors in 'input_sequence'.
:param input_seq: input sequence
:param pos: position of the output tensor
:return: True if position is in-bounds
"""
seq_length = tf.shape(input_seq.to_sparse(), out_type=pos.dtype)[0]
cond1 = tf.greater_equal(pos, tf.negative(seq_length))
cond2 = tf.less_equal(pos, seq_length - 1)
# pos >= -n and pos < n
return tf.reduce_all(tf.logical_and(cond1, cond2))
示例7: chk_pos_in_bounds
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def chk_pos_in_bounds(cls, input_seq, pos):
"""
Check the position is in-bounds with respect to the sequence.
Accepted range for 'position' is in [-n, n], where n is the
number of tensors in 'input_sequence'.
:param input_seq: input sequence
:param pos: position to insert the tensor
:return: True if position is in-bounds.
"""
seq_length = tf.shape(input_seq.to_sparse(), out_type=pos.dtype)[0]
cond1 = tf.greater_equal(pos, tf.negative(seq_length))
cond2 = tf.less_equal(pos, seq_length)
# pos >= -n and pos <= n
return tf.reduce_all(tf.logical_and(cond1, cond2))
示例8: calculate_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def calculate_loss(self, predictions, labels, weights=None, **unused_params):
with tf.name_scope("loss_xent"):
epsilon = 10e-6
if FLAGS.label_smoothing:
float_labels = smoothing(labels)
else:
float_labels = tf.cast(labels, tf.float32)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss)
if weights is not None:
print cross_entropy_loss, weights
weighted_loss = tf.einsum("ij,i->ij", cross_entropy_loss, weights)
print "create weighted_loss", weighted_loss
return tf.reduce_mean(tf.reduce_sum(weighted_loss, 1))
else:
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
示例9: calculate_loss_distill_boost
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def calculate_loss_distill_boost(self, predictions, labels_distill, labels, **unused_params):
with tf.name_scope("loss_distill_boost"):
print("loss_distill_boost")
epsilon = 10e-6
float_labels = tf.cast(labels, tf.float32)
batch_size = tf.shape(float_labels)[0]
float_labels_distill = tf.cast(labels_distill, tf.float32)
error = tf.negative(float_labels * tf.log(float_labels_distill + epsilon) + (
1 - float_labels) * tf.log(1 - float_labels_distill + epsilon))
error = tf.reduce_sum(error,axis=1,keep_dims=True)
alpha = error / tf.reduce_sum(error) * tf.cast(batch_size,dtype=tf.float32)
alpha = tf.clip_by_value(alpha, 0.5, 5)
alpha = alpha / tf.reduce_sum(alpha) * tf.cast(batch_size,dtype=tf.float32)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss * alpha)
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
示例10: calculate_loss_distill_relabel
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def calculate_loss_distill_relabel(self, predictions, labels_distill, labels, **unused_params):
with tf.name_scope("loss_distill_relabel"):
print("loss_distill_relabel")
epsilon = 10e-6
float_labels = tf.cast(labels, tf.float32)
sum_labels = tf.cast(tf.reduce_sum(float_labels),dtype=tf.int32)
pos_distill, _ = tf.nn.top_k(tf.reshape(labels_distill,[-1]), k=sum_labels)
labels_true = tf.ones(tf.shape(labels))
labels_false = tf.zeros(tf.shape(labels))
labels_add = tf.where(tf.greater_equal(labels_distill, pos_distill[-1]), labels_true, labels_false)
print(labels_add.get_shape().as_list())
float_labels = float_labels+labels_add*(1.0-float_labels)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss)
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
示例11: calculate_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def calculate_loss(self, predictions, labels, **unused_params):
with tf.name_scope("loss_xent"):
epsilon = 10e-6
vocab_size = predictions.get_shape().as_list()[1]
float_labels = tf.cast(labels, tf.float32)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss)
neg_labels = 1 - float_labels
predictions_pos = predictions*float_labels+10*neg_labels
predictions_minpos = tf.reduce_min(predictions_pos,axis=1,keep_dims=True)
predictions_neg = predictions*neg_labels-10*float_labels
predictions_maxneg = tf.reduce_max(predictions_neg,axis=1,keep_dims=True)
mask_1 = tf.cast(tf.greater_equal(predictions_neg, predictions_minpos),dtype=tf.float32)
mask_2 = tf.cast(tf.less_equal(predictions_pos, predictions_maxneg),dtype=tf.float32)
cross_entropy_loss = cross_entropy_loss*(mask_1+mask_2)*10 + cross_entropy_loss
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
示例12: calculate_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def calculate_loss(self, predictions, labels, **unused_params):
bound = FLAGS.softmax_bound
vocab_size_1 = bound
with tf.name_scope("loss_softmax"):
epsilon = 10e-8
float_labels = tf.cast(labels, tf.float32)
labels_1 = float_labels[:,:vocab_size_1]
predictions_1 = predictions[:,:vocab_size_1]
cross_entropy_loss = CrossEntropyLoss().calculate_loss(predictions_1,labels_1)
lables_2 = float_labels[:,vocab_size_1:]
predictions_2 = predictions[:,vocab_size_1:]
# l1 normalization (labels are no less than 0)
label_rowsum = tf.maximum(
tf.reduce_sum(lables_2, 1, keep_dims=True),
epsilon)
label_append = 1.0-tf.reduce_max(lables_2, 1, keep_dims=True)
norm_float_labels = tf.concat((tf.div(lables_2, label_rowsum),label_append),axis=1)
predictions_append = 1.0-tf.reduce_sum(predictions_2, 1, keep_dims=True)
softmax_outputs = tf.concat((predictions_2,predictions_append),axis=1)
softmax_loss = norm_float_labels * tf.log(softmax_outputs + epsilon) + (
1 - norm_float_labels) * tf.log(1 - softmax_outputs + epsilon)
softmax_loss = tf.negative(tf.reduce_sum(softmax_loss, 1))
return tf.reduce_mean(softmax_loss) + cross_entropy_loss
示例13: _inputs_check
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def _inputs_check(self, scores_pos, scores_neg):
"""Creates any dependencies that need to be checked before performing loss computations
Parameters
----------
scores_pos : tf.Tensor
A tensor of scores assigned to positive statements.
scores_neg : tf.Tensor
A tensor of scores assigned to negative statements.
"""
logger.debug('Creating dependencies before loss computations.')
self._dependencies = []
if LOSS_REGISTRY[self.name].class_params['require_same_size_pos_neg'] and self._loss_parameters['eta'] != 1:
logger.debug('Dependencies found: \n\tRequired same size positive and negative. \n\tEta is not 1.')
self._dependencies.append(tf.Assert(tf.equal(tf.shape(scores_pos)[0], tf.shape(scores_neg)[0]),
[tf.shape(scores_pos)[0], tf.shape(scores_neg)[0]]))
示例14: _apply
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def _apply(self, scores_pos, scores_neg):
"""Apply the loss function. Every inherited class must implement this function.
(All the TF code must go in this function.)
Parameters
----------
scores_pos : tf.Tensor
A tensor of scores assigned to positive statements.
scores_neg : tf.Tensor
A tensor of scores assigned to negative statements.
Returns
-------
loss : tf.Tensor
The loss value that must be minimized.
"""
msg = 'This function is a placeholder in an abstract class.'
logger.error(msg)
NotImplementedError(msg)
示例15: apply
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import negative [as 别名]
def apply(self, scores_pos, scores_neg):
"""Interface to external world.
This function does the input checks, preprocesses input and finally applies loss function.
Parameters
----------
scores_pos : tf.Tensor
A tensor of scores assigned to positive statements.
scores_neg : tf.Tensor
A tensor of scores assigned to negative statements.
Returns
-------
loss : tf.Tensor
The loss value that must be minimized.
"""
self._inputs_check(scores_pos, scores_neg)
with tf.control_dependencies(self._dependencies):
loss = self._apply(scores_pos, scores_neg)
return loss