STT-tensorflow/tensorflow/g3doc/api_docs/python/test.md
Jonathan Hseu bed8383c27 Merge changes from github.
Change: 142805270
2016-12-22 15:48:41 -08:00

28 KiB

Testing

[TOC]

Unit tests

TensorFlow provides a convenience class inheriting from unittest.TestCase which adds methods relevant to TensorFlow tests. Here is an example:

    import tensorflow as tf


    class SquareTest(tf.test.TestCase):

      def testSquare(self):
        with self.test_session():
          x = tf.square([2, 3])
          self.assertAllEqual(x.eval(), [4, 9])


    if __name__ == '__main__':
      tf.test.main()

tf.test.TestCase inherits from unittest.TestCase but adds a few additional methods. We will document these methods soon.


tf.test.main()

Runs all unit tests.


class tf.test.TestCase

Base class for tests that need to test TensorFlow.


tf.test.TestCase.__call__(*args, **kwds)


tf.test.TestCase.__eq__(other)


tf.test.TestCase.__hash__()


tf.test.TestCase.__init__(methodName='runTest')


tf.test.TestCase.__ne__(other)


tf.test.TestCase.__repr__()


tf.test.TestCase.__str__()


tf.test.TestCase.addCleanup(function, *args, **kwargs)

Add a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success.

Cleanup items are called even if setUp fails (unlike tearDown).


tf.test.TestCase.addTypeEqualityFunc(typeobj, function)

Add a type specific assertEqual style function to compare a type.

This method is for use by TestCase subclasses that need to register their own type equality functions to provide nicer error messages.

Args:
  • typeobj: The data type to call this function on when both values are of the same type in assertEqual().
  • function: The callable taking two arguments and an optional msg= argument that raises self.failureException with a useful error message when the two arguments are not equal.

tf.test.TestCase.assertAllClose(a, b, rtol=1e-06, atol=1e-06)

Asserts that two numpy arrays have near values.

Args:
  • a: a numpy ndarray or anything can be converted to one.
  • b: a numpy ndarray or anything can be converted to one.
  • rtol: relative tolerance
  • atol: absolute tolerance

tf.test.TestCase.assertAllCloseAccordingToType(a, b, rtol=1e-06, atol=1e-06)

Like assertAllClose, but also suitable for comparing fp16 arrays.

In particular, the tolerance is reduced to 1e-3 if at least one of the arguments is of type float16.

Args:
  • a: a numpy ndarray or anything can be converted to one.
  • b: a numpy ndarray or anything can be converted to one.
  • rtol: relative tolerance
  • atol: absolute tolerance

tf.test.TestCase.assertAllEqual(a, b)

Asserts that two numpy arrays have the same values.

Args:
  • a: a numpy ndarray or anything can be converted to one.
  • b: a numpy ndarray or anything can be converted to one.

tf.test.TestCase.assertAlmostEqual(first, second, places=None, msg=None, delta=None)

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

If the two objects compare equal then they will automatically compare almost equal.


tf.test.TestCase.assertAlmostEquals(first, second, places=None, msg=None, delta=None)

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

If the two objects compare equal then they will automatically compare almost equal.


tf.test.TestCase.assertArrayNear(farray1, farray2, err)

Asserts that two float arrays are near each other.

Checks that for all elements of farray1 and farray2 |f1 - f2| < err. Asserts a test failure if not.

Args:
  • farray1: a list of float values.
  • farray2: a list of float values.
  • err: a float value.

tf.test.TestCase.assertDeviceEqual(device1, device2)

Asserts that the two given devices are the same.

Args:
  • device1: A string device name or TensorFlow DeviceSpec object.
  • device2: A string device name or TensorFlow DeviceSpec object.

tf.test.TestCase.assertDictContainsSubset(expected, actual, msg=None)

Checks whether actual is a superset of expected.


tf.test.TestCase.assertDictEqual(d1, d2, msg=None)


tf.test.TestCase.assertEqual(first, second, msg=None)

Fail if the two objects are unequal as determined by the '==' operator.


tf.test.TestCase.assertEquals(first, second, msg=None)

Fail if the two objects are unequal as determined by the '==' operator.


tf.test.TestCase.assertFalse(expr, msg=None)

Check that the expression is false.


tf.test.TestCase.assertGreater(a, b, msg=None)

Just like self.assertTrue(a > b), but with a nicer default message.


tf.test.TestCase.assertGreaterEqual(a, b, msg=None)

Just like self.assertTrue(a >= b), but with a nicer default message.


tf.test.TestCase.assertIn(member, container, msg=None)

Just like self.assertTrue(a in b), but with a nicer default message.


tf.test.TestCase.assertIs(expr1, expr2, msg=None)

Just like self.assertTrue(a is b), but with a nicer default message.


tf.test.TestCase.assertIsInstance(obj, cls, msg=None)

Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.


tf.test.TestCase.assertIsNone(obj, msg=None)

Same as self.assertTrue(obj is None), with a nicer default message.


tf.test.TestCase.assertIsNot(expr1, expr2, msg=None)

Just like self.assertTrue(a is not b), but with a nicer default message.


tf.test.TestCase.assertIsNotNone(obj, msg=None)

Included for symmetry with assertIsNone.


tf.test.TestCase.assertItemsEqual(expected_seq, actual_seq, msg=None)

An unordered sequence specific comparison. It asserts that actual_seq and expected_seq have the same element counts. Equivalent to::

self.assertEqual(Counter(iter(actual_seq)),
                 Counter(iter(expected_seq)))

Asserts that each element has the same count in both sequences.

Example:
- [0, 1, 1] and [1, 0, 1] compare equal.
- [0, 0, 1] and [0, 1] compare unequal.

tf.test.TestCase.assertLess(a, b, msg=None)

Just like self.assertTrue(a < b), but with a nicer default message.


tf.test.TestCase.assertLessEqual(a, b, msg=None)

Just like self.assertTrue(a <= b), but with a nicer default message.


tf.test.TestCase.assertListEqual(list1, list2, msg=None)

A list-specific equality assertion.

Args:
  • list1: The first list to compare.
  • list2: The second list to compare.
  • msg: Optional message to use on failure instead of a list of differences.

tf.test.TestCase.assertMultiLineEqual(first, second, msg=None)

Assert that two multi-line strings are equal.


tf.test.TestCase.assertNDArrayNear(ndarray1, ndarray2, err)

Asserts that two numpy arrays have near values.

Args:
  • ndarray1: a numpy ndarray.
  • ndarray2: a numpy ndarray.
  • err: a float. The maximum absolute difference allowed.

tf.test.TestCase.assertNear(f1, f2, err, msg=None)

Asserts that two floats are near each other.

Checks that |f1 - f2| < err and asserts a test failure if not.

Args:
  • f1: A float value.
  • f2: A float value.
  • err: A float value.
  • msg: An optional string message to append to the failure message.

tf.test.TestCase.assertNotAlmostEqual(first, second, places=None, msg=None, delta=None)

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

Objects that are equal automatically fail.


tf.test.TestCase.assertNotAlmostEquals(first, second, places=None, msg=None, delta=None)

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most signficant digit).

Objects that are equal automatically fail.


tf.test.TestCase.assertNotEqual(first, second, msg=None)

Fail if the two objects are equal as determined by the '!=' operator.


tf.test.TestCase.assertNotEquals(first, second, msg=None)

Fail if the two objects are equal as determined by the '!=' operator.


tf.test.TestCase.assertNotIn(member, container, msg=None)

Just like self.assertTrue(a not in b), but with a nicer default message.


tf.test.TestCase.assertNotIsInstance(obj, cls, msg=None)

Included for symmetry with assertIsInstance.


tf.test.TestCase.assertNotRegexpMatches(text, unexpected_regexp, msg=None)

Fail the test if the text matches the regular expression.


tf.test.TestCase.assertProtoEquals(expected_message_maybe_ascii, message)

Asserts that message is same as parsed expected_message_ascii.

Creates another prototype of message, reads the ascii message into it and then compares them using self._AssertProtoEqual().

Args:
  • expected_message_maybe_ascii: proto message in original or ascii form
  • message: the message to validate

tf.test.TestCase.assertProtoEqualsVersion(expected, actual, producer=20, min_consumer=0)


tf.test.TestCase.assertRaises(excClass, callableObj=None, *args, **kwargs)

Fail unless an exception of class excClass is raised by callableObj when invoked with arguments args and keyword arguments kwargs. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

If called with callableObj omitted or None, will return a context object used like this::

 with self.assertRaises(SomeException):
     do_something()

The context manager keeps a reference to the exception as the 'exception' attribute. This allows you to inspect the exception after the assertion::

with self.assertRaises(SomeException) as cm:
    do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)

tf.test.TestCase.assertRaisesOpError(expected_err_re_or_predicate)


tf.test.TestCase.assertRaisesRegexp(expected_exception, expected_regexp, callable_obj=None, *args, **kwargs)

Asserts that the message in a raised exception matches a regexp.

Args:
  • expected_exception: Exception class expected to be raised.
  • expected_regexp: Regexp (re pattern object or string) expected to be found in error message.
  • callable_obj: Function to be called.
  • args: Extra args.
  • kwargs: Extra kwargs.

tf.test.TestCase.assertRaisesWithPredicateMatch(exception_type, expected_err_re_or_predicate)

Returns a context manager to enclose code expected to raise an exception.

If the exception is an OpError, the op stack is also included in the message predicate search.

Args:
  • exception_type: The expected type of exception that should be raised.
  • expected_err_re_or_predicate: If this is callable, it should be a function of one argument that inspects the passed-in exception and returns True (success) or False (please fail the test). Otherwise, the error message is expected to match this regular expression partially.
Returns:

A context manager to surround code that is expected to raise an exception.


tf.test.TestCase.assertRegexpMatches(text, expected_regexp, msg=None)

Fail the test unless the text matches the regular expression.


tf.test.TestCase.assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)

An equality assertion for ordered sequences (like lists and tuples).

For the purposes of this function, a valid ordered sequence type is one which can be indexed, has a length, and has an equality operator.

Args:
  • seq1: The first sequence to compare.
  • seq2: The second sequence to compare.
  • seq_type: The expected datatype of the sequences, or None if no datatype should be enforced.
  • msg: Optional message to use on failure instead of a list of differences.

tf.test.TestCase.assertSetEqual(set1, set2, msg=None)

A set-specific equality assertion.

Args:
  • set1: The first set to compare.
  • set2: The second set to compare.
  • msg: Optional message to use on failure instead of a list of differences.

assertSetEqual uses ducktyping to support different types of sets, and is optimized for sets specifically (parameters must support a difference method).


tf.test.TestCase.assertShapeEqual(np_array, tf_tensor)

Asserts that a Numpy ndarray and a TensorFlow tensor have the same shape.

Args:
  • np_array: A Numpy ndarray or Numpy scalar.
  • tf_tensor: A Tensor.
Raises:
  • TypeError: If the arguments have the wrong type.

tf.test.TestCase.assertStartsWith(actual, expected_start, msg=None)

Assert that actual.startswith(expected_start) is True.

Args:
  • actual: str
  • expected_start: str
  • msg: Optional message to report on failure.

tf.test.TestCase.assertTrue(expr, msg=None)

Check that the expression is true.


tf.test.TestCase.assertTupleEqual(tuple1, tuple2, msg=None)

A tuple-specific equality assertion.

Args:
  • tuple1: The first tuple to compare.
  • tuple2: The second tuple to compare.
  • msg: Optional message to use on failure instead of a list of differences.

tf.test.TestCase.assert_(expr, msg=None)

Check that the expression is true.


tf.test.TestCase.checkedThread(target, args=None, kwargs=None)

Returns a Thread wrapper that asserts 'target' completes successfully.

This method should be used to create all threads in test cases, as otherwise there is a risk that a thread will silently fail, and/or assertions made in the thread will not be respected.

Args:
  • target: A callable object to be executed in the thread.
  • args: The argument tuple for the target invocation. Defaults to ().
  • kwargs: A dictionary of keyword arguments for the target invocation. Defaults to {}.
Returns:

A wrapper for threading.Thread that supports start() and join() methods.


tf.test.TestCase.countTestCases()


tf.test.TestCase.debug()

Run the test without collecting errors in a TestResult


tf.test.TestCase.defaultTestResult()


tf.test.TestCase.doCleanups()

Execute all cleanup functions. Normally called for you after tearDown.


tf.test.TestCase.fail(msg=None)

Fail immediately, with the given message.


tf.test.TestCase.failIf(*args, **kwargs)


tf.test.TestCase.failIfAlmostEqual(*args, **kwargs)


tf.test.TestCase.failIfEqual(*args, **kwargs)


tf.test.TestCase.failUnless(*args, **kwargs)


tf.test.TestCase.failUnlessAlmostEqual(*args, **kwargs)


tf.test.TestCase.failUnlessEqual(*args, **kwargs)


tf.test.TestCase.failUnlessRaises(*args, **kwargs)


tf.test.TestCase.get_temp_dir()


tf.test.TestCase.id()


tf.test.TestCase.run(result=None)


tf.test.TestCase.setUp()


tf.test.TestCase.setUpClass(cls)

Hook method for setting up class fixture before running tests in the class.


tf.test.TestCase.shortDescription()

Returns a one-line description of the test, or None if no description has been provided.

The default implementation of this method returns the first line of the specified test method's docstring.


tf.test.TestCase.skipTest(reason)

Skip this test.


tf.test.TestCase.tearDown()


tf.test.TestCase.tearDownClass(cls)

Hook method for deconstructing the class fixture after running all tests in the class.


tf.test.TestCase.test_session(graph=None, config=None, use_gpu=False, force_gpu=False)

Returns a TensorFlow Session for use in executing tests.

This method should be used for all functional tests.

Use the use_gpu and force_gpu options to control where ops are run. If force_gpu is True, all ops are pinned to /gpu:0. Otherwise, if use_gpu is True, TensorFlow tries to run as many ops on the GPU as possible. If both force_gpu and use_gpu` are False, all ops are pinned to the CPU.

Example:

class MyOperatorTest(test_util.TensorFlowTestCase): def testMyOperator(self): with self.test_session(use_gpu=True): valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] result = MyOperator(valid_input).eval() self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] invalid_input = [-1.0, 2.0, 7.0] with self.assertRaisesOpError("negative input not supported"): MyOperator(invalid_input).eval()

Args:
  • graph: Optional graph to use during the returned session.
  • config: An optional config_pb2.ConfigProto to use to configure the session.
  • use_gpu: If True, attempt to run as many ops as possible on GPU.
  • force_gpu: If True, pin all ops to /gpu:0.
Returns:

A Session object that should be used as a context manager to surround the graph building and execution code in a test case.


tf.test.test_src_dir_path(relative_path)

Creates an absolute test srcdir path given a relative path.

Args:
  • relative_path: a path relative to tensorflow root. e.g. "core/platform".
Returns:

An absolute path to the linked in runfiles.

Utilities


tf.test.assert_equal_graph_def(actual, expected, checkpoint_v2=False)

Asserts that two GraphDefs are (mostly) the same.

Compares two GraphDef protos for equality, ignoring versions and ordering of nodes, attrs, and control inputs. Node names are used to match up nodes between the graphs, so the naming of nodes must be consistent.

Args:
  • actual: The GraphDef we have.
  • expected: The GraphDef we expected.
  • checkpoint_v2: boolean determining whether to ignore randomized attribute values that appear in V2 checkpoints.
Raises:
  • AssertionError: If the GraphDefs do not match.
  • TypeError: If either argument is not a GraphDef.

tf.test.get_temp_dir()

Returns a temporary directory for use during tests.

There is no need to delete the directory after the test.

Returns:

The temporary directory.


tf.test.is_built_with_cuda()

Returns whether TensorFlow was built with CUDA (GPU) support.


tf.test.is_gpu_available(cuda_only=False)

Returns whether TensorFlow can access a GPU.

Args:
  • cuda_only: limit the search to CUDA gpus.
Returns:

True iff a gpu device of the requested kind is available.

Gradient checking

compute_gradient and compute_gradient_error perform numerical differentiation of graphs for comparison against registered analytic gradients.


tf.test.compute_gradient(x, x_shape, y, y_shape, x_init_value=None, delta=0.001, init_targets=None, extra_feed_dict=None)

Computes and returns the theoretical and numerical Jacobian.

If x or y is complex, the Jacobian will still be real but the corresponding Jacobian dimension(s) will be twice as large. This is required even if both input and output is complex since TensorFlow graphs are not necessarily holomorphic, and may have gradients not expressible as complex numbers. For example, if x is complex with shape [m] and y is complex with shape [n], each Jacobian J will have shape [m * 2, n * 2] with

J[:m, :n] = d(Re y)/d(Re x)
J[:m, n:] = d(Im y)/d(Re x)
J[m:, :n] = d(Re y)/d(Im x)
J[m:, n:] = d(Im y)/d(Im x)
Args:
  • x: a tensor or list of tensors

  • x_shape: the dimensions of x as a tuple or an array of ints. If x is a list, then this is the list of shapes.

  • y: a tensor

  • y_shape: the dimensions of y as a tuple or an array of ints.

  • x_init_value: (optional) a numpy array of the same shape as "x" representing the initial value of x. If x is a list, this should be a list of numpy arrays. If this is none, the function will pick a random tensor as the initial value.

  • delta: (optional) the amount of perturbation.

  • init_targets: list of targets to run to initialize model params. TODO(mrry): remove this argument.

  • extra_feed_dict: dict that allows fixing specified tensor values during the Jacobian calculation.

Returns:

Two 2-d numpy arrays representing the theoretical and numerical Jacobian for dy/dx. Each has "x_size" rows and "y_size" columns where "x_size" is the number of elements in x and "y_size" is the number of elements in y. If x is a list, returns a list of two numpy arrays.


tf.test.compute_gradient_error(x, x_shape, y, y_shape, x_init_value=None, delta=0.001, init_targets=None, extra_feed_dict=None)

Computes the gradient error.

Computes the maximum error for dy/dx between the computed Jacobian and the numerically estimated Jacobian.

This function will modify the tensors passed in as it adds more operations and hence changing the consumers of the operations of the input tensors.

This function adds operations to the current session. To compute the error using a particular device, such as a GPU, use the standard methods for setting a device (e.g. using with sess.graph.device() or setting a device function in the session constructor).

Args:
  • x: a tensor or list of tensors

  • x_shape: the dimensions of x as a tuple or an array of ints. If x is a list, then this is the list of shapes.

  • y: a tensor

  • y_shape: the dimensions of y as a tuple or an array of ints.

  • x_init_value: (optional) a numpy array of the same shape as "x" representing the initial value of x. If x is a list, this should be a list of numpy arrays. If this is none, the function will pick a random tensor as the initial value.

  • delta: (optional) the amount of perturbation.

  • init_targets: list of targets to run to initialize model params. TODO(mrry): Remove this argument.

  • extra_feed_dict: dict that allows fixing specified tensor values during the Jacobian calculation.

Returns:

The maximum error in between the two Jacobians.

Other Functions and Classes


class tf.test.Benchmark

Abstract class that provides helpers for TensorFlow benchmarks.


tf.test.Benchmark.is_abstract(cls)


tf.test.Benchmark.report_benchmark(iters=None, cpu_time=None, wall_time=None, throughput=None, extras=None, name=None)

Report a benchmark.

Args:
  • iters: (optional) How many iterations were run
  • cpu_time: (optional) Total cpu time in seconds
  • wall_time: (optional) Total wall time in seconds
  • throughput: (optional) Throughput (in MB/s)
  • extras: (optional) Dict mapping string keys to additional benchmark info. Values may be either floats or values that are convertible to strings.
  • name: (optional) Override the BenchmarkEntry name with name. Otherwise it is inferred from the top-level method name.

tf.test.Benchmark.run_op_benchmark(sess, op_or_tensor, feed_dict=None, burn_iters=2, min_iters=10, store_trace=False, name=None, extras=None, mbs=0)

Run an op or tensor in the given session. Report the results.

Args:
  • sess: Session object to use for timing.
  • op_or_tensor: Operation or Tensor to benchmark.
  • feed_dict: A dict of values to feed for each op iteration (see the feed_dict parameter of Session.run).
  • burn_iters: Number of burn-in iterations to run.
  • min_iters: Minimum number of iterations to use for timing.
  • store_trace: Boolean, whether to run an extra untimed iteration and store the trace of iteration in the benchmark report. The trace will be stored as a string in Google Chrome trace format in the extras field "full_trace_chrome_format".
  • name: (optional) Override the BenchmarkEntry name with name. Otherwise it is inferred from the top-level method name.
  • extras: (optional) Dict mapping string keys to additional benchmark info. Values may be either floats or values that are convertible to strings.
  • mbs: (optional) The number of megabytes moved by this op, used to calculate the ops throughput.
Returns:

A dict containing the key-value pairs that were passed to report_benchmark.