|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import * as tf from '@tensorflow/tfjs'; |
| 19 | + |
| 20 | +import {Tokenizer} from './tokenizer'; |
| 21 | + |
| 22 | +const BASE_PATH = |
| 23 | + 'https://storage.googleapis.com/tfjs-models/savedmodel/universal_sentence_encoder/'; |
| 24 | + |
| 25 | +export async function load() { |
| 26 | + const use = new UniversalSentenceEncoder(); |
| 27 | + await use.load(); |
| 28 | + return use; |
| 29 | +} |
| 30 | + |
| 31 | +export class UniversalSentenceEncoder { |
| 32 | + private model: tf.FrozenModel; |
| 33 | + private tokenizer: Tokenizer; |
| 34 | + |
| 35 | + async loadModel() { |
| 36 | + return tf.loadFrozenModel( |
| 37 | + `${BASE_PATH}tensorflowjs_model.pb`, |
| 38 | + `${BASE_PATH}weights_manifest.json`); |
| 39 | + } |
| 40 | + |
| 41 | + async loadVocabulary() { |
| 42 | + const vocabulary = await fetch(`${BASE_PATH}vocab.json`); |
| 43 | + return vocabulary.json(); |
| 44 | + } |
| 45 | + |
| 46 | + async load() { |
| 47 | + const [model, vocabulary] = |
| 48 | + await Promise.all([this.loadModel(), this.loadVocabulary()]); |
| 49 | + |
| 50 | + this.model = model; |
| 51 | + this.tokenizer = new Tokenizer(vocabulary); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * |
| 56 | + * Returns a 2D Tensor of shape [input.length, 512] that contains the |
| 57 | + * Universal Sentence Encoder embeddings for each input. |
| 58 | + * |
| 59 | + * @param inputs A string or an array of strings to embed. |
| 60 | + */ |
| 61 | + async embed(inputs: string[]|string): Promise<tf.Tensor2D> { |
| 62 | + if (typeof inputs === 'string') { |
| 63 | + inputs = [inputs]; |
| 64 | + } |
| 65 | + |
| 66 | + const encodings = inputs.map(d => this.tokenizer.encode(d)); |
| 67 | + |
| 68 | + const indicesArr = |
| 69 | + encodings.map((arr, i) => arr.map((d, index) => [i, index])); |
| 70 | + |
| 71 | + let flattenedIndicesArr: Array<[number, number]> = []; |
| 72 | + for (let i = 0; i < indicesArr.length; i++) { |
| 73 | + flattenedIndicesArr = |
| 74 | + flattenedIndicesArr.concat(indicesArr[i] as Array<[number, number]>); |
| 75 | + } |
| 76 | + |
| 77 | + const indices = tf.tensor2d( |
| 78 | + flattenedIndicesArr, [flattenedIndicesArr.length, 2], 'int32'); |
| 79 | + const values = tf.tensor1d(tf.util.flatten(encodings) as number[], 'int32'); |
| 80 | + |
| 81 | + const embeddings = await this.model.executeAsync({indices, values}); |
| 82 | + indices.dispose(); |
| 83 | + values.dispose(); |
| 84 | + |
| 85 | + return embeddings as tf.Tensor2D; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export {Tokenizer}; |
0 commit comments