Path: blob/master/examples/keras_recipes/ipynb/tensorflow_numpy_models.ipynb
3508 views
Writing Keras Models With TensorFlow NumPy
Author: lukewood
Date created: 2021/08/28
Last modified: 2021/08/28
Description: Overview of how to use the TensorFlow NumPy API to write Keras models.
Introduction
NumPy is a hugely successful Python linear algebra library.
TensorFlow recently launched tf_numpy, a TensorFlow implementation of a large subset of the NumPy API. Thanks to tf_numpy
, you can write Keras layers or models in the NumPy style!
The TensorFlow NumPy API has full integration with the TensorFlow ecosystem. Features such as automatic differentiation, TensorBoard, Keras model callbacks, TPU distribution and model exporting are all supported.
Let's run through a few examples.
Setup
To test our models we will use the Boston housing prices regression dataset.
Subclassing keras.Model with TNP
The most flexible way to make use of the Keras API is to subclass the keras.Model
class. Subclassing the Model class gives you the ability to fully customize what occurs in the training loop. This makes subclassing Model a popular option for researchers.
In this example, we will implement a Model
subclass that performs regression over the boston housing dataset using the TNP API. Note that differentiation and gradient descent is handled automatically when using the TNP API alongside keras.
First let's define a simple TNPForwardFeedRegressionNetwork
class.
Just like with any other Keras model we can utilize any supported optimizer, loss, metrics or callbacks that we want.
Let's see how the model performs!
Great! Our model seems to be effectively learning to solve the problem at hand.
We can also write our own custom loss function using TNP.
Implementing a Keras Layer Based Model with TNP
If desired, TNP can also be used in layer oriented Keras code structure. Let's implement the same model, but using a layered approach!
You can also seamlessly switch between TNP layers and native Keras layers!
The Keras API offers a wide variety of layers. The ability to use them alongside NumPy code can be a huge time saver in projects.
Distribution Strategy
TensorFlow NumPy and Keras integrate with TensorFlow Distribution Strategies. This makes it simple to perform distributed training across multiple GPUs, or even an entire TPU Pod.
TensorBoard Integration
One of the many benefits of using the Keras API is the ability to monitor training through TensorBoard. Using the TensorFlow NumPy API alongside Keras allows you to easily leverage TensorBoard.
To load the TensorBoard from a Jupyter notebook, you can run the following magic:
To load the TensorBoard from a Jupyter notebook you can use the %tensorboard
magic:
The TensorBoard monitor metrics and examine the training curve.
The TensorBoard also allows you to explore the computation graph used in your models.
The ability to introspect into your models can be valuable during debugging.
Conclusion
Porting existing NumPy code to Keras models using the tensorflow_numpy
API is easy! By integrating with Keras you gain the ability to use existing Keras callbacks, metrics and optimizers, easily distribute your training and use Tensorboard.
Migrating a more complex model, such as a ResNet, to the TensorFlow NumPy API would be a great follow up learning exercise.
Several open source NumPy ResNet implementations are available online.