In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

In [2]:
%matplotlib inline

In [3]:
a=tf.random_normal([2,20])

In [4]:
sess=tf.Session()

In [5]:
out=sess.run(a)

In [6]:
print(out)


[[-1.01132369 -0.03734883 -1.50488603  0.05773655  0.66317093 -1.22423506
  -1.81542122  1.57534635 -0.14326067  2.08635116 -0.79781163 -1.12896252
   1.26442504 -0.26508138 -0.05267891  0.99382091  0.29550475  0.50181234
   1.28935134  1.03054881]
 [-0.79155535  0.21548827 -0.07632679 -0.20849626  0.64689469 -0.98350734
   1.59773135  2.77432394 -0.90824676 -1.14271474 -0.55331206 -0.81772059
  -0.02598415  0.43808228 -0.93310952 -0.33111054  1.91047168  0.71959078
  -0.75361609  1.29549837]]

In [7]:
x,y=out

In [8]:
plt.scatter(x,y)


Out[8]:
<matplotlib.collections.PathCollection at 0x7fbc48beb9b0>

In [9]:
a=tf.constant(5,name='input_a')
b=tf.constant(3,name='input_b')
c=tf.multiply(a,b,name='mul_c')
d=tf.add(a,b,name='add_d')
e=tf.add(c,d,name='add_e')

In [10]:
sess=tf.Session()

In [11]:
sess.run(e)


Out[11]:
23

In [16]:
writer=tf.summary.FileWriter('my_graph',sess.graph)

The above command will output graph to my_graph directory Then run the command '$ tensorboard --logdir="my_graph"' type http://localhost:6006 in the browser


In [17]:
writer.close()

In [18]:
sess.close()

In [3]:
a=tf.constant([5,3],name='input_a')

In [6]:
b=tf.reduce_prod(a,name='prod_b')

In [7]:
c=tf.reduce_sum(a,name='sum_c')

In [8]:
d=tf.add(b,c,name='add_d')

In [9]:
sess=tf.Session()

In [10]:
sess.run(d)


Out[10]:
23

Tensors, as mentioned before, are simply the n-dimensional abstractionof matrices. So a 1-D tensor would be equivalent to a vector, a 2-Dtensor is a matrix, and above that you can just say “N-D tensor”.

Comparing to last example, this one has only one input which is a vector, so simplify the graph.And also reduce to only one dependency to check.


In [12]:
help(tf.placeholder)


Help on function placeholder in module tensorflow.python.ops.array_ops:

placeholder(dtype, shape=None, name=None)
    Inserts a placeholder for a tensor that will be always fed.
    
    **Important**: This tensor will produce an error if evaluated. Its value must
    be fed using the `feed_dict` optional argument to `Session.run()`,
    `Tensor.eval()`, or `Operation.run()`.
    
    For example:
    
    ```python
    x = tf.placeholder(tf.float32, shape=(1024, 1024))
    y = tf.matmul(x, x)
    
    with tf.Session() as sess:
      print(sess.run(y))  # ERROR: will fail because x was not fed.
    
      rand_array = np.random.rand(1024, 1024)
      print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.
    ```
    
    Args:
      dtype: The type of elements in the tensor to be fed.
      shape: The shape of the tensor to be fed (optional). If the shape is not
        specified, you can feed a tensor of any shape.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor` that may be used as a handle for feeding a value, but not
      evaluated directly.


In [13]:
a=tf.placeholder(tf.int32,shape=(2),name='input_a')

In [14]:
b=tf.reduce_prod(a,name='prod_b')

In [15]:
c=tf.mul(b,3,name='mul_c')

In [16]:
sess=tf.Session()

In [19]:
input_dict={a:np.array([3,5],dtype=np.int32)}

In [20]:
sess.run(c,feed_dict=input_dict)


Out[20]:
45

This way create a placeholder which hold variable values