Universal Sentence Encoder

Description

The Universal Sentence Encoder encodes text into high-dimensional vectors that can be used for text classification, semantic similarity, clustering and other natural language tasks.

The model is trained and optimized for greater-than-word length text, such as sentences, phrases or short paragraphs. It is trained on a variety of data sources and a variety of tasks with the aim of dynamically accommodating a wide variety of natural language understanding tasks. The input is variable length English text and the output is a 512 dimensional vector. We apply this model to the STS benchmark for semantic similarity, and the results can be seen in the example notebook made available. The universal-sentence-encoder model is trained with a deep averaging network (DAN) encoder.

The details are described in the paper “Universal Sentence Encoder”.

Download

How to use

...
embeddings = UniversalSentenceEncoder.pretrained("tfhub_use", "en") \
      .setInputCols("sentence") \
      .setOutputCol("sentence_embeddings")
nlp_pipeline = Pipeline(stages=[document_assembler, sentence_detector, embeddings])
pipeline_model = nlp_pipeline.fit(spark.createDataFrame([[""]]).toDF("text"))
result = pipeline_model.transform(spark.createDataFrame(pd.DataFrame({"text": ["I love NLP", "Many thanks"]})))
...
val embeddings = UniversalSentenceEncoder.pretrained("tfhub_use", "en")
      .setInputCols("sentence")
      .setOutputCol("sentence_embeddings")
val pipeline = new Pipeline().setStages(Array(document_assembler, sentence_detector, embeddings))
val result = pipeline.fit(Seq.empty["I love NLP", "Many thanks"].toDS.toDF("text")).transform(data)
import nlu

text = ["I love NLP"]
embeddings_df = nlu.load('en.embed_sentence.tfhub_use').predict(text, output_level='sentence')
embeddings_df

Results


        sentence	en_embed_sentence_tfhub_use_embeddings
	
0	I love NLP	[0.06498772650957108, 0.01892215944826603, -0....
1	Many thanks	[0.0255892276763916, -0.042829226702451706, -0...

Model Information

Model Name: tfhub_use
Type: embeddings
Compatibility: Spark NLP 2.4.0
License: Open Source
Edition: Official
Input Labels: [sentence]
Output Labels: [sentence_embeddings]
Language: [en]
Dimension: 512
Case sensitive: true

Data Source

The model is imported from https://tfhub.dev/google/universal-sentence-encoder/2