Skip to content

Commit 05510af

Browse files
committed
update basic op + update comments
1 parent b6716a7 commit 05510af

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

basic_operations.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,73 @@
1+
'''
2+
Basic Operations example using TensorFlow library.
3+
4+
Author: Aymeric Damien
5+
Project: https://github.com/aymericdamien/TensorFlow-Examples/
6+
'''
7+
18
import tensorflow as tf
29

3-
#With constants
10+
# Basic constant operations
11+
# The value returned by the constructor represents the output
12+
# of the Constant op.
413
a = tf.constant(2)
514
b = tf.constant(3)
615

16+
# Launch the default graph.
717
with tf.Session() as sess:
818
print "a=2, b=3"
919
print "Addition with constants: %i" % sess.run(a+b)
1020
print "Multiplication with constants: %i" % sess.run(a*b)
1121

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
1426
a = tf.placeholder(tf.types.int16)
1527
b = tf.placeholder(tf.types.int16)
1628

29+
# Define some operations
1730
add = tf.add(a, b)
1831
mul = tf.mul(a, b)
1932

33+
# Launch the default graph.
2034
with tf.Session() as sess:
35+
# Run every operation with variable input
2136
print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
2237
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.]]

helloworld.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
'''
2+
HelloWorld example using TensorFlow library.
3+
4+
Author: Aymeric Damien
5+
Project: https://github.com/aymericdamien/TensorFlow-Examples/
6+
'''
7+
18
import tensorflow as tf
29

310
#Simple hello world using TensorFlow
411

12+
# Create a Constant op
13+
# The op is added as a node to the default graph.
14+
#
15+
# The value returned by the constructor represents the output
16+
# of the Constant op.
517
hello = tf.constant('Hello, TensorFlow!')
618

19+
# Start tf session
720
sess = tf.Session()
821

922
print sess.run(hello)

0 commit comments

Comments
 (0)