"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

August 23, 2018

Day #120 - Tensorflow

Tensor (N-Dimensional array of data)
  • High Performance Library for Numerical Computation
  • Represented as Directed Graphs
  • DAG (Directed Acyclic Graphs)
  • Edges - Arrays of Data
DAG
  • Language independent version of representation
  • Similar to JVM
  • Tensorflow engine written in C++
  • Tensorflow Lite - On device interference of ML Models
API Hierarchy
  • Number of abstraction layers
  • High Level API -> tf.estimator
  • tf.layers, tf.losses, tf.metrics -> Custom NN Models
  • Core Tensorflow Python
  • Core Tensorflow C++
  • CPU / GPU / TPU / Android
Execution
  • Code
  • Tensors Definition
  • Creates DAG
  • Run DAG in Session
  • Lazy Evaluation model (minimize context switches)
Graph and Session
  • Explicit edges to represent dependencies
  • Helps to partition and run parallel pieces
import numpy as np
import tensorflow as tf
#numpy
a = np.array([5,3,8])
b = np.array([3,-1,2])
c = np.add(a,b)
print('numpy data')
print(c)
#Tensorflow
#Build
a = tf.constant([5,3,8])
b = tf.constant([3,-1,2])
c = tf.add(a,b)
print(c)
#scalar
x = tf.constant(4)
#vector
x = tf.constant([3,5,7])
#matrix
#2D
x = tf.constant([[3,5,7],[4,6,8]])
#Get 0th column
y = x[:,0]
#Get the 0th row
z = x[0,:]
#get two values only
#start at 0, fetch two elements
#python range first number included last number is not
zz = x[0,:2]
#rewrite into 3x2 rows
yy = tf.reshape(x,[3,2])
#rewrite into 6x1 rows
yyy = tf.reshape(x,[6,1])
#3D
x = tf.constant([[[3,5,7],[4,6,8]]])
#placeholders to feed value
data = tf.placeholder("float",None)
b = data*2
#Run
with tf.Session() as sess:
result = sess.run(c)
print('tensor data')
print(result)
print('c evaluation')
print(c.eval())
print(y.eval())
print(z.eval())
print(zz.eval())
print(yy.eval())
print(yyy.eval())
print(sess.run(b,feed_dict={data: [1,2,3]}))
view raw tensor1.py hosted with ❤ by GitHub

 

Happy Learning!!!

No comments: