You are currently viewing R interface to TensorFlow Hub

R interface to TensorFlow Hub

We’re happy to announce that the primary model of tfhub is now on CRAN. tfhub is an R interface to TensorFlow Hub – a library for the publication, discovery, and consumption of reusable components of machine studying fashions. A module is a self-contained piece of a TensorFlow graph, together with its weights and property, that may be reused throughout totally different duties in a course of generally known as switch studying.

The CRAN model of tfhub may be put in with:

After putting in the R package deal it’s worthwhile to set up the TensorFlow Hub python package deal. You are able to do it by working:

Getting began

The important operate of tfhub is layer_hub which works identical to a keras layer however permits you to load a whole pre-trained deep studying mannequin.

For instance you may:

layer_mobilenet <- layer_hub(
  deal with = ""

This may obtain the MobileNet mannequin pre-trained on the ImageNet dataset. tfhub fashions are cached regionally and don’t should be downloaded the subsequent time you utilize the identical mannequin.

Now you can use layer_mobilenet as a common Keras layer. For instance you may outline a mannequin:

enter <- layer_input(form = c(224, 224, 3))
output <- layer_mobilenet(enter)
mannequin <- keras_model(enter, output)
Mannequin: "mannequin"
Layer (kind)                  Output Form               Param #    
input_2 (InputLayer)          [(None, 224, 224, 3)]      0          
keras_layer_1 (KerasLayer)    (None, 1001)               3540265    
Complete params: 3,540,265
Trainable params: 0
Non-trainable params: 3,540,265

This mannequin can now be used to foretell Imagenet labels for a picture. For instance, let’s see the outcomes for the well-known Grace Hopper’s photograph:

Grace Hopper
img <- image_load("", target_size = c(224,224)) %>% 
img <- img/255
dim(img) <- c(1, dim(img))
pred <- predict(mannequin, img)
  class_name class_description    rating
1  n03763968  military_uniform 9.760404
2  n02817516          bearskin 5.922512
3  n04350905              go well with 5.729345
4  n03787032       mortarboard 5.400651
5  n03929855       pickelhaube 5.008665

TensorFlow Hub additionally presents many different pre-trained picture, textual content and video fashions.
All doable fashions may be discovered on the TensorFlow hub web site.

TensorFlow Hub

You will discover extra examples of layer_hub utilization within the following articles on the TensorFlow for R web site:

Utilization with Recipes and the Function Spec API

tfhub additionally presents recipes steps to make
it simpler to make use of pre-trained deep studying fashions in your machine studying workflow.

For instance, you may outline a recipe that makes use of a pre-trained textual content embedding mannequin with:

rec <- recipe(obscene ~ comment_text, information = practice) %>%
    deal with = ""
  ) %>%

You possibly can see a whole working instance right here.

You can too use tfhub with the brand new Function Spec API carried out in tfdatasets. You possibly can see a whole instance right here.

We hope our readers have enjoyable experimenting with Hub fashions and/or can put them to good use. When you run into any issues, tell us by creating a problem within the tfhub repository


Textual content and figures are licensed underneath Artistic Commons Attribution CC BY 4.0. The figures which have been reused from different sources do not fall underneath this license and may be acknowledged by a notice of their caption: “Determine from …”.


For attribution, please cite this work as

Falbel (2019, Dec. 18). Posit AI Weblog: tfhub: R interface to TensorFlow Hub. Retrieved from

BibTeX quotation

  creator = {Falbel, Daniel},
  title = {Posit AI Weblog: tfhub: R interface to TensorFlow Hub},
  url = {},
  12 months = {2019}

Leave a Reply