MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / extractEmbeddingMatrix

Function extractEmbeddingMatrix

sentiment/embedding.js:34–46  ·  view source on GitHub ↗

* Extract the first embedding matrix from a TensorFlow.js model. * * @param {tf.model} model An instance of tf.Model, assumed to contain an * Embedding layer. * @retuns {tf.Tensor} The embedding matrix from the first Embedding * layer encoutnered while iterating through all layers of the mo

(model)

Source from the content-addressed store, hash-verified

32 * @throws Error if no embedding layer can be found in the model.
33 */
34function extractEmbeddingMatrix(model) {
35 for (const layer of model.layers) {
36 if (layer.getClassName() === 'Embedding') {
37 const embed = layer.getWeights()[0];
38 tf.util.assert(
39 embed.rank === 2,
40 `Expected the rank of an embedding matrix to be 2, ` +
41 `but got ${embed.rank}`);
42 return embed;
43 }
44 }
45 throw new Error('Cannot find any Embedding layer in model.');
46}
47
48/**
49 * Write the values of the first embedding matrix of a model to files.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected