Working with TensorFlow 2.0 Alpha

With the release of TensorFlow 2.0 Alpha during the TensorFlow Developer Summit, we’d like to take a moment and look at how we can use it. One of the major updates in this version is the use of Keras as the high-level API and eager execution. If you’re familiar with Keras, you’ll find the code in this short tutorial to be very familiar. In this short tutorial, we’ll illustrate how you can use TensorFlow 2.0 to build a deep learning model.

Imports

The first step is to import TensorFlow and Keras. The Keras API is already implemented in TensorFlow, so we can import it from TensorFlow.

import tensorflow as tf
from tensorflow import keras

Loading Datasets

Loading datasets is very similar to how we’d load them using Keras. Let’s show how we’d load in the fashion MNIST datasets.

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

Building the Model

Layers can be added using tf.keras.layers and specifying the layer to be added. Again, if you’ve worked with Keras, this will be very intuitive. We start by initializing the layers Sequential and afterward adding the different layers. Below, we’ll show how that can be accomplished.

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

Compiling the Model

The next step is usually to compile the model by specifying the loss, optimizer, and evaluation metrics. Let’s show how a model can be compiled using the adam optimizer, using sparse_categorical_crossentropy loss, and identifying accuracy as metrics.

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Training the Model

In order to train the model, we’ll need to pass in the training data, its labels, and the number of epochs.

model.fit(training_data, training_labels, epochs=1)

Evaluate the Accuracy

Evaluating the model can be done using the evaluate function as shown below.

test_loss, test_acc = model.evaluate(testing_data, testing_labels)

print('Test accuracy:', test_acc)

Making Predictions

As you guessed, predictions can be made using the predict method. All you have to do is pass in the testing set.

predictions = model.predict(testing_data)

Saving and Restoring the Model

In order to save a model, one has to first install these two dependencies.

pip install -q h5py pyyaml 

Saving the entire model HDF5 file is also very easy, as we’ll see below.

model.save('model.h5')

Loading the Saved Model

Loading in the saved model is quite straightforward, as well. All we have to do is call the load_models function and pass in the path to the model.

my_model = keras.models.load_model('model.h5')
my_model.summary()

This is obviously a very brief overview of how to use TensorFlow 2.0 Alpha. If you’re just starting out with TensorFlow, you can find some beginner tutorials here. For developers who are a bit more advanced, there’s something for you here.

As you’ve seen, TensorFlow 2.0 Alpha is much easier to implement compared to TensorFlow 1.0. It’s now much easier to define your model using the Keras implementation. Since TensorFlow 2.0 Alpha works in eager mode, you also don’t have to define a session in order to execute the code. You can also take advantage of the many tools in the TensorFlow ecosystem such as TensorBoard for visualizing your results and TensorFlow Serving for hosting your models.

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square