Overview

    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:

    • Train a model with a smaller dataset,
    • Improve generalization, and
    • Speed up training.

    Installation

    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:

    Loading modules

    Modules can be loaded from URL’s and local paths using hub_load()

    module <- hub_load("https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2")

    Module’s behave like functions and can be called with Tensors eg:

    Using with Keras

    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:

    Using with tfdatasets

    tfhub can also be used with tfdatasets since it provides implementations of feature_columns:

    You can find a working example here.

    Using with recipes

    tfhub adds a step_pretrained_text_embedding that can be used with the recipes package.

    An example can be found here.

    tfhub.dev

    tfhub.dev is a gallery of pre-trained model ready to be used with TensorFlow Hub.