In [1]:
import tensorflow as tf

Constants and Sessions


In [2]:
a = tf.constant(1.5)
b = tf.constant(2, dtype=tf.float32)

In [3]:
a


Out[3]:
<tf.Tensor 'Const:0' shape=() dtype=float32>

In [4]:
b


Out[4]:
<tf.Tensor 'Const_1:0' shape=() dtype=float32>

In [5]:
c = a + b

In [6]:
c


Out[6]:
<tf.Tensor 'add:0' shape=() dtype=float32>

In [8]:
with tf.Session() as session:
    res = session.run(c)
    res2 = a.eval()
    print(res)
    print(res2)


3.5
1.5

Variables


In [9]:
state = tf.Variable(0)
one = tf.constant(1)

update = tf.assign(state, state + one)

In [10]:
with tf.Session() as session:
    # It is mandatory to initialize variables before we start running the graph
    session.run(tf.global_variables_initializer())
    print(session.run(state))
    for ind in range(5):
        session.run(update)
        print(session.run(state))


0
1
2
3
4
5

Placeholders to pass data from outside the model


In [11]:
input_data = tf.placeholder(tf.float32)

In [12]:
op = tf.matmul(input_data, tf.transpose(input_data))

In [13]:
with tf.Session() as session:
    res = session.run(op, feed_dict={input_data: [[0, 1, 2, 3]]})
    print(res)


[[ 14.]]

In [ ]: