python-tensorflowHow do I test a Python TensorFlow example?
Testing a Python TensorFlow example can be done in several ways.
- To ensure the example code is running correctly, it can be run in a Python interpreter. For example, the following code can be used to test a simple TensorFlow example:
import tensorflow as tf
# Create a Constant op that produces a 1x2 matrix.
matrix1 = tf.constant([[3., 3.]])
# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])
# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
# To run the matmul op we call the session 'sess' on 'product'
with tf.Session() as sess:
result = sess.run(product)
print(result)
Output example
[[12.]]
- Unit tests can also be used to test a TensorFlow example. For example, the following code can be used to test the same simple TensorFlow example:
import unittest
import tensorflow as tf
class TensorFlowTest(unittest.TestCase):
def test_matmul(self):
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
with tf.Session() as sess:
result = sess.run(product)
self.assertEqual(result.all(), [[12.]].all())
if __name__ == '__main__':
unittest.main()
Output example
..
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
- A debugger can also be used to step through the example code and verify that the expected output is being produced.
Helpful links
More of Python Tensorflow
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use Python TensorFlow with a GPU?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How do I use TensorFlow 1.x with Python?
- How do I use the Xception model in TensorFlow with Python?
- How can I use Tensorflow 1.x with Python 3.8?
- How can I install and use TensorFlow on a Windows machine using Python?
- How can I use XGBoost, Python, and Tensorflow together for software development?
See more codes...