本文整理匯總了Python中tensorflow.python.ops.gen_logging_ops._assert方法的典型用法代碼示例。如果您正苦於以下問題:Python gen_logging_ops._assert方法的具體用法?Python gen_logging_ops._assert怎麽用?Python gen_logging_ops._assert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.ops.gen_logging_ops
的用法示例。
在下文中一共展示了gen_logging_ops._assert方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Assert
# 需要導入模塊: from tensorflow.python.ops import gen_logging_ops [as 別名]
# 或者: from tensorflow.python.ops.gen_logging_ops import _assert [as 別名]
def Assert(condition, data, summarize=None, name=None):
"""Asserts that the given condition is true.
If `condition` evaluates to false, print the list of tensors in `data`.
`summarize` determines how many entries of the tensors to print.
NOTE: To ensure that Assert executes, one usually attaches a dependency:
```python
# Ensure maximum element of x is smaller or equal to 1
assert_op = tf.Assert(tf.less_equal(tf.reduce_max(x), 1.), [x])
with tf.control_dependencies([assert_op]):
... code using x ...
```
Args:
condition: The condition to evaluate.
data: The tensors to print out when condition is false.
summarize: Print this many entries of each tensor.
name: A name for this operation (optional).
Returns:
assert_op: An `Operation` that, when executed, raises a
`tf.errors.InvalidArgumentError` if `condition` is not true.
"""
with ops.name_scope(name, "Assert", [condition, data]) as name:
xs = ops.convert_n_to_tensor(data)
if all([x.dtype in {dtypes.string, dtypes.int32} for x in xs]):
# As a simple heuristic, we assume that string and int32 are
# on host to avoid the need to use cond. If it is not case,
# we will pay the price copying the tensor to host memory.
return gen_logging_ops._assert(
condition, data, summarize, name="Assert")
else:
condition = ops.convert_to_tensor(condition, name="Condition")
def true_assert():
return gen_logging_ops._assert(
condition, data, summarize, name="Assert")
guarded_assert = cond(
condition, no_op, true_assert, name="AssertGuard")
return guarded_assert.op
示例2: testGuardedAssertDoesNotCopyWhenTrue
# 需要導入模塊: from tensorflow.python.ops import gen_logging_ops [as 別名]
# 或者: from tensorflow.python.ops.gen_logging_ops import _assert [as 別名]
def testGuardedAssertDoesNotCopyWhenTrue(self):
with self.test_session(use_gpu=True) as sess:
with tf.device("/gpu:0"):
value = tf.constant(1.0)
with tf.device("/cpu:0"):
true = tf.constant(True)
guarded_assert = tf.Assert(true, [value], name="guarded")
unguarded_assert = gen_logging_ops._assert(
true, [value], name="unguarded")
opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
guarded_metadata = tf.RunMetadata()
sess.run(guarded_assert, options=opts, run_metadata=guarded_metadata)
unguarded_metadata = tf.RunMetadata()
sess.run(unguarded_assert, options=opts, run_metadata=unguarded_metadata)
guarded_nodestat_names = [
n.node_name for d in guarded_metadata.step_stats.dev_stats
for n in d.node_stats]
unguarded_nodestat_names = [
n.node_name for d in unguarded_metadata.step_stats.dev_stats
for n in d.node_stats]
guarded_memcpy_nodestat_names = [
n for n in guarded_nodestat_names if "MEMCPYDtoH" in n]
unguarded_memcpy_nodestat_names = [
n for n in unguarded_nodestat_names if "MEMCPYDtoH" in n]
if "GPU" in [d.device_type for d in device_lib.list_local_devices()]:
# A copy was performed for the unguarded assert
self.assertLess(0, len(unguarded_memcpy_nodestat_names))
# No copy was performed for the guarded assert
self.assertEqual([], guarded_memcpy_nodestat_names)
示例3: Assert
# 需要導入模塊: from tensorflow.python.ops import gen_logging_ops [as 別名]
# 或者: from tensorflow.python.ops.gen_logging_ops import _assert [as 別名]
def Assert(condition, data, summarize=None, name=None):
"""Asserts that the given condition is true.
If `condition` evaluates to false, print the list of tensors in `data`.
`summarize` determines how many entries of the tensors to print.
NOTE: To ensure that Assert executes, one usually attaches a dependency:
```python
# Ensure maximum element of x is smaller or equal to 1
assert_op = tf.Assert(tf.less_equal(tf.reduce_max(x), 1.), [x])
x = tf.with_dependencies([assert_op], x)
```
Args:
condition: The condition to evaluate.
data: The tensors to print out when condition is false.
summarize: Print this many entries of each tensor.
name: A name for this operation (optional).
Returns:
assert_op: An `Operation` that, when executed, raises a
`tf.errors.InvalidArgumentError` if `condition` is not true.
"""
with ops.name_scope(name, "Assert", [condition, data]) as name:
xs = ops.convert_n_to_tensor(data)
if all([x.dtype in {dtypes.string, dtypes.int32} for x in xs]):
# As a simple heuristic, we assume that string and int32 are
# on host to avoid the need to use cond. If it is not case,
# we will pay the price copying the tensor to host memory.
return gen_logging_ops._assert(
condition, data, summarize, name="Assert")
else:
condition = ops.convert_to_tensor(condition, name="Condition")
def true_assert():
return gen_logging_ops._assert(
condition, data, summarize, name="Assert")
guarded_assert = cond(
condition, no_op, true_assert, name="AssertGuard")
return guarded_assert.op
示例4: testAssertOp
# 需要導入模塊: from tensorflow.python.ops import gen_logging_ops [as 別名]
# 或者: from tensorflow.python.ops.gen_logging_ops import _assert [as 別名]
def testAssertOp(self):
@function.Defun(tf.float32)
def Foo(x):
check = gen_logging_ops._assert(tf.greater(x, 0), [x])
with tf.control_dependencies([check]):
return x * 2
g = tf.Graph()
with g.as_default(), self.test_session():
self.assertAllEqual(Foo(tf.constant(3.0)).eval(), 6.0)
with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
"assertion failed.*-3"):
self.assertAllEqual(Foo(tf.constant(-3.0)).eval(), 6.0)