Guest User

Untitled

a guest
Jun 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. const tf = require('@tensorflow/tfjs');
  2. require('@tensorflow/tfjs-node');
  3. tf.setBackend('tensorflow');
  4.  
  5. var fs = require('fs');
  6. var performance = require('perf_hooks').performance;
  7.  
  8. const model_path = 'file://' + __dirname + '/model/model.json';
  9. const model_metadata = __dirname + '/model/metadata.json';
  10.  
  11. var text = 'this is a bad day';
  12.  
  13. tf.loadModel(model_path)
  14. .then(model => {
  15.  
  16. let sentimentMetadata = JSON.parse(fs.readFileSync(model_metadata));
  17. //console.log(sentimentMetadata);
  18.  
  19. let indexFrom = sentimentMetadata['index_from'];
  20. let maxLen = sentimentMetadata['max_len'];
  21. let wordIndex = sentimentMetadata['word_index'];
  22.  
  23. console.log('indexFrom = ' + indexFrom);
  24. console.log('maxLen = ' + maxLen);
  25.  
  26. console.log('model_type', sentimentMetadata['model_type']);
  27. console.log('vocabulary_size', sentimentMetadata['vocabulary_size']);
  28. console.log('max_len', sentimentMetadata['max_len']);
  29.  
  30. const inputText =
  31. text.trim().toLowerCase().replace(/(.|,|!)/g, '').split(/s+/g); // tokenized
  32.  
  33. console.log(inputText);
  34.  
  35. // Look up word indices.
  36. const inputBuffer = tf.buffer([1, maxLen], 'float32');
  37. for (let i = 0; i < inputText.length; ++i) {
  38. const word = inputText[i];
  39. if (typeof wordIndex[word] == 'undefined') { // TODO(cais): Deal with OOV words.
  40. console.log(word, wordIndex[word]);
  41. }
  42. inputBuffer.set(wordIndex[word] + indexFrom, 0, i);
  43. }
  44. const input = inputBuffer.toTensor();
  45.  
  46. console.log(text, "n", input);
  47.  
  48. const beginMs = performance.now();
  49. const predictOut = model.predict(inputBuffer);
  50. const score = predictOut.dataSync()[0];
  51. predictOut.dispose();
  52. const endMs = performance.now();
  53. console.log({ score: score, elapsed: (endMs - beginMs) });
  54.  
  55. })
  56. .catch(error => {
  57. console.error(error)
  58. })
  59.  
  60. Error: Argument 'x' passed to 'slice' must be a Tensor, but got object.
  61.  
  62. Tensor {
  63. isDisposedInternal: false,
  64. size: 100,
  65. shape: [ 1, 100 ],
  66. dtype: 'float32',
  67. strides: [ 100 ],
  68. dataId: {},
  69. id: 22,
  70. rankType: '2' }
  71.  
  72. const input = inputBuffer.toTensor();
  73.  
  74. {
  75. "dependencies": {
  76. "@tensorflow/tfjs": "^0.11.5",
  77. "@tensorflow/tfjs-node": "^0.1.7"
  78. },
  79. "scripts": {
  80. "postinstall": "yarn upgrade --pattern @tensorflow"
  81. }
  82. }
Add Comment
Please, Sign In to add comment