Sunday, December 31, 2017

Machine Learning with TensorFlow

Over past decade, there has been an exponential rise in the data we collect as the cost of hardware decreases and processing power has surged. Data whether it may be in form of a collection of pictures, social media messages, scientific readings, stock exchange, GPS tracking data, activity monitoring, news contains seemingly valuable information which could tells us about the public trends, entity relationships such as cause and effect, pattern repetitions and various other insights. This helps us in deep understanding about the actors and the environment in which the data is generated. Remarkably this data can also be used by computers to train themselves by determining the patterns, generate models which later can be used to query about the data, but further also to make future predictions. During the 2017 Google I/O Conference, were few improvements were announced, smart computers using Machine learning and other AI techniques were envisioned as next phase in computer technology.
    Deep learning is one of the broader methods of machine learning which learns the high level features from the data itself. In deep learning, multiple artificial neurons stacked up as layers perform individual functions and serve as an input to the next layer.

TensorFlow

TensorFlow is software library for dataflow programming released by Google in 2015 which is used implementing deep learning models. TensorFlow first defines an abstract model which defines the computations, called the Computational Graph. The computational graph then runs within a session to make the model a reality. A Computational Graph is a series of TensorFlow operations arranged into a graph of nodes. When Computational Graph is defined all the operations are created without holding any values or running any calculations. Below is an example of an computational graph.
import tensorflow as tf

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = node1 * node2
print(node1, node2, node3)

The TensorFlow session's allows to execute computational graph or a part of the graph, and producing actual results as shown below. The session encapsulates the control and state of the TensorFlow runtime.
session = tf.Session()
print(session.run([node1, node2, node3]))
session.close()

Data is represented in form of tensors in TensoreFlow. A tensor is a multi dimensional arrays or a lists, for example, an array is a 1-dimensional tensor, a matrix is 2-dimensional tensor, a three dimensional matrix is a 3-dimensional tensor. Tensors are described by a unit of dimensions called the rank.

Rank Math entity Python example
0 Scalar (magnitude only) s = 483
1 Vector (magnitude and direction) v = [1.1, 2.2, 3.3]
2 Matrix (table of numbers) m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3 3-Tensor (cube of numbers) t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
n n-Tensor ....


TensorBoard is a suite of web applications for visualizing and understanding TensoreFlow graphs. To create a TensorFlow graph FileWriter is used to output the graph to a directory.
session = tf.Session()
File = tf.summary.FileWriter('log_simple_graph', session.graph)
session.close()
TensorBoard runs as a local web app, on default port 6006 on executing the command tensorboard --logdir="path_to_the_graph".

DataTypes in Tensorflow

Constant nodes takes no inputs and outputs the value it stores internally.
Placeholder is a parameter of the graph that can accept external inputs. It is a promise to provide a value later. Below is an example.
import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

adder_node = a + b
session = tf.Session()

print(session.run(adder_node,{a: [1,3], b: [2,4]}))
session.close()

Variable allows to add a trainable parameters to a graph. They are used to hold and update parameters while training a model. Variable must be initialized before using them unlike constants and placeholders as below.
import tensorflow as tf

W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)

linear_model = W * x + b

init = tf.global_variables_initialzer()

session = tf.Session()
session.run(init)
print(session.run(linear_model, {x:[1,2,3,4]}))
session.close()


No comments: