|
| 1 | +''' |
| 2 | +Basic Operations example using TensorFlow library. |
| 3 | +
|
| 4 | +Author: Aymeric Damien |
| 5 | +Project: https://github.com/aymericdamien/TensorFlow-Examples/ |
| 6 | +''' |
| 7 | + |
1 | 8 | import tensorflow as tf
|
2 | 9 |
|
3 |
| -#With constants |
| 10 | +# Basic constant operations |
| 11 | +# The value returned by the constructor represents the output |
| 12 | +# of the Constant op. |
4 | 13 | a = tf.constant(2)
|
5 | 14 | b = tf.constant(3)
|
6 | 15 |
|
| 16 | +# Launch the default graph. |
7 | 17 | with tf.Session() as sess:
|
8 | 18 | print "a=2, b=3"
|
9 | 19 | print "Addition with constants: %i" % sess.run(a+b)
|
10 | 20 | print "Multiplication with constants: %i" % sess.run(a*b)
|
11 | 21 |
|
12 |
| - |
13 |
| -#With variables |
| 22 | +# Basic Operations with variable as graph input |
| 23 | +# The value returned by the constructor represents the output |
| 24 | +# of the Variable op. (define as input when running session) |
| 25 | +# tf Graph input |
14 | 26 | a = tf.placeholder(tf.types.int16)
|
15 | 27 | b = tf.placeholder(tf.types.int16)
|
16 | 28 |
|
| 29 | +# Define some operations |
17 | 30 | add = tf.add(a, b)
|
18 | 31 | mul = tf.mul(a, b)
|
19 | 32 |
|
| 33 | +# Launch the default graph. |
20 | 34 | with tf.Session() as sess:
|
| 35 | + # Run every operation with variable input |
21 | 36 | print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
|
22 | 37 | print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})
|
| 38 | + |
| 39 | + |
| 40 | +# ---------------- |
| 41 | +# More in details: |
| 42 | +# Matrix Multiplication from TensorFlow official tutorial |
| 43 | + |
| 44 | +# Create a Constant op that produces a 1x2 matrix. The op is |
| 45 | +# added as a node to the default graph. |
| 46 | +# |
| 47 | +# The value returned by the constructor represents the output |
| 48 | +# of the Constant op. |
| 49 | +matrix1 = tf.constant([[3., 3.]]) |
| 50 | + |
| 51 | +# Create another Constant that produces a 2x1 matrix. |
| 52 | +matrix2 = tf.constant([[2.],[2.]]) |
| 53 | + |
| 54 | +# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs. |
| 55 | +# The returned value, 'product', represents the result of the matrix |
| 56 | +# multiplication. |
| 57 | +product = tf.matmul(matrix1, matrix2) |
| 58 | + |
| 59 | +# To run the matmul op we call the session 'run()' method, passing 'product' |
| 60 | +# which represents the output of the matmul op. This indicates to the call |
| 61 | +# that we want to get the output of the matmul op back. |
| 62 | +# |
| 63 | +# All inputs needed by the op are run automatically by the session. They |
| 64 | +# typically are run in parallel. |
| 65 | +# |
| 66 | +# The call 'run(product)' thus causes the execution of threes ops in the |
| 67 | +# graph: the two constants and matmul. |
| 68 | +# |
| 69 | +# The output of the op is returned in 'result' as a numpy `ndarray` object. |
| 70 | +with tf.Session() as sess: |
| 71 | + result = sess.run(product) |
| 72 | + print result |
| 73 | + # ==> [[ 12.]] |
0 commit comments