* 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)
| 32 | * @throws Error if no embedding layer can be found in the model. |
| 33 | */ |
| 34 | function 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. |
no outgoing calls
no test coverage detected