Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const tf = require('@tensorflow/tfjs');
- require('@tensorflow/tfjs-node');
- tf.setBackend('tensorflow');
- var fs = require('fs');
- var performance = require('perf_hooks').performance;
- const model_path = 'file://' + __dirname + '/model/model.json';
- const model_metadata = __dirname + '/model/metadata.json';
- var text = 'this is a bad day';
- tf.loadModel(model_path)
- .then(model => {
- let sentimentMetadata = JSON.parse(fs.readFileSync(model_metadata));
- //console.log(sentimentMetadata);
- let indexFrom = sentimentMetadata['index_from'];
- let maxLen = sentimentMetadata['max_len'];
- let wordIndex = sentimentMetadata['word_index'];
- console.log('indexFrom = ' + indexFrom);
- console.log('maxLen = ' + maxLen);
- console.log('model_type', sentimentMetadata['model_type']);
- console.log('vocabulary_size', sentimentMetadata['vocabulary_size']);
- console.log('max_len', sentimentMetadata['max_len']);
- const inputText =
- text.trim().toLowerCase().replace(/(.|,|!)/g, '').split(/s+/g); // tokenized
- console.log(inputText);
- // Look up word indices.
- const inputBuffer = tf.buffer([1, maxLen], 'float32');
- for (let i = 0; i < inputText.length; ++i) {
- const word = inputText[i];
- if (typeof wordIndex[word] == 'undefined') { // TODO(cais): Deal with OOV words.
- console.log(word, wordIndex[word]);
- }
- inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
- }
- const input = inputBuffer.toTensor();
- console.log(text, "n", input);
- const beginMs = performance.now();
- const predictOut = model.predict(inputBuffer);
- const score = predictOut.dataSync()[0];
- predictOut.dispose();
- const endMs = performance.now();
- console.log({ score: score, elapsed: (endMs - beginMs) });
- })
- .catch(error => {
- console.error(error)
- })
- Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.
- Tensor {
- isDisposedInternal: false,
- size: 100,
- shape: [ 1, 100 ],
- dtype: 'float32',
- strides: [ 100 ],
- dataId: {},
- id: 22,
- rankType: '2' }
- const input = inputBuffer.toTensor();
- {
- "dependencies": {
- "@tensorflow/tfjs": "^0.11.5",
- "@tensorflow/tfjs-node": "^0.1.7"
- },
- "scripts": {
- "postinstall": "yarn upgrade --pattern @tensorflow"
- }
- }
Add Comment
Please, Sign In to add comment