Tensorflow in node-red

Hello,

I try to use tensorflow npm without luck.
What is done: npm module is in .node-red\node_modules@tensorflow via npm i @tensorflow/tfjs

I tried using: https://www.npmjs.com/package/node-red-contrib-function-npm which helped me with other libraries.

Node:

[{"id":"f6ae15b3.57f0a8","type":"function-npm","z":"f44e3317.597e7","name":"tensorflow","func":"\n\n//const assert = require('assert');\nconst tf = require('@tensorflow/tfjs');\n//import * as tf from '@tensorflow/tfjs';\n \n// Define a model for linear regression.\nconst model = tf.sequential();\nmodel.add(tf.layers.dense({units: 1, inputShape: [1]}));\n \n// Prepare the model for training: Specify the loss and the optimizer.\nmodel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});\n \n// Generate some synthetic data for training.\nconst xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);\nconst ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);\n \n// Train the model using the data.\nmodel.fit(xs, ys).then(() => {\n  // Use the model to do inference on a data point the model hasn't seen before:\n  model.predict(tf.tensor2d([5], [1, 1])).print();\n})\n","outputs":1,"noerr":0,"x":340,"y":100,"wires":[[]]}] 

I get error: Error: Cannot find module 'C:\Users\KAROLI~1\AppData\Local\Temp\d-119111-12464-1epjuwk.vorz/node_modules/'

When I try to use:

functionGlobalContext: {
tf:require('@tensorflow/tfjs')

Node:

[{"id":"d7cdfe07.39642","type":"function","z":"f44e3317.597e7","name":"","func":"//const assert = require('assert');\nconst tf = global.get('tf');\n//import * as tf from '@tensorflow/tfjs';\n \n// Define a model for linear regression.\nconst model = tf.sequential();\nmodel.add(tf.layers.dense({units: 1, inputShape: [1]}));\n \n// Prepare the model for training: Specify the loss and the optimizer.\nmodel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});\n \n// Generate some synthetic data for training.\nconst xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);\nconst ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);\n \n// Train the model using the data.\nmodel.fit(xs, ys).then(() => {\n  // Use the model to do inference on a data point the model hasn't seen before:\n  model.predict(tf.tensor2d([5], [1, 1])).print();\n})\n\nmsg.payload = xs;\n\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":420,"wires":[["b2aa70b.0641c9"]]}]

I receive error: TypeError: Cannot read property 'Sequential' of undefined

What am I missing?

My bad, I found problem.

I have 2 node-red insances, and was modifying wrong settings.js file

Hey @KarolisL,

Glad that you have found the cause (by yourself :wink: ) ...

If you ever find an interesting use case for Tensorflow in Node-RED, it would be nice if you could share your experiences here with us. I'm interested a lot in Tensorflow, but have no time to dig into it myself ...

Thanks in advance!!
Bart

Will try for sure. But all the experience is limited at tutorials level, which is hard to find at beginner level:

I started with: https://github.com/BrainJS/brain.js but problem was with training models and as I found it was stable only with sigmoid and leaky-relu.

First steps to start for me was: https://medium.freecodecamp.org/get-to-know-tensorflow-js-in-7-minutes-afcd0dfd3d2f

If anyone has better info than git tutorials please share

1 Like