63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
|
|
"""
|
|
@authors: TensorFlow Team (understood and improved by Sam')
|
|
"""
|
|
|
|
|
|
import tensorflow as tf
|
|
|
|
|
|
"""
|
|
Create a Constant operation that produces a 1x2 matrix.
|
|
The operation is added as a node to the default graph of TensorFlow.
|
|
The value returned by the constructor represents the output
|
|
of the Constant operation.
|
|
"""
|
|
matrix1 = tf.constant([[3., 3.]])
|
|
|
|
"""
|
|
Create another Constant that produces a 2x1 matrix.
|
|
"""
|
|
matrix2 = tf.constant([[2.], [2.]])
|
|
|
|
"""
|
|
Create a 'matmul' operation that takes 'matrix1' and 'matrix2' as inputs.
|
|
The returned value, 'product', represents the result of
|
|
the matrix multiplication, the output.
|
|
"""
|
|
product = tf.matmul(matrix1, matrix2)
|
|
|
|
"""
|
|
For the next statement, we can easily choose the device to use for
|
|
graph computation.
|
|
/!\ In order to use a GPU device, you will need both CUDA and cuDDN
|
|
installed on your system /!\
|
|
"""
|
|
with tf.device("/cpu:0"):
|
|
|
|
"""
|
|
Let's create a session to compute our TensorFlow graph.
|
|
"""
|
|
with tf.Session() as sess:
|
|
|
|
"""
|
|
To run the 'matmul' operation we call the session 'run()' method,
|
|
passing 'product' as parameter.
|
|
This indicates to the call that we want to get the output of
|
|
the 'matmul' operation back.
|
|
All inputs needed by the operation are run automatically
|
|
by the session. They typically are run in parallel.
|
|
The call 'run(product)' thus causes the execution of three operations
|
|
in the graph: the two constants and 'matmul'.
|
|
The output of the operations is returned in 'result' as
|
|
a numpy `ndarray` object.
|
|
"""
|
|
print(sess.run(matrix2)) # ==> [[ 12.]]
|
|
|
|
"""
|
|
Close the session when we're done.
|
|
"""
|
|
sess.close()
|