02. TensorFlow

Basic of tensorflow

TOC

tensorflow

TensorFlow

  • An open source software library for numerical computation using data flow graphs.

Data Flow Graph

  • Nodes in the graph represent mathematical operations.
  • Egges represent the multidimensional data arrays(tensors) communicated between them.
dataflow
Image 1. Data Flow Graph (src: https://www.tensorflow.org/)

Hello TensorFlow!

import tensorflow as tf

# Creates a constant node
hello = tf.constant("Hello, TensorFlow!")

# Start a TensorFlow session
sess = tf.Session()

# Run the operation and print result
print(sess.run(hello))
b'Hello, TensorFlow!'

Computation Graph

import tensorflow as tf

# Defines constant and operation nodes
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # tf.float32 can be implicitly given.
node3 = tf.add(node1, node2)

# If node is printed, only type information will be displayed
print("node1:", node1)
print("node2:", node2)
print("node3:", node3)

# To get outputs of each nodes, session should run the data flow graph.
sess = tf.Session()

# Session can run single node or list of nodes
print("sess.run(node1, node2): ", sess.run([node1, node2]))
print("sess.run(node3): ", sess.run(node3))
node1: Tensor("Const_3:0", shape=(), dtype=float32)
node2: Tensor("Const_4:0", shape=(), dtype=float32)
node3: Tensor("Add:0", shape=(), dtype=float32)
sess.run(node1, node2):  [3.0, 4.0]
sess.run(node3):  7.0

TensorFlow Mechanics

  1. Build graph using TensoFlow operations
    • define nodes
  2. Feed data and run grap (operation)
    • sess.run(node)
  3. Update variables in the graph and return values
    • print(output of sess.run(node))
mechanics
Image 2. TensorFlow Mechanics (src: www.mathwarehouse.com)

Placeholder

  • For the second step of TensorFlow Mechanics, data should be fed to the node in the data graph.
  • Placeholder is a special tensorflow's node, which can be fed.
import tensorflow as tf

# Defines placeholders
node1 = tf.placeholder(tf.float32)
node2 = tf.placeholder(tf.float32)

# Defines an operation
node3 = node1 + node2

# Defines a session
sess = tf.Session()

# Run session with fed data
# feed_dict is a dictionary data type,
#  and single value and list can be injected.
print(sess.run(node3, feed_dict={node1: 3, node2: 4}))

print(sess.run(node3, feed_dict={node1: [3, 5], node2: [4, 6]}))
7.0
[  7.  11.]

Everything is Tensor

Tensor rank

Rank Math entity Python example
0 Scalar (magnitude only) s = 100
1 Vector (magnitude and direction) v = [1,2,3]
2 Matrix (table of numbers) m = [[1,2,3], [1,2,3]]
3 3-Tensor (cube of numbers) t = [[[2], [4], [6]], [[8], [9], [10]]]
4 n-Tensor (you get the idea) ...
Table 1. Rank

Tensor shape

Rank Shape Dimension number Example
0 [] 0-D A 0-D tensor. A Scalar
1 [D0] 1-D A 1-D tensor with shape [5]
2 [D0, D1] 2-D A 2-D tensor with shape [3, 4]
3 [D0, D1, D2] 3-D A 3-D tensor with shape [1,4,3]
n [D0, D1,..., Dn-1] n-D A tensor with shape [D0, D1, ..., Dn-1]
Table 2. Shape

Data type

Data type Python type Description
DT_FLOAT tf.float32 32 bits floating point
DT_DOUBLE tf.float64 64 bits floating point
DT_INT8 tf.int8 8 bits signed integer
DT_INT16 tf.int16 16 bits signed integer
DT_INT32 tf.int32 32 bits sigend integer
DT_INT64 tf.int64 64 bits sigend integer
Table 3. Type

COMMENTS

Name

0 weights,1,abstract class,1,active function,3,adam,2,Adapter,1,affine,2,argmax,1,back propagation,3,binary classification,3,blog,2,Bucket list,1,C++,11,Casting,1,cee,1,checkButton,1,cnn,3,col2im,1,columnspan,1,comboBox,1,concrete class,1,convolution,2,cost function,6,data preprocessing,2,data set,1,deep learning,31,Design Pattern,12,DIP,1,django,1,dnn,2,Don't Repeat Your code,1,drop out,2,ensemble,2,epoch,2,favicon,1,fcn,1,frame,1,gradient descent,5,gru,1,he,1,identify function,1,im2col,1,initialization,1,Lab,9,learning rate,2,LifeLog,1,linear regression,6,logistic function,1,logistic regression,3,logit,3,LSP,1,lstm,1,machine learning,31,matplotlib,1,menu,1,message box,1,mnist,3,mse,1,multinomial classification,3,mutli layer neural network,1,Non Virtual Interface,1,normalization,2,Note,21,numpy,4,one-hot encoding,3,OOP Principles,2,Open Close Principle,1,optimization,1,overfitting,1,padding,2,partial derivative,2,pooling,2,Prototype,1,pure virtual function,1,queue runner,1,radioButton,1,RBM,1,regularization,1,relu,2,reshape,1,restricted boltzmann machine,1,rnn,2,scrolledText,1,sigmoid,2,sigmoid function,1,single layer neural network,1,softmax,6,softmax classification,3,softmax cross entropy with logits,1,softmax function,2,softmax regression,3,softmax-with-loss,2,spinBox,1,SRP,1,standardization,1,sticky,1,stride,1,tab,1,Template Method,1,TensorFlow,31,testing data,1,this,2,tkinter,5,tooltip,1,Toplevel,1,training data,1,vanishing gradient,1,Virtual Copy Constructor,1,Virtual Destructor,1,Virtual Function,1,weight decay,1,xavier,2,xor,3,
ltr
item
Universe In Computer: 02. TensorFlow
02. TensorFlow
Basic of tensorflow
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s0/
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s72-c/
Universe In Computer
https://kunicom.blogspot.com/2017/06/02-tensorflow.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2017/06/02-tensorflow.html
true
2543631451419919204
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy