TensorFlow Hub: A Library for Reusable Machine Learning Modules in TensorFlow

Introducing TensorFlow Hub: Library for Reusable Machine Learning Modules at TensorFlow

One of the most fundamental things in software development is the idea of ​​a store of shared code that is easy to ignore. As programmers, libraries instantly make us more effective. In a sense, they change the process of problem-solving programming. When using the library, we often think of programming in terms of building blocks - or modules - that can be glued together.

How can a library be considered a machine education developer? Of course, in addition to the share to code, we also want to share pre-trend models. Sharing pre-trained models make it possible for developers to optimize for their domain, without access to computer resources or data used to train the model in the original hands. For example, the NASNet train took thousands of GPU-hours. By sharing the weights learned, a model developer can make it easier for others to reuse and build their work.

It's a library idea for machine education developers that inspired the TensorFlow Hub, and we're happy to share it with the community today. TensorFlow Hub TensorFlow is a platform for publishing, locating, and reusing portions of machine learning modules. By a module, we mean a self-contained fraction of a TensorFlow graph, with its weight, which can be reused in other, similar functions. By reusing the module, a developer can train the model using a small dataset, improve generalizations, or simply speed up the training. Let's look at some examples of how to make this concrete.

Image retraining

As a first example, let's look at a technique that you can use to train an image classifier, starting with a small amount of training data. Modern image recognition models have millions of parameters, and of course, training from scratch requires a large amount of labeled data and computing power. Using a technique called image retraining, you can train the model using very little data, and very little computing time. Here's what it looks like in a TensorFlow hub.

The basic idea is to reuse existing image recognition modules to extract features from your images, and then train new categorizers on these. As you can see above, TensorFlow Hub modules can be instant from the URL (or, from the file system path) when the TensorFlow graph is being created. From there, TensorFlow Hub has a variety of modules that you can choose from, including NASNet, MobileNet (including its latest V2), Inception, ResNet, and more. To use the module you import the TensorFlow Hub, then copy/paste the module's URL into your code.

Some image modules are available at TensorFlow Hub.

Each module has a defined interface that allows it to be used in a modifiable way, with little or no knowledge of its interiors. In this case, this module has a method that you can use to retrieve the desired image size. As a developer, you only need to provide the correct size of the group of images, and call modules in them to get feature representation. This module takes care of the pre-processing of images for you so that you can represent the feature in a single step directly from the group of images. From here, you can classify linear models or other types, above all.

In this case, notify that the module we are using is hosted by Google, and has been edited (so that you cannot change the module that you use in your experiments). Modules can be used as a simple Python type to construct a portion of a graphic. Once exported to disk, the modules contain themselves, and others can use it to create code and data, create it and use it for training (of course you can also publish).

Text classification

Let's take a look at the second example. Imagine that you want to train a model to categorize movie reviews as positive or negative, starting with just a small amount of training data (say, several hundred positive and negative movie reviews). Since you have a limited number of examples you decide to pick up a dataset of word embedding, first trained in a very large corpus. Here's how to use a tensor flow hub.

As before, we start by selecting the module. TensorFlow Hub has a variety of text modules for you to explore, including neural network language models in different languages ​​(N, JP, DE, and ES), as well as Word 2vec trained on Wikipedia, and NNLM embedding trained on Google News.

Some text modules shared in TensorFlow Hub.

In this case, we will use the module for word embedding. The code above downloads the module, uses it to preprocess the sentence, then retrieves the embedding for each token. This means you can go directly to your dataset from a sentence that can go to the appropriate format for the classifier in one step. The module works to tokenize sentences, and to handle other logic-like words outside of vocabulary. Both preprocessing logic and embedding are encapsulated in a single module, making it easy to use different datasets of simple word embedding, or different pre-processing strategies, without changing your code.

Text classification tutorial using TensorFlow Hub.

If you want to try it, use this tutorial to take it for a spin and to learn how the TensorFlow Hub module works with TensorFlow Estimators.

Universal Sentence Encoder

We've also shared the new TensorFlow Hub module! Below is an example of the Universal Sentence Encoder used. It is a sentence-level embedding module that is trained in a wide variety of datasets (in other words, "universal"). Some of the good things about it are semantic similarity, custom text classification, and cluster.

This notebook shows how to train a simple binary text classifier above any TF-Hub module.

As with image retraining, the module requires relatively little label data to adapt to your own work. Let's try this in a restaurant review for example.

Other modules

Tensorflow hub is more than image and text classification. On the website, you will also find some modules for progressive GAN and Google Landmark Deep local features.


There are some important considerations when using the TensorFlow Hub modules. First, note that the modules contain code that can be turned on. Always use modules from trusted sources. Second, as in all machine learning, fairness is an important consideration. Both examples are large pre-trained datasets we have shown above. When replicating such a dataset, it is important to consider what data it contains (and whether there are any existing biases), and how this may affect the product you are building and its users.

Get started with TensorFlow.js → https://js.tensorflow.org