Basic of tensorflow
TOC
- TensorFlow
- Data Flow Graph
- Hello TensorFlow!
- Computation Graph
- TensorFlow Mechanics
- Placeholder
- Everything is Tensor
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.
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
- Build graph using TensoFlow operations
- define nodes
- Feed data and run grap (operation)
- sess.run(node)
- Update variables in the graph and return values
- print(output of sess.run(node))
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