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.