|
1 | 1 | # tfjs node tiny
|
2 | 2 | A light-weight, 193MB version of `@tensorflow/tfjs-node` to perform inference on any TensorFlow model in the SavedModel format.
|
3 |
| -This repository trims all built-in TensorFlow components used for model training, while still allowing for quicker model inference. |
| 3 | +This repository trims all built-in TensorFlow components used for model training, while still allowing for faster model inference. |
4 | 4 |
|
5 |
| -With ≈450 MB reduction in module size, ≈150%-200% the speed to load a model, and slightly faster model inference, this repository outperforms the `@tensorflow/tfjs-node` module in model inference. |
| 5 | +With ≈450 MB reduction in module size, ≈150%-200% the speed to load a model, and slightly faster model inference, this repository outperforms the `@tensorflow/tfjs-node` module in resource efficiency. |
| 6 | + |
| 7 | + |
| 8 | +### Code Comparison |
| 9 | +`@tensorflow/tfjs-node`: |
| 10 | + |
| 11 | +```js |
| 12 | +async function run() { |
| 13 | + const { node, tensor} = require('@tensorflow/tfjs-node') |
| 14 | + const { bert_multilingual_encode } = require('./tfjs-node-tiny/bert-tokenizer') |
| 15 | + const model = await node.loadSavedModel('./bert-small-multilingual'); |
| 16 | + const input = bert_multilingual_encode(`What's up?`); |
| 17 | + while (input.length < 192) input.push(0); |
| 18 | + let t = tensor(input, [192], 'int32'); |
| 19 | + const prediction = model.predict({ |
| 20 | + input_ids: t |
| 21 | + })['output_0']; |
| 22 | + console.log(prediction.max().arraySync()); |
| 23 | +} |
| 24 | +run() |
| 25 | +``` |
| 26 | +<br> |
| 27 | + |
| 28 | +`tfjs-node-tiny`: |
| 29 | + |
| 30 | +```js |
| 31 | +async function run() { |
| 32 | + const { bert_multilingual_encode } = require('./tfjs-node-tiny/bert-tokenizer'); |
| 33 | + const { loadSavedModel, tensor} = require('./tfjs-node-tiny/node'); |
| 34 | + const model = await loadSavedModel('./bert-small-multilingual'); |
| 35 | + const input = bert_multilingual_encode(`What's up?`); |
| 36 | + let t = tensor(input, [192], 'int32'); |
| 37 | + const prediction = model.predict({ |
| 38 | + input_ids: t |
| 39 | + })['output_0']; |
| 40 | + console.log(prediction.dataSync()[0]) |
| 41 | +} |
| 42 | +run() |
| 43 | +``` |
| 44 | + |
| 45 | +### Setup |
| 46 | + |
| 47 | +```bash |
| 48 | +node setup.js |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +### Model Release |
0 commit comments