BASIC TENSORFLOW

LOAD PACKAGES


In [1]:
import numpy as np
import tensorflow as tf
print ("PACKAGES LOADED")
print ("TENSORFLOW VERSION IS %s" % (tf.__version__))


PACKAGES LOADED
TENSORFLOW VERSION IS 1.0.1

CONSTANT


In [2]:
def print_tf(x):
    print("TYPE IS\n %s" % (type(x)))
    print("VALUE IS\n %s" % (x))
hello = tf.constant("HELLO, WORLD")
print_tf(hello)


TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("Const:0", shape=(), dtype=string)

TO MAKE THINGS HAPPEN


In [3]:
sess = tf.Session()
print ("OPEN SESSION")


OPEN SESSION

GET VALUES


In [4]:
hello_out = sess.run(hello)
print_tf(hello_out)


TYPE IS
 <type 'str'>
VALUE IS
 HELLO, WORLD

OTHER TYPES OF CONSTANTS


In [5]:
a = tf.constant(1.5)
b = tf.constant(2.5)
print_tf(a)
print_tf(b)


TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("Const_1:0", shape=(), dtype=float32)
TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("Const_2:0", shape=(), dtype=float32)

In [6]:
a_out = sess.run(a)
b_out = sess.run(b)
print_tf(a_out)
print_tf(b_out)


TYPE IS
 <type 'numpy.float32'>
VALUE IS
 1.5
TYPE IS
 <type 'numpy.float32'>
VALUE IS
 2.5

OPERATORS


In [7]:
a_plus_b = tf.add(a, b)
print_tf(a_plus_b)


TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("Add:0", shape=(), dtype=float32)

In [8]:
a_plus_b_out = sess.run(a_plus_b)
print_tf(a_plus_b_out)


TYPE IS
 <type 'numpy.float32'>
VALUE IS
 4.0

In [10]:
a_mul_b = tf.multiply(a, b)
a_mul_b_out = sess.run(a_mul_b)
print_tf(a_mul_b_out)


TYPE IS
 <type 'numpy.float32'>
VALUE IS
 3.75

VARIABLES


In [11]:
weight = tf.Variable(tf.random_normal([5, 2], stddev=0.1))
print_tf(weight)


TYPE IS
 <class 'tensorflow.python.ops.variables.Variable'>
VALUE IS
 Tensor("Variable/read:0", shape=(5, 2), dtype=float32)

TO GET VALUES...?


In [12]:
weight_out = sess.run(weight)
print_tf(weight_out)


---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-12-e453db2b7ada> in <module>()
----> 1 weight_out = sess.run(weight)
      2 print_tf(weight_out)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    963     if final_fetches or final_targets:
    964       results = self._do_run(handle, final_targets, final_fetches,
--> 965                              feed_dict_string, options, run_metadata)
    966     else:
    967       results = []

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1013     if handle is None:
   1014       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1015                            target_list, options, run_metadata)
   1016     else:
   1017       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1033         except KeyError:
   1034           pass
-> 1035       raise type(e)(node_def, op, message)
   1036 
   1037   def _extend_graph(self):

FailedPreconditionError: Attempting to use uninitialized value Variable
	 [[Node: Variable/_8 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_4_Variable", _device="/job:localhost/replica:0/task:0/gpu:0"](Variable)]]

FOR VARIABLES, WE SHOULD DO


In [13]:
init = tf.global_variables_initializer()
sess.run(init)
print ("INITIALIZING ALL VARIALBES")


INITIALIZING ALL VARIALBES

ONCE, WE DO THIS


In [14]:
weight_out = sess.run(weight)
print_tf(weight_out)


TYPE IS
 <type 'numpy.ndarray'>
VALUE IS
 [[-0.20289104 -0.13041103]
 [-0.05912723 -0.10989689]
 [ 0.07426394 -0.08165238]
 [-0.19318706  0.0113402 ]
 [-0.02975834 -0.07080634]]

PLACEHOLDERS


In [15]:
x = tf.placeholder(tf.float32, [None, 5])
print_tf(x)


TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("Placeholder:0", shape=(?, 5), dtype=float32)

OPERATION WITH PLACEHOLDERS


In [16]:
oper = tf.matmul(x, weight)
print_tf(oper)


TYPE IS
 <class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
 Tensor("MatMul:0", shape=(?, 2), dtype=float32)

FEED DATA WITH FEED_DICT


In [17]:
data = np.random.rand(1, 5)
oper_out = sess.run(oper, feed_dict={x: data})
print_tf(oper_out)


TYPE IS
 <type 'numpy.ndarray'>
VALUE IS
 [[-0.22608797 -0.22412241]]

CAN HANLDE MULTIPLE DATA


In [18]:
data = np.random.rand(10, 5)
oper_out = sess.run(oper, feed_dict={x: data})
print_tf(oper_out)


TYPE IS
 <type 'numpy.ndarray'>
VALUE IS
 [[-0.13164151 -0.14662519]
 [-0.03979556 -0.11639561]
 [-0.35830581 -0.17749062]
 [-0.39543742 -0.26551411]
 [-0.0840186  -0.1913866 ]
 [-0.23334444 -0.22994462]
 [-0.04922917 -0.17242573]
 [-0.12115169 -0.18672167]
 [-0.32352611 -0.2245672 ]
 [-0.13667051 -0.17885914]]