python-tensorflowWhat is TensorFlow in Python?
TensorFlow is an open-source library for numerical computation and machine learning using data flow graphs in Python. It is used for a variety of tasks such as classification, regression, and clustering. It has a comprehensive, flexible ecosystem of tools, libraries, and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications.
Example code
import tensorflow as tf
# Create a Constant op that produces a 1x2 matrix.
# The op is added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
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)
# Launch the default graph.
sess = tf.Session()
# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op. This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session. They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]
Output example
[[12.]]
Code explanation
import tensorflow as tf
- imports the TensorFlow library as thetf
module.matrix1 = tf.constant([[3., 3.]])
- creates a constant op that produces a 1x2 matrix.matrix2 = tf.constant([[2.],[2.]])
- creates a constant op that produces a 2x1 matrix.product = tf.matmul(matrix1, matrix2)
- creates a Matmul op that takesmatrix1
andmatrix2
as inputs and returns the result of the matrix multiplication asproduct
.sess = tf.Session()
- launches the default graph.result = sess.run(product)
- runs the matmul op and returns the output inresult
as a numpyndarray
object.print(result)
- prints the output.
Helpful links
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use TensorFlow Lite with XNNPACK in Python?
- How can I use TensorFlow with Python 3.11?
- How can I use Tensorflow 1.x with Python 3.8?
- How do I use TensorFlow 1.x with Python?
- How can I install and use TensorFlow on a Windows machine using Python?
- How do I use TensorFlow 1.15 with Python?
- How do I use the Xception model in TensorFlow with Python?
See more codes...