Introduction to Tensorflow
Tensorflow is an Open-Source Machine Learning and Deep Learning Library that was launched by Google back in 2015 under Apache License 2.0. The popularity of Tensorflow has scaled heights ever since, with multiple companies and organization turning towards Tensorflow and the ecosystem it provides which is leveraged to bring the benefits of Machine Learning and Deep Learning in a much wider audience. The word "Tensorflow" is made up of two words: Tensor and Flow. Tensor is a multidimensional data array that is used for mathematical computations while the flow is a graph of operations that is performed on the multidimensional array.
Tensorflow and Models built with Tensorflow are one of the most trending Open-Source Projects on Github.
You might ask, why Tensorflow for Machine Learning and what makes it better than its competitors? Tensorflow provides multiple Application Programming Interfaces (APIs) with the low-level APIs rendering us a Programming Control upon Cores while the high-level APIs rendering us control over Tensorflow core. Tensorflow makes it very easy to develop and deploy Machine Learning Models and allow us to develop Machine Learning Applications using custom datasets or pre-trained models.
Tensorflow also provides a very flexible architecture and supports the production of Machine Learning Models on CPUs, GPUs and TPUs as well as Edge Devices. Tensorflow is supported by many Programming Languages and is highly popular in Python Programming Language including other Scripting Languages like Javascript and Swift. Tensorflow is used by multiple organizations all across the world like Airbus, PayPal, Twitter to name a few. Let us have a look at a simple "Hello World" code written in Tensorflow:
Since we are clear with Tensorflow and implementing a basic code with Tensorflow, let's move onto implementing a simple code to recognize Hand-Written Digits using Tensorflow and develop a model based on that.
Implementing MNIST Hand-Written Digit Recognition using Tensorflow
The MNIST database is a large database of handwritten digits that is commonly used for training various image processing systems. MNIST ("Modified National Institute of Standards and Technology") is the de facto “Hello World” dataset of computer vision. It is a dataset of 60,000 small square 28×28 pixel grayscale images of handwritten single digits between 0 and 9. In this Code, we will implement an MNIST Digit Recognition Application using Tensorflow which can recognize digits from 0-9.
Let us implement our code:
- At first, we will be importing the necessary modules and dependencies required to run our code. We will import Tensorflow and load the MNIST Dataset from Tensorflow which is available by Keras API. Once the Dataset has been loaded, we will normalize the data. MNIST Dataset comprises of Images in 32x32 pixels each for red, blue and green channels and has a value between 0-255. We will normalize the values to 0-1.
2. Next, we will implement our Tensorflow Model which at first consists of tf.keras.models.Sequential() function which creates a linear arrangement of layers. Next, we will add, tf.keras.layers.Flatten(input_shape=(28,28)) which will implement one node for each pixel in the image. Flatten creates 28*28=784 Nodes which can represent each pixel in an image. tf.keras.layers.Dense() will implement a densely connected layer. We will also be using Activation Functions here: ReLU and Softmax which takes logits as an input and convert them to activated values. A Dropout Layer tf.keras.layers.Dropout() is also added to counter the disadvantages of a Densely Connected Layer and simply means that some nodes will not be passing information to other nodes in next iteration. At last a model.compile() function is called which will run the model.
3. Lastly, we will run the model for 10 Epochs (You can change the number of epochs according to your needs) and we will use model.evaluate() function to compute the accuracy of our model.
We will get an accuracy of nearly 98% on the MNIST Dataset. The model can be saved later in a Keras Model File with a .h5 Extension which can be loaded onto other code which will cut down the time required for training and testing the model. The Code for the above-mentioned processing has been saved here.
SDE @ Amazon | 5? CodeChef | Specialist Codeforces
4 年Great article Harsh Bardhan Mishra
--
4 年Pytorch >> tensorflow ( except tensorflow serving)
Member Technical at ADP|Microsoft student partner 2020
4 年Amazing tutorial