本文整理汇总了Python中tensorflow.logical_xor方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.logical_xor方法的具体用法?Python tensorflow.logical_xor怎么用?Python tensorflow.logical_xor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.logical_xor方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testBCast
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def testBCast(self):
shapes = [
([1, 3, 2], [1]),
([1, 3, 2], [2]),
([1, 3, 2], [3, 2]),
([1, 3, 2], [3, 1]),
([1, 3, 2], [1, 3, 2]),
([1, 3, 2], [2, 3, 1]),
([1, 3, 2], [2, 1, 1]),
([1, 3, 2], [1, 3, 1]),
([2, 1, 5], [2, 3, 1]),
([2, 0, 5], [2, 0, 1]),
([2, 3, 0], [2, 3, 1]),
]
for (xs, ys) in shapes:
x = np.random.randint(0, 2, np.prod(xs)).astype(np.bool).reshape(xs)
y = np.random.randint(0, 2, np.prod(ys)).astype(np.bool).reshape(ys)
for use_gpu in [True, False]:
self._compareBinary(x, y, np.logical_and, tf.logical_and, use_gpu)
self._compareBinary(x, y, np.logical_or, tf.logical_or, use_gpu)
self._compareBinary(x, y, np.logical_xor, tf.logical_xor, use_gpu)
示例2: __xor__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def __xor__(self, other):
return tf.logical_xor(self, other)
示例3: __rxor__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def __rxor__(self, other):
return tf.logical_xor(other, self)
# boolean operations
示例4: testScalar
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def testScalar(self):
data = [np.array([True]), np.array([False])]
for use_gpu in [True, False]:
for x in data:
self._not(x, use_gpu)
for x in data:
for y in data:
self._compareBinary(
x, y, np.logical_and, tf.logical_and, use_gpu)
self._compareBinary(
x, y, np.logical_or, tf.logical_or, use_gpu)
self._compareBinary(
x, y, np.logical_xor, tf.logical_xor, use_gpu)
示例5: testTensor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def testTensor(self):
x = np.random.randint(0, 2, 6).astype(np.bool).reshape(1, 3, 2)
y = np.random.randint(0, 2, 6).astype(np.bool).reshape(1, 3, 2)
for use_gpu in [True, False]:
self._not(x, use_gpu)
self._compareBinary(x, y, np.logical_and, tf.logical_and, use_gpu)
self._compareBinary(x, y, np.logical_or, tf.logical_or, use_gpu)
self._compareBinary(x, y, np.logical_xor, tf.logical_xor, use_gpu)
示例6: testShapeMismatch
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def testShapeMismatch(self):
x = np.random.randint(0, 2, 6).astype(np.bool).reshape(1, 3, 2)
y = np.random.randint(0, 2, 6).astype(np.bool).reshape(3, 2, 1)
for f in [tf.logical_and, tf.logical_or, tf.logical_xor]:
with self.assertRaisesWithPredicateMatch(
ValueError, lambda e: "Dimensions must" in str(e)):
f(x, y)
示例7: testOverloadComparisons
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def testOverloadComparisons(self):
dtypes = [
tf.float16,
tf.float32,
tf.float64,
tf.int32,
tf.int64,
]
funcs = [
(np.less, _LT),
(np.less_equal, _LE),
(np.greater, _GT),
(np.greater_equal, _GE),
]
for dtype in dtypes:
for np_func, tf_func in funcs:
self._compareBinary(10, 5, dtype, np_func, tf_func)
logical_funcs = [
(np.logical_and, _AND),
(np.logical_or, _OR),
(np.logical_xor, _XOR),
(np.equal, tf.equal),
(np.not_equal, tf.not_equal)
]
for np_func, tf_func in logical_funcs:
self._compareBinary(True, False, tf.bool, np_func, tf_func)
self._compareBinary(True, True, tf.bool, np_func, tf_func)
self._compareBinary(False, False, tf.bool, np_func, tf_func)
self._compareBinary(False, True, tf.bool, np_func, tf_func)
self._compareBinary([True, True, False, False],
[True, False, True, False],
tf.bool, np_func, tf_func)
self._compareUnary(True, tf.bool, np.logical_not, _INV)
self._compareUnary(False, tf.bool, np.logical_not, _INV)
self._compareUnary([True, False], tf.bool, np.logical_not, _INV)
示例8: visit_xor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def visit_xor(self, op: d5.ops.Xor, network: TensorflowNetwork):
A, B = network.fetch_internal_tensors([op.i_A, op.i_B])
C = tf.logical_xor(A, B)
network.feed_internal_tensor(op.o_C, C)
示例9: test_logical_xor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def test_logical_xor():
with tf.Graph().as_default():
in1 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name='in1')
in2 = tf.placeholder(tf.bool, shape=[1, 4, 4, 3], name='in2')
out = tf.logical_xor(in1, in2, name='out')
in_data1 = np.random.choice(
a=[False, True], size=(1, 4, 4, 3)).astype('bool')
in_data2 = np.random.choice(
a=[False, True], size=(1, 4, 4, 3)).astype('bool')
compare_tf_with_tvm([in_data1, in_data2], ['in1:0', 'in2:0'], 'out:0')
示例10: desc_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import logical_xor [as 别名]
def desc_loss(dists, pids, pos_margin=0.1, neg_margin=1.4, false_negative_mask=None):
"""Computes the contrastive loss.
Args:
dists (2D tensor): A square all-to-all distance matrix as given by cdist.
pids (1D tensor): The identities of the entries in `batch`, shape (B,).
This can be of any type that can be compared, thus also a string.
pos_margin, neg_margin (float): the margin for contrastive loss
false_negative_mask (2D tensor): A boolean matrix to indicate the false negative within the safe_radius.
Returns:
A 1D tensor of shape (B,) containing the loss value for each sample.
"""
with tf.name_scope("desc_loss"):
same_identity_mask = tf.equal(tf.expand_dims(pids, axis=1),
tf.expand_dims(pids, axis=0))
negative_mask = tf.logical_not(same_identity_mask)
if false_negative_mask is not None:
negative_mask = tf.logical_and(negative_mask, tf.logical_not(false_negative_mask))
negative_mask.set_shape([None, None])
# positive_mask = tf.logical_xor(same_identity_mask,
# tf.eye(tf.shape(pids)[0], dtype=tf.bool))
furthest_positive = tf.reduce_max(dists * tf.cast(same_identity_mask, tf.float32), axis=1)
# closest_negative = tf.map_fn(lambda x: tf.reduce_min(tf.boolean_mask(x[0], x[1])), (dists, negative_mask), tf.float32)
closest_negative = tf.reduce_min(dists + 1e5 * tf.cast(same_identity_mask, tf.float32), axis=1)
# Another way of achieving the same, though more hacky:
# closest_negative_col = tf.reduce_min(dists + 1e5*tf.cast(same_identity_mask, tf.float32), axis=1)
# closest_negative_row = tf.reduce_min(dists + 1e5*tf.cast(same_identity_mask, tf.float32), axis=0)
# closest_negative = tf.minimum(closest_negative_col, closest_negative_row)
# # calculate average negative to monitor the training
# average_negative = tf.map_fn(lambda x: tf.reduce_mean(tf.boolean_mask(x[0], x[1])), (dists, negative_mask), tf.float32)
average_negative = tf.reduce_mean(dists * tf.cast(negative_mask, tf.float32)) * tf.cast(tf.size(pids), tf.float32) / (tf.cast(tf.size(pids), tf.float32) - 1.0)
# average_diff = tf.reduce_mean(furthest_positive - average_negative)
diff = furthest_positive - closest_negative
accuracy = tf.reduce_sum(tf.cast(tf.greater_equal(0., diff), tf.float32)) / tf.cast(tf.shape(diff)[0], tf.float32)
# contrastive loss
diff = tf.maximum(furthest_positive - pos_margin, 0.0) + tf.maximum(neg_margin - closest_negative, 0.0)
return tf.reduce_mean(diff), accuracy, tf.reduce_mean(furthest_positive), tf.reduce_mean(average_negative)