Tensorflow in Python

In this Python tutorial, we will discuss the Tensorflow library, What is TensorflowHow to use it in Python and we will also cover the following topics:

  • What is Tensorflow
  • Features of Tensorflow
  • How to install Tensorflow Python
  • Tensorflow environment setup
  • Example of how to use Tensorflow in Python
  • Advantages and Disadvantages of Tensorflow

What is Tensorflow in Python

  • Tensorflow is a library that is used in machine learning and it is an open-source library for numerical computation.
  • It is used for developing machine learning applications and this library was first created by the Google brain team and it is the most common and successfully used library that provides various tools for machine learning applications.
  • Tensorflow library is used in many companies in the industries like Airbnb. This company applies machine learning using TensorFlow to detect objects and classify the images.
  • In Python, if you want to move data through a graph then you can easily use the TensorFlow library for creating dataflow graphs. Basically tensor is a matrix of n dimensions that represents the input type and flows works on the basis of flow graphs that have edges and nodes.
  • It was created and coded by the Google brain team and it was built on mobile O.S and multiple GPUs and CPUs. The current stable version of Tensorflow is 2.6.1 that released on 1 November 2021.
  • The first stable version was released in 2015 under the Apache open source license and then it modify the version and released in September 2019 named Tensorflow 2.0.
  • It is used in many programming languages like Python, R, C++. It provides a user to build a dataflow graph to make data understandable.
  • You can use the Tensorflow package in any Jupyter notebook, google collab, Python shell, and web application servers like Django using anaconda.

Features of Tensorflow

  • It is used as a visualization of graph library for the Python Programming language.
  • It is an open-source library for complex analysis and it is easy to build a neural network.
  • To handle a large amount of data we can easily use the TensorFlow library. It supports multiple programming languages like Python, c++, and java.
  • It defines the level of abstraction by reducing the development time and it is easily trainable on architectures like CPUs and GPUs.
  • It is used to display the images, graphs and also deploy the machine learning models. It also works in any environment like Android and IOS.
  • There are three types of structures in tensor rank, shape, and type. In Python, if you want to train the neural network models then you can easily use the TPU(Tensor processing unit).

Also, check: TensorFlow get shape

Tensorflow environment setup

  • Let’s discuss, Tensorflow download and installation in Windows 10. To download the tensorflow library you have to install the conda package manager.
  • To download the package you have to install miniconda in you system. Open the Miniconda offical site for downloading the conda updated version.
  • Once you have installed the conda package then open the start menu in windows 10. After that open the conda prompt for creating an envirioment.

Step1: Create an environment on Conda prompt by default it takes the base environment. Now check how to create an environment. You can do it so, by executing the following command

conda create --name tensor

Here we have to use the environment name ‘tensor’ you can choose any name

Tensorflow environment setup
Tensorflow environment setup

Step2: To activate this environment, you can implement the following given code

conda activate tensor

The above command activates the environment

Step 3: Now to check the if ‘Tensor’ environment is successfully installed on your system by implementing the below command

conda env list
environment setup of Tensorflow
environment setup of Tensorflow

Step 4: Now in the all Numpy, pandas, and matplotlib library in your environment by using the below command

conda install numpy pandas matplotlib scikit-learn

Step 5: Now in the all Tensorflow library from your development environment. You can do it so, by executing the following command

conda install -c conda-forge tensorflow
tensorflow install
TensorFlow install

To test that everything works fine let’s test by using the below command

conda list

By using this command, you can easily check whether the TensorFlow library is available in the environment path or not

Step 6: Now let’s try to import the Tensorflow library but before importing the library you have to install the jupyter notebook. To install the Jupyter notebook in your system. You can easily use the following given command

conda install jupyter notebook
install jupyter
install jupyter

Step 7: To open the jupyter notebook in your system use the following given command

jupyter notebook

Now you can check if the Tensorflow library is successfully installed on your system by implementing the command below in the jupyter notebook:

import tensorflow as tf
d= tf.__version__
print(d)

This is the easiest way to check whether the TensorFlow library is available in our system.

Screenshot

check version of Tensorflow
check version of Tensorflow

Also, check: Module ‘TensorFlow’ has no attribute ‘session’

Example of how to use Tensorflow in Python

  • In this Program first, we have to import the TensorFlow library and then declare variables by using the constant function.
  • There are different types of tensors that you can easily use in our example like tf.constant, tf.variable, etc.
  • Now in this example, we are going to use simple operations like multiply addition. To do this task we will take the constant function and in Python, this function has basically initialized an object like an array or list and within this function, we can easily use-value dtype parameter.
  • While using tf.variable that indicates the values can be mutuable and it also contains multiple parameters. When you are going to train or design the model of machine learning than in that case you have to apply all these parameters.

Syntax:

Let’s have a look at the Syntax and understand the working of tf.constant and tf.variables

tf.constant
           (
            value,
            dtype=None,
            shape=None,
            name='const'
           )
  • It consists of a few parameters
    • value: This parameter indicates the scaler value
    • dtype: By default it takes none but, you can add dtype=int,float values in it,
    • shape: This parameter specifies the dimension like what will be the array shape (3,3).

Example:

import tensorflow as tf

m = tf.constant(34,dtype="int32",name="mul1")
n = tf.constant(34,dtype="int32",name="mul2")
new_output= tf.multiply(m,n)
print(new_output)

Here is the Screenshot of the following given code

tf constant Python
tf constant Python

Now let’s take the example of the tf.variable in Tensorflow Python

Syntax:

tf.variable
           (
            initial_value=None,
            trainable=None,
            validate_shape=True,
            caching_device=None,
            name=None,
            variable_def=None,
            dtype=None,
            import_scope=None,
            constraint=None,
            synchronization=tf.VariableSynchronization.Auto,
            aggregation=tf.compat.v1.VariableAggregation.None,
            shape=None
           )

Example:

import tensorflow as tf

a = tf.Variable([[14, 23],[34, 67]])
b = tf.Variable([[25, 78],[89, 178]])
new_output= tf.add(a,b)
print(new_output)

Here is the implementation of the following given code

tf variable python
tf variable python

Read: Python TensorFlow random uniform

Advantages and Disadvantages of Tensorflow

Advantages:

  • It is an open-source library that means users can easily download the file from official website.
  • It has the advantage that it can easily modify the performance and also it updates the releases and features.
  • It is a versatile and compatible library like it works on C++, Python, CUDA, etc It is an open-source platform like it works on Linux, Mac o.s and mobile operating systems.
  • It designed backend software like (GPUs, ASIC).

Disadvantages:

  • In this library, it is difficult to get an error and debug because it is an easier and more compatible library as compared to others.
  • It modifies the version every 2-3 months the latest version is Tensorflow 2.0. The issue is that many tf functions are not working in the latest version like the session attribute.
  • There is no support for OpenCL we have used GPUs and TPUs. The latest GPU is NVIDIA and it only supports the Python language.

In this Python tutorial, we have discussed the Tensorflow library, What is TensorflowHow to use it in Python and we have also covered the following topics:

  • What is Tensorflow
  • Features of Tensorflow
  • How to install Tensorflow Python
  • Tensorflow environment setup
  • Example of how to use Tensorflow in Python
  • Advantages and Disadvantages of Tensorflow