Thursday, November 5, 2020

Tensorflow Tutorial for Beginners



         TensorFlow is Google’s framework for deep learning.
 Deep learning algorithms have been used for several years across many products and areas at Google, such as search, translation, advertising, computer vision, and speech recognition. TensorFlow is, in fact, a second-generation system for implementing and deploying deep neural networks at Google

        TensorFlow was released to the public as an open source framework with an Apache 2.0 license in November 2015 and has already taken the industry by storm, with adoption going far beyond internal Google projects.

Using TensorFlow for AI Systems

  • One primary area where deep learning is truly shining is computer vision. A fundamental task in computer vision is image classification—building algorithms and systems that receive images as input, and return a set of categories that best describe them. 
  • One exciting area of deep learning research for building machine intelligence systems is focused on generating natural language descriptions for visual content. Like seeing the image and explaining what is there in the image.
  • Natural language understanding (NLU) is a key capability for building AI systems.  One of the most sought-after abilities is to summarize text, taking long documents and generating succinct and coherent sentences that extract the key information from the original texts

A High-Level Overview

        Tensors are the standard way of representing data in deep learning. Simply put, tensors are just multidimensional arrays, an extension of two-dimensional tables (matrices) to data with higher dimensionality.

        In TensorFlow, computation is approached as a dataflow graph.Broadly speaking, in this graph, nodes represent operations (such as addition or multiplication), and edges represent data (tensors) flowing around the system. 


        TensorFlow, in the most general terms, is a software framework for numerical computations based on dataflow graphs. It is designed primarily, however, as an interface for expressing and implementing machine learning algorithms, chief among them deep neural networks.

        The core of TensorFlow is in C++, and it has two primary high-level frontend languages and interfaces for expressing and executing the computation graphs. The most developed frontend is in Python, used by most researchers and data scientists.

Installing TensorFlow

        If you are using a clean Python installation (probably set up for the purpose of learning TensorFlow), you can get started with the simple pip installation

$ pip install tensorflow

This approach does, however, have the drawback that TensorFlow will override existing packages and install specific versions to satisfy dependencies. If you are using this Python installation for other purposes as well, this will not do. One common way around this is to install TensorFlow in a virtual environment, managed by a utility called virtualenv.

$ pip install virtualenv

In order to install TensorFlow in a virtual environment, you must first create the virtual environment.

$ cd ~
$ mkdir envs
$ virtualenv ~/envs/tensorflow


This will create a virtual environment named tensorflow in ~/envs (which will manifest as the folder ~/envs/tensorflow). To activate the environment, use:

$ source ~/envs/tensorflow/bin/activate
The prompt should now change to indicate the activated environment:

(tensorflow)$
At this point the pip install command:

(tensorflow)$ pip install tensorflow
will install TensorFlow into the virtual environment, without impacting other packages installed on your machine.


Finally, in order to exit the virtual environment, you type:

(tensorflow)$ deactivate

Simple hello world (helloWorld.py)

import tensorflow as tf
h = tf.constant("Hello")
w = tf.constant(" World!")
hw = h + w
with tf.Session() as sess:
    ans = sess.run(hw)

Computation Graphs

        TensorFlow allows us to implement machine learning algorithms by creating and computing operations that interact with one another. These interactions form what we call a “computation graph,” with which we can intuitively represent complicated functional architectures.
        In TensorFlow, each of the graph’s nodes represents an operation, possibly applied to some input, and can generate an output that is passed on to other nodes.
TensorFlow involves two main phases: 
  1. Constructing a graph 
  2. Executing it.

Constructing and Managing Our Graph

        As we import TensorFlow, a default graph is automatically created for us. We can create additional graphs and control their association with some given operations. tf.Graph() creates a new graph, represented as a TensorFlow object. 

import tensorflow as tf
print(tf.get_default_graph())
g = tf.Graph()
print(g)


Out:
<tensorflow.python.framework.ops.Graph object at 0x7fd88c3c07d0>
<tensorflow.python.framework.ops.Graph object at 0x7fd88c3c03d0>

Data Types

    The basic units of data that pass through a graph are numerical, Boolean, or string elements. 

c = tf.constant(4.0, dtype=tf.float64) 

print(c) 

print(c.dtype) 


Out: Tensor("Const_10:0", shape=(), dtype=float64) <dtype: 

'float64'>

Data type

Python type

Description

DT_FLOAT

tf.float32

32-bit floating point.

DT_DOUBLE

tf.float64

64-bit floating point.

DT_INT8

tf.int8

8-bit signed integer.

DT_INT16

tf.int16

16-bit signed integer.

DT_INT32

tf.int32

32-bit signed integer.

DT_INT64

tf.int64

64-bit signed integer.

DT_UINT8

tf.uint8

8-bit unsigned integer.

DT_UINT16

tf.uint16

16-bit unsigned integer.

DT_STRING

tf.string

Variable-length byte array. Each element of a Tensor is a byte array.

DT_BOOL

tf.bool

Boolean.

DT_COMPLEX64

tf.complex64

Complex number made of two 32-bit floating points: real and imaginary parts.

DT_COMPLEX128

tf.complex128

Complex number made of two 64-bit floating points: real and imaginary parts.

DT_QINT8

tf.qint8

8-bit signed integer used in quantized ops.

DT_QINT32

tf.qint32

32-bit signed integer used in quantized ops.

DT_QUINT8

tf.quint8

8-bit unsigned integer used in quantized ops.

Tenserflow Operations

TensorFlow operation

Description

tf.constant(value)

Creates a tensor populated with the value or values specified by the argument value

tf.fill(shapevalue)

Creates a tensor of shape shape and fills it with value 

tf.zeros(shape)

Returns a tensor of shape shape with all elements set to 0

tf.zeros_like(tensor)

Returns a tensor of the same type and shape as tensor with all elements set to 0

tf.ones(shape)

Returns a tensor of shape shape with all elements set to 1

tf.ones_like(tensor)

Returns a tensor of the same type and shape as tensor with all elements set to 1

tf.random_normal(shape,meanstddev)

Outputs random values from a normal distribution

tf.truncated_normal(shape,meanstddev)

Outputs random values from a truncated normal distribution (values whose magnitude is more than two standard deviations from the mean are dropped and re-picked)

tf.random_uniform(shape,minvalmaxval)

Generates values from a uniform distribution in the range [minvalmaxval)

tf.random_shuffle(tensor)

Randomly shuffles a tensor along its first dimension 

Related topics:

Introduction to Artificial Intelligence (AI) and Machine Learning

Python tutorial for beginners(Python Basics)

Best Tenserflow related book :

Learn TensorFlow 2.0: Implement Machine Learning and Deep Learning Models with Python

Pro Deep Learning with TensorFlow: A Mathematical Approach to Advanced Artificial Intelligence in Python

You may also like :

Internet of Things (IOT) Basics

Android Basic Concepts

Did you find our blog helpful ? Share in comment section. Feel free to share on Twitter or Facebook by using the share buttons

Share:

0 comments:

Post a Comment

Popular Posts

Contact Form

Name

Email *

Message *