TensorFlow 2.0

16 May, 2020
TensorFlow the massively popular open-source platform to develop and integrate large scale AI and Deep Learning Models has recently been updated to its newer form TensorFlow 2.0. This brings a massive boost in features in the originally feature-rich ML ecosystem created by the TensorFlow community.
What is Open-Source and how is it made TensorFlow so successful?Open-Source means something that the people(mainly developers) can modify, share, integrate because all the original design features are open to all. This makes it very easy for a particular software, product to expand easily, effectively, and in a very little time. This feature allowed the original creator of TensorFlow i.e Google to easily port this into every platform available in the market that includes Web, Mobile, Internet of Things, Embedded Systems, Edge Computing and included support of various other languages such JavaScript, Node.js, F#, C++, C#, React.js, Go, Julia, Rust, Android, Swift, Kotlin and many other.Along with this came the support for hardware acceleration for running large scale Machine Learning codes. These include CUDA(library for running ML code on GPUs), TPUs(Tensor Processing Unit- Custom hardware provided by Google specially designed and developed to process tensors using TensorFlow) for multiple machine configuration, GPU, GPGPU, Cloud-based TPU’s, ASIC (Application Specific Integrated Circuits) FPGAs(Field-Programmable Gate Arrays- These are exclusively used for custom Programmable Hardware). This also includes new additions such as NVIDIA’s Jetson TX2 and Intel’s Movidius chips.Now coming back to the newer and much feature rich TensorFlow2.0:This is a graphical implementation of the changes:
Model Diagram
The things which are added include:
The main API is now non-other than the Keras: The fluid layer of Keras is now integrated on top of the raw TensorFlow code make it simple and easy to use. This would help bring a lot of progress and productivity in the field of Machine Learning and AI.
Eager Command-line : This simple command line helps us to execute operation immediately without using Session.run command.
Simplified and Integrated Workflow:Using tf.data for data loading(Or NumPy).Use Keras for model construction.(We can also use any premade Estimators).Use tf.function for DAG graph execution or use eager execution.Utilize distribution strategy for high-performance-computing and deep learning models. (For TPUs, GPUs etc).TF 2.0 standardizes Saved Model as a serialized version of a TensorFlow graph for a variety of different platforms ranging from Mobile, JavaScript, TensorBoard, TensorHub..etc.
Using tf.data for data loading(Or NumPy).Use Keras for model construction.(We can also use any premade Estimators).Use tf.function for DAG graph execution or use eager execution.Utilize distribution strategy for high-performance-computing and deep learning models. (For TPUs, GPUs etc).TF 2.0 standardizes Saved Model as a serialized version of a TensorFlow graph for a variety of different platforms ranging from Mobile, JavaScript, TensorBoard, TensorHub..etc.
Using tf.data for data loading(Or NumPy).
Use Keras for model construction.(We can also use any premade Estimators).
Use tf.function for DAG graph execution or use eager execution.
Utilize distribution strategy for high-performance-computing and deep learning models. (For TPUs, GPUs etc).
TF 2.0 standardizes Saved Model as a serialized version of a TensorFlow graph for a variety of different platforms ranging from Mobile, JavaScript, TensorBoard, TensorHub..etc.
Support for TensorFlow Lite and TensorFlow Edge Computing: This would help the developers to give effective Machine Learning and AI services to the end devices. This would require very less computing power along with faster model implementation and end-users
The new extensions for Web Applications and Node.js using TensorFlow.js for new and interactive AI-based websites and applications.
TensorFlow optimization for Android.
TensorFlow Integration for Swift and IOS based applications.
Support for the most-awaited upcoming WebGPU Chrome RFC proposal.
Unified Programming Paradigms(Directed Acyclic Graph/Functional and Stack/Sequential).
TensorFlow AIY(Artificial Intelligence for Yourself) support.
Integration of tf.contrib into separate repositories.
Improved TPU and TPU support and distributed computation support and support for the same up to v3.
Improved HPC integration for Parallel Computing.
Community Integration for Development, Support and Research.
Integration of tf.contrib best package implementation into the core package.
Domain-Specific Community Support.
Extra Support for Model Validation and Reuse.
End-to-End ML Pipelines and Products available at TensorFlow Hub.
At last we can now build big ML and Deep Learning models easily, effectively on TensorFlow2.0 for end-users, and implement them on a large scale.
Examples:Training a neural network to categorize MNSIT data
# Write Python3 code hereimport tensorflow as tf  """The Fashion MNIST data is available directly in the tf.keras datasets API. You load it like this:"""  mnist = tf.keras.datasets.fashion_mnist  """Calling load_data on this object will give you two sets of two lists, these will be the training and testing values for the graphics that contain the clothing items and their labels."""  (training_images, training_labels), (test_images, test_labels) = mnist.load_data()"""You'll notice that all of the values in the number are between 0 and 255. If we are training a neural network, for various reasons it's easier if wetreat all values as between 0 and 1, a process called '**normalizing**'...andfortunately in Python it's easy to normalize a list like this without looping.So, perform it like - """  training_images  = training_images / 255.0test_images = test_images / 255.0  """Now you might be wondering why there are 2 sets...training and testing-- remember we spoke about this in the intro? The idea is to have 1 set ofdata for training, and then another set of data...that the model hasn't yetseen...to see how good it would be at classifying values. After all, whenyou're done, you're going to want to try it out with data that it hadn't previously seen!  Let's now design the model. There are quite a few new concepts here, but don't worry, you'll get the hang of them."""  model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),                                     tf.keras.layers.Dense(                                            128, activation=tf.nn.relu),                                     tf.keras.layers.Dense(                                            10, activation=tf.nn.softmax)])  """**Sequential**: That defines a SEQUENCE of layers in the neural network  **Flatten**: Remember earlier where our images were a square whenyou printed them out? Flatten just takes that square and turns itinto a 1-dimensional set.  **Dense**: Adds a layer of neurons  Each layer of neurons needs an **activation function** to tell them what todo. There are lots of options, but just use these for now.   **Relu** effectively means "If X>0 return X, else return 0" -- so what it does it only passes values 0 or greater to the next layer in the network.  **Softmax** takes a set of values, and effectively picks the biggest one, so, for example, if the output of the last layer looks like [0.1, 0.1, 0.05, 0.1, 9.5, 0.1, 0.05, 0.05, 0.05], it saves you from fishing through it looking for the biggest value, and turns it into [0,0,0,0,1,0,0,0,0] --  The goal is to save a lot of coding!  The next thing to do, now the model is defined, is to actually build it.You do this by compiling it with an optimizer and loss function as before -- and then you train it by calling **model.fit ** asking it to fit your trainingdata to your training labels -- i.e. have it figure out the relationship betweenthe training data and its actual labels, so in future, if you have data thatlooks like the training data, then it can make a prediction for what that datawould look like."""  model.compile(optimizer = tf.keras.optimizers.Adam(),              loss = 'sparse_categorical_crossentropy',              metrics=['accuracy'])  model.fit(training_images, training_labels, epochs=5)  """Once it's done training -- you should see an accuracy value at the end of thefinal epoch. It might look something like 0.9098. This tells you that yourneural network is about 91% accurate in classifying the training data. I.E.,it figured out a pattern match between the image and the labels that worked91% of the time. Not great, but not bad considering it was only trained for 5epochs and done quite quickly.  But how would it work with unseen data? That's why we have the test images. We can call model.evaluate, and pass in the two sets, and it will report back theloss for each. Let's give it a try:"""  model.evaluate(test_images, test_labels)  """ For me, that returned an accuracy of about .8838, which means it was about 88% accurate. As expected it probably would not do as well with *unseen*data as it did with data it was trained on!   """
Output:
Expected Accuracy 88-91%

Eager Execution :
import tensorflow as tfimport tensorflow.contrib.eager as tfe   tfe.enable_eager_execution()   x = [[2.]]m = tf.matmul(x, x)   print(m)
Output :
tf.Tensor([[4.]], shape=(1, 1), dtype=float32)
ADARSHSINGH9
data-science
Advanced Computer Subject
Articles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.