Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Deep Learning with TensorFlow 2 and Keras - Second Edition

You're reading from  Deep Learning with TensorFlow 2 and Keras - Second Edition

Product type Book
Published in Dec 2019
Publisher Packt
ISBN-13 9781838823412
Pages 646 pages
Edition 2nd Edition
Languages
Authors (3):
Antonio Gulli Antonio Gulli
Profile icon Antonio Gulli
Amita Kapoor Amita Kapoor
Profile icon Amita Kapoor
Sujit Pal Sujit Pal
Profile icon Sujit Pal
View More author details

Table of Contents (19) Chapters

Preface Neural Network Foundations with TensorFlow 2.0 TensorFlow 1.x and 2.x Regression Convolutional Neural Networks Advanced Convolutional Neural Networks Generative Adversarial Networks Word Embeddings Recurrent Neural Networks Autoencoders Unsupervised Learning Reinforcement Learning TensorFlow and Cloud TensorFlow for Mobile and IoT and TensorFlow.js An introduction to AutoML The Math Behind Deep Learning Tensor Processing Unit Other Books You May Enjoy
Index

What are the most important changes in TensorFlow 2.0?

There are many changes in TensorFlow 2.0. There is no longer a need to question "Do I use Keras or TensorFlow?" because Keras is now part of TensorFlow. Another question is "Should I use Keras or tf.keras?" tf.keras is the implementation of Keras inside TensorFlow. Use tf.keras instead of Keras for better integration with other TensorFlow APIs, such as eager execution, tf.data, and many more benefits that we are going to discuss in Chapter 2, TensorFlow 1.x and 2.x.

For now, let's start with a simple code comparison just to give you some initial intuition. If you have never installed TensorFlow before, then let's install it using pip:

You can find more options for installing TensorFlow at https://www.tensorflow.org/install.

Only CPU support:

pip install tensorflow

With GPU support:

pip install tensorflow-gpu

In order to understand what's new in TensorFlow 2.0, it might be useful to have a look at the traditional way of coding neural networks in TensorFlow 1.0. If this is the first time you have seen a neural network, please do not pay attention to the details but simply count the number of lines:

import tensorflow.compat.v1 as tf
in_a = tf.placeholder(dtype=tf.float32, shape=(2))
def model(x):
  with tf.variable_scope("matmul"):
    W = tf.get_variable("W", initializer=tf.ones(shape=(2,2)))
    b = tf.get_variable("b", initializer=tf.zeros(shape=(2)))
    return x * W + b
out_a = model(in_a)
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  outs = sess.run([out_a],
                feed_dict={in_a: [1, 0]})

In total, we have 11 lines here. Now let's install TensorFlow 2.0:

Only CPU support:

pip install tensorflow==2.0.0-alpha0

With GPU support:

pip install tensorflow-gpu==2.0.0-alpha0

Here's how the code is written in TensorFlow 2.0 to achieve the same results:

import tensorflow as tf
W = tf.Variable(tf.ones(shape=(2,2)), name="W")
b = tf.Variable(tf.zeros(shape=(2)), name="b")
@tf.function
def model(x):
  return W * x + b
out_a = model([1,0])
print(out_a)

In this case, we have eight lines in total and the code looks cleaner and nicer. Indeed, the key idea of TensorFlow 2.0 is to make TensorFlow easier to learn and to apply. If you have started with TensorFlow 2.0 and have never seen TensorFlow 1.x, then you are lucky. If you are already familiar with 1.x, then it is important to understand the differences and you need to be ready to rewrite your code with some help from automatic tools for migration, as discussed in Chapter 2, TensorFlow 1.x and 2.x. Before that, let's start by introducing neural networks–one of the most powerful learning paradigms supported by TensorFlow.

You have been reading a chapter from
Deep Learning with TensorFlow 2 and Keras - Second Edition
Published in: Dec 2019 Publisher: Packt ISBN-13: 9781838823412
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}