First start to learn about the graph structure, tensorflow is built upon the nodes
In [9]:
# basic imported headers
import tensorflow as tf
In [ ]:
Second, learn how to fetch the data from the result
In [10]:
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermd = tf.add(input1, input2)
mult = tf.multiply(input3, intermd)
with tf.Session() as sess:
result = sess.run([mult, intermd])
print result
In [3]:
# Create a constant op and adds as a node into the default graph
matrix1 = tf.constant([[3., 3.]])
## Pay attention to this wrong one, DIMENSION
# matrix1 = tf.constant([3., 3.])
matrix2 = tf.constant([[2.], [2.]])
product = tf.matmul(matrix1, matrix2)
with tf.Session() as sess:
print sess.run(product)
Next, we are going to show how to feed data as the parameters
In [6]:
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
print (sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
At last, we are going to learn how to use variable, unlike the placeholder
In [12]:
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
#define the op, or rule to update/assign value
update = tf.assign(state, new_value)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
# print the initial state of state
print sess.run(state)
# use loop to output interatively
for _ in range(3):
sess.run(update)
print sess.run(state)
In [ ]: