BASIC TENSORFLOW
LOAD PACKAGES
import numpy as np
import tensorflow as tf
print ("PACKAGES LOADED")
PACKAGES LOADED
SESSION
sess = tf.Session()
print ("OPEN SESSION")
OPEN SESSION
TF CONSTANT
def print_tf(x):
print("TYPE IS\n %s" % (type(x)))
print("VALUE IS\n %s" % (x))
hello = tf.constant("HELLO. IT'S ME. ")
print_tf(hello)
TYPE IS
<class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
Tensor("Const:0", shape=(), dtype=string)
TO MAKE THINKS HAPPEN
hello_out = sess.run(hello)
print_tf(hello_out)
TYPE IS
<type 'str'>
VALUE IS
HELLO. IT'S ME.
OTHER TYPES OF CONSTANTS
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)
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
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)
a_plus_b_out = sess.run(a_plus_b)
print_tf(a_plus_b_out)
TYPE IS
<type 'numpy.float32'>
VALUE IS
4.0
a_mul_b = tf.mul(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
weight = tf.Variable(tf.random_normal([5, 2], stddev=0.1))
print_tf(weight)
TYPE IS
<class 'tensorflow.python.ops.variables.Variable'>
VALUE IS
<tensorflow.python.ops.variables.Variable object at 0x7ff2bc04c050>
weight_out = sess.run(weight)
print_tf(weight_out)
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
<ipython-input-11-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)
338 try:
339 result = self._run(None, fetches, feed_dict, options_ptr,
--> 340 run_metadata_ptr)
341 if run_metadata:
342 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)
562 try:
563 results = self._do_run(handle, target_list, unique_fetches,
--> 564 feed_dict_string, options, run_metadata)
565 finally:
566 # The movers are no longer used. Delete them.
/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)
635 if handle is None:
636 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 637 target_list, options, run_metadata)
638 else:
639 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)
657 # pylint: disable=protected-access
658 raise errors._make_specific_exception(node_def, op, error_message,
--> 659 e.code)
660 # pylint: enable=protected-access
661
FailedPreconditionError: Attempting to use uninitialized value Variable
[[Node: Variable/_14 = _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_25_Variable", _device="/job:localhost/replica:0/task:0/gpu:0"](Variable)]]
[[Node: Variable/_15 = _Recv[_start_time=0, 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_25_Variable", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
WHY DOES THIS ERROR OCCURS?
init = tf.initialize_all_variables()
sess.run(init)
print ("INITIALIZING ALL VARIALBES")
INITIALIZING ALL VARIALBES
ONCE, WE INITIALIZE VARIABLES
weight_out = sess.run(weight)
print_tf(weight_out)
TYPE IS
<type 'numpy.ndarray'>
VALUE IS
[[ 0.08847231 -0.08040368]
[-0.00344782 -0.32673332]
[ 0.10427399 -0.12950435]
[ 0.19032514 -0.1323577 ]
[-0.00949183 -0.10073283]]
PLACEHOLDERS
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 VARIABLES AND PLACEHOLDERS
oper = tf.matmul(x, weight)
print_tf(oper)
TYPE IS
<class 'tensorflow.python.framework.ops.Tensor'>
VALUE IS
Tensor("MatMul_1:0", shape=(?, 2), dtype=float32)
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.15189362 -0.18824303]]
data = np.random.rand(2, 5)
oper_out = sess.run(oper, feed_dict={x: data})
print_tf(oper_out)
TYPE IS
<type 'numpy.ndarray'>
VALUE IS
[[ 0.25157502 -0.50201333]
[ 0.11929326 -0.4199847 ]]