Quick Learn-Tensorflow With Sample Code
TensorFlow is an open-source machine learning framework that is used for machine learning applications such as neural networks and deep learning. It is a Python-friendly open source symbolic math library for numerical computation that makes machine learning faster and easier which might be your new best friend if you have a lot of data
What is ‘Tensor’ in tensorflow
A tensor consists of a set of primitive values shaped into an array of any number of dimensions. Actually TensorFlow represents tensors as n-dimensional arrays of base datatypes where the N-dimensional array or ndarray is usually a fixed-size multidimensional container of items of the same type and size.
Variable In Tensorflow
In programming, a variable is a value that can change, depending on conditions or on information passed to the program. In TensorFlow, variables maintain state across executions of the graphIn. Defining variables is necessary to hold the parameter. Without having parameters, training, updating, saving, restoring and any other operations cannot be performed. The defined variables in TensorFlow are just tensors with certain shapes and types.
When we define a variable, we basically pass a tensor and its value to the graph. Variables are manipulated via the tf.Variable class. A tf.Variable represents a tensor whose value can be changed by running ops on it.
Nodes in the graph are called ops (short for operations). An op takes zero or more Tensors, performs some computation, and produces zero or more Tensors.
How variable is used in Python . Let us take an example
x = 100
y = x + 5
print(y)
To do the same thing in tensorflow
import tensorflow as tf
x = tf.constant(100, name='x')
y = tf.Variable(x + 5, name='y')
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
print("I do not print the value")
print(x)
print("I print the value")
print(session.run(x))
print(session.run(y))
Output:
I do not print the value
Tensor("x:0", shape=(), dtype=int32)
I print the value
100
105
What is happening in the above case . As you can see only using print(x) is not giving the result we need to use session.run to get the actual result
To compute anything, a graph must be launched in a session.
A Session places the graph ops onto Devices, such as CPUs or GPUs, and provides methods to execute them.
import tensorflow as tf
# Build a graph.
num1 = tf.constant(100.0)
num2 = tf.constant(10.0)
result = num1 * num2
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor result.
print(sess.run(result))
# Hello World Example
# Import tensorflow
import tensorflow as tf
hello = tf.constant('Hello World')
hellosession = tf.Session()
print(hellosession.run(hello))
Output: 'Hello World'
TensorFlow has its own data types. Though TensorFlow accepts Python native types like booleans, strings and numeric (int, float). But you should use the tensorflow data types instead.
Commonly used operators
- Adding Tf.add
Subtracting tf.subtract
Multiply tf.multiply
Other Operators
tf.pow(x, y) Take the element-wise power of x to y
tf.exp(x) Equivalent to pow(e, x), where e is Euler’s number (2.718…)
tf.sqrt(x) Equivalent to pow(x, 0.5)
tf.div(x, y) Take the element-wise division of x and y
tf.truediv(x, y) Same as tf.div, except casts the arguments as a float
tf.floordiv(x, y) Same as truediv, except rounds down the final answer into an integer
tf.mod(x, y) Takes the element-wise remainder from division