Tensorflow Usage: Babii Andrii
Tensorflow Usage: Babii Andrii
Babii Andrii
andrii.babii@nure.ua
Motivation
TinyFlow
http://dmlc.ml/2016/09/30/build-your-own-tensorflow-with-nnvm-and-torch.html
Syntax (‘Frontend’) like TensorFlow but interpretation is … Torch!
NNVM inspired by LLVM…
It provides ways to construct, represent and transform computation graphs
invariant of how it is executed.
2
TensorFlow basic concepts
In a TensorFlow graph, each node has zero or more inputs and zero or
more outputs, and represents the instantiation of an operation
Values that flow along normal edges in the graph (from outputs to inputs)
are tensors - arbitrary dimensionality arrays where the underlying el-
ement type is specified or inferred at graph-construction time
http://download.tensorflow.org/paper/whitepaper2015.pdf 3
TensorFlow architecture
Python C++ …
There is the client, which uses the session interface to communicate with
the master and one or more worker processes
Each worker process responsible for arbitrating access to one or more
computational devices (such as CPU cores and GPU cards)
TensorFlow Computation graph
https://www.tensorflow.org/
TensorFlow basic concepts. Tensor
A Tensor is a typed multi-dimensional array. For example, a 4-D array of
floating point numbers representing a mini-batch of images with dimensions
[batch, height, width, channel].
In the Python API: class used to represent the output and inputs of ops added
to the graph tf.Tensor. Instances of this class do not hold data.
In the C++ API: class used to represent tensors returned from a Session::Run()
call tensorflow::Tensor. Instances of this class hold data.
https://www.tensorflow.org/versions/r0.9/resources/glossary.html#glossary
TensorFlow basic concepts. Operations
http://download.tensorflow.org/paper/whitepaper2015.pdf
TensoFlow built-in operations
http://download.tensorflow.org/paper/whitepaper2015.pdf
TensorFlow. Execution of computation graph
Single device (for example we have only one core CPU for computation)
The nodes of the graph are executed in an order that
respects the dependencies between nodes
Multi-device execution
•Select device to place the computation for each node in the graph
•Managing the required communication of data
across device boundaries implied by these
placement decisions
http://download.tensorflow.org/paper/whitepaper2015.pdf
9
TensorFlow node placement
http://download.tensorflow.org/paper/whitepaper2015.pdf
10
Cross-device communications
http://download.tensorflow.org/paper/whitepaper2015.pdf
11
TensorFlow. Extensions
•Automatic Differentiation – Automatically computes gradients for data flow graphs.
•Control Flow – Enables support for conditionals and loops in data flow graphs.
•Input Operations – Facilitate efficient loading of data into large scale models from
the storage system.
import tensorflow as tf
a = tf.constant(5.0)
b = tf.constant(3.0)
c = a +b
import tensorflow as tf
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()
# Later, when launching the model
with tf.Session() as sess:
# Run the init operation.
sess.run(init_op)
...
# Use the model
…
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in file: %s" % save_path)
Or we can init variable from value of other variable (it should be initialized before):
w2 = tf.Variable(weights.initialized_value(), name="w2")
Multiplication: tf.matmul(c, d)
16
TensorFlow data input
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
17
TensorFlow data input
Use tf.placeholder variables (dummy nodes that provide entry points for data
to computational graph).
Example:
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
18
TensorFlow data input
Evaluation:
feed_dict={input1:[6.], input2:[3.]}
result
19
TensorFlow namespaces & get_variable
20
Example
Real value
x
x
x Y = X*k + b
x
x Predicted value
21
Example
https://github.com/anrew-git/tf_linear
import numpy as np
Import tensorflow as tf
# Prepre input data for regression. X from 1 to 100 with step 0.1
# Y = X+ 10*cos(X/5)
X_gen = np.arange(100, step=.1)
Y_gen = X_gen + 10 * np.cos(X_gen/5)
#Batch size
batch_size = 100
#Steps number
steps_number = 400
22
Example
23
Example
# Preparing placeholders
24
Example
with tf.variable_scope("linear-regression"):
k = tf.get_variable("weights", (1, 1),
initializer=tf.random_normal_initializer())
b = tf.get_variable("bias", (1,),
initializer=tf.constant_initializer(0.0))
y_predicted = tf.matmul(X, k) + b
25
Example
sess.run(tf.initialize_all_variables())
# Do optimization step
sess.run([opt_operation, loss],
feed_dict={X: X_batch, Y: y_batch})
y_predicted = tf.matmul(X, k) + b
29
TensorFlow auto-differentiation and gradient
30
TensorFlow
31
References
1. http://download.tensorflow.org/paper/whitepaper2015.pdf
2. https://www.tensorflow.org/
3. Getting Started with TensorFlow by Giancarlo Zaccone
4. TensorFlow Machine Learning Cookbook Paperback by Nick McClure
5. https://github.com/aymericdamien/TensorFlow-Examples
6. https://www.tensorflow.org/versions/r0.10/tutorials/index.html
7. http://bcomposes.com/2015/11/26/simple-end-to-end-tensorflow-examples/
8. https://github.com/anrew-git/tf_linear
9. Основные концепции нейронных сетей. Р. Каллан
32
Questions?
33