Multi-Task Learning for Sentence Embeddings
Tags: ai, python, machinelearning, datascience
Published: 2019-07-22 00:17:09

---
title: Multi-Task Learning for Sentence Embeddings
published: true
tags: artificial-intellige,python,machine-learning,data-science
canonical_url: https://medium.com/@makcedward/multi-task-learning-for-sentence-embeddings-55f47be1610a
---

Universal Sentence Encoder

![](https://cdn-images-1.medium.com/max/1024/0*eaSQmfXUK5j4XWwN)<figcaption>“ Mount Fuji“ by <a href="https://unsplash.com/@makcedward?utm_source=medium&amp;utm_medium=referral">Edward Ma</a> on <a href="https://unsplash.com/photos/xFmcfCR9h20?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Cera et al. demonstrated that the transfer learning result of sentence embeddings is outperform [word embeddings](https://towardsdatascience.com/3-silver-bullets-of-word-embedding-in-nlp-10fa8f50cc5a). The traditional way of building sentence embeddings is either average, sum or contacting a set of word vectors to product sentence embeddings. This method loss lots of information but just easier of calculation. Cera et al. evaluated two famous network architectures which are transformer based model and deep averaging network (DAN) based model.

![](https://cdn-images-1.medium.com/max/507/1*wgGU92OyaWrz6TBwBKclpg.png)<figcaption>Sentence similarity score (Cera et al., 2018)</figcaption>

This story will discuss about [Universal Sentence Encoder](https://arxiv.org/pdf/1803.11175.pdf) (Cera et al., 2018) and the following are will be covered:

- Data
- Architecture
- Implementation

### Data

As it is designed to support multiple downstream tasks, multi task learning is adopted. Therefore, Cera et al. use multiple data sources to train model including movie review, customer review, sentiment classification, question classification, semantic textual similarity and Word Embedding Association Test (WEAT) data.

### Architecture

Text will be tokenized by Penn Treebank(PTB) method and passing to either transformer architecture or deep averaging network. As both models are designed to be a general purpose, multi-task learning approach is adopted. The training objective includes:

- Same as [Skip-though](https://towardsdatascience.com/transforming-text-to-sentence-embeddings-layer-via-some-thoughts-b77bed60822c), predicting previous sentence and next sentence by giving current sentence.
- Conversational response suggestion for the inclusion of parsed conversational data.
- Classification task on supervised data

![](https://cdn-images-1.medium.com/proxy/1*jKZr6M_UOUizE3ej74ohwg.png)<figcaption>Predicting previous sentence and next sentence (Kiros et al., 2015)</figcaption>

[Transformer](https://arxiv.org/pdf/1706.03762.pdf) architecture is developed by Google in 2017. It leverages self attention with multi blocks to learn the context aware word representation.

![](https://cdn-images-1.medium.com/max/438/1*42QeWPLtG-kwhblDbtGP4Q.png)<figcaption>Transformer architecture (Vaswani et al,, 2017)</figcaption>

Deep averaging network (DAN) is using average of embeddings (word and bi-gram) and feeding to feedforward neural network.

![](https://cdn-images-1.medium.com/max/539/1*v07lrQnceNCWXxyVx2yixg.png)<figcaption>DAN architecture (Ivver et al., 2015)</figcaption>

The reasons of introducing two models because different concern. Transformer architecture achieve a better performance but it needs more resource to train. Although DAN does not perform as good as transformer architecture. The advantage of DAN is simple model and requiring less training resource.

### Implementation

To explore the Universal Sentence Encoder, if you simply follow the instruction from [Tensorflow Hub](https://tfhub.dev/google/universal-sentence-encoder/2).

### Take Away

- Multi-task learning is important for learning text representations. It can be found that lots of modern NLP model architecture use multi-task learning rather than standalone data set
- Rather than aggregate multi word vectors to represent sentence embeddings, learning it from multi word vectors achieve better result.

### About Me

I am Data Scientist in Bay Area. Focusing on state-of-the-art in Data Science, Artificial Intelligence , especially in NLP and platform related. Feel free to connect with me on [LinkedIn](https://www.linkedin.com/in/edwardma1026) or following me on [Medium](http://medium.com/@makcedward/) or [Github](https://github.com/makcedward). I am offering short advise on machine learning problem or data science platform for small fee.

### Extension Reading

- [Universal Sentence Encoder](https://tfhub.dev/google/universal-sentence-encoder-large/3) implementation
- [Word Embeddings](https://towardsdatascience.com/3-silver-bullets-of-word-embedding-in-nlp-10fa8f50cc5a)
- [Skip-though (Sentence Embeddings)](https://towardsdatascience.com/transforming-text-to-sentence-embeddings-layer-via-some-thoughts-b77bed60822c)

### Reference

D. Cera , Y. Yang , S. Y. Kong , N, Hua , N. Limtiaco, R. S. Johna , N. Constanta , M. Guajardo-Cespedes, S. Yuan, C. Tar , Y. H. Sung , B. Strope and Ray Kurzweil. [Universal Sentence Encoder](https://arxiv.org/pdf/1803.11175.pdf). 2018

A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser and I. Polosukhin. [Attention Is All You Need](https://arxiv.org/pdf/1706.03762.pdf). 2017

M. Iyyer, V. Manjunatha, J. Boyd-Graber and H. Daume III. [Deep Unordered Composition Rivals Syntactic Methods for Text Classification](http://www.aclweb.org/anthology/P15-1162). 2015