The tfhub package provides R wrappers to TensorFlow Hub.
TensorFlow Hub is a library for reusable machine learning modules.
TensorFlow Hub is a library for the publication, discovery, and consumption of reusable parts of machine learning models. A module is a self-contained piece of a TensorFlow graph, along with its weights and assets, that can be reused across different tasks in a process known as transfer learning. Transfer learning can:
You can install the released version of tfhub from CRAN with:
And the development version from GitHub with:
After installing the tfhub package you need to install the TensorFlow Hub python module:
Modules can be loaded from URL’s and local paths using
hub_load()
Module’s behave like functions and can be called with Tensors eg:
The easiest way to get started with tfhub is using
layer_hub
. A Keras layer that loads a TensorFlow Hub module
and prepares it for using with your model.
library(tfhub)
library(keras)
input <- layer_input(shape = c(32, 32, 3))
output <- input %>%
# we are using a pre-trained MobileNet model!
layer_hub(handle = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2") %>%
layer_dense(units = 10, activation = "softmax")
model <- keras_model(input, output)
model %>%
compile(
loss = "sparse_categorical_crossentropy",
optimizer = "adam",
metrics = "accuracy"
)
We can then fit our model in the CIFAR10 dataset:
tfhub can also be used with tfdatasets:
hub_text_embedding_column()
hub_sparse_text_embedding_column()
hub_image_embedding_column()
recipes
tfhub adds a step_pretrained_text_embedding
that can be
used with the recipes package.
An example can be found here.
tfhub.dev is a gallery of pre-trained model ready to be used with TensorFlow Hub.