TensorFlow Addons
TensorFlow Addons (TFA) is a repository of contributions that conform to well-established API patterns, but implement new functionality not available in core TensorFlow. It provides a curated collection of specialized layers, optimizers, losses, metrics, and other operations. As of its 0.23.0 version, TFA has ended development and introduction of new features, entering a minimal maintenance and release mode until a planned end of life in May 2024.
Warnings
- breaking TensorFlow Addons (TFA) has reached its planned end of life in May 2024. Development of new features has ceased, and the library is in a minimal maintenance mode. Users are advised to migrate to other TensorFlow community repositories (e.g., Keras, Keras-CV, Keras-NLP).
- gotcha Compatibility with TensorFlow versions is crucial. TensorFlow Addons is only guaranteed to be compatible with the TensorFlow versions it was tested against. Warnings will be emitted if versions do not match, and custom C++ operations may lead to segmentation faults or crashes with incompatible TensorFlow builds (e.g., `conda` installations).
- deprecated Some functionalities, like `tfa.activations.gelu` and `data_format` arguments in `tensorflow_addons/image`, have been deprecated, with `gelu` migrating to core TensorFlow.
- breaking Windows support for `tensorflow-addons` was dropped due to inconsistent TensorFlow 2.15 `.whl` packaging. Users on Windows will fall back to pure TensorFlow Python implementations where possible for custom ops.
- gotcha APIs in TensorFlow Addons might evolve more rapidly than those in core TensorFlow. While the SIG strives for stability, frequent updates can introduce changes.
Install
-
pip install tensorflow-addons -
pip install tensorflow-addons[tensorflow]
Imports
- tfa
import tensorflow_addons as tfa
- tfa.activations
import tensorflow_addons.activations as tfa_activations
- tfa.layers
import tensorflow_addons.layers as tfa_layers
- tfa.optimizers
import tensorflow_addons.optimizers as tfa_optimizers
- tfa.losses
import tensorflow_addons.losses as tfa_losses
- tfa.metrics
import tensorflow_addons.metrics as tfa_metrics
Quickstart
import tensorflow as tf
import tensorflow_addons as tfa
# Define a simple Keras model with a TFA layer and optimizer
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(10,)),
tf.keras.layers.Dense(128, activation='relu'),
tfa.layers.GroupNormalization(groups=8, axis=-1), # Example TFA layer
tf.keras.layers.Dense(10, activation='softmax')
])
# Use a TFA optimizer
optimizer = tfa.optimizers.AdamW(weight_decay=0.001, learning_rate=0.001)
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Create dummy data
import numpy as np
x_train = np.random.rand(100, 10).astype(np.float32)
y_train = np.random.randint(0, 10, 100)
print("Model summary:")
model.summary()
print("\nTraining model with TFA components...")
model.fit(x_train, y_train, epochs=1, batch_size=32, verbose=0)
print("Training complete.")