WebGL and Node.js: setBackend('webgl') error

743 views
Skip to first unread message

Loreto Parisi

unread,
May 11, 2018, 4:28:55 AM5/11/18
to TensorFlow.js Discussion
I'm building some examples for the new npm module in @tensorflow/tfj-node in node.js javascript rather than typescript like

require('@tensorflow/tfjs-node');
var tf = require('@tensorflow/tfjs');
tf
.setBackend('tensorflow');
//tf.setBackend('webgl');


// Tiny TFJS train / predict example.
function myFirstTfjs() {
   
// Create a simple model.
   
const model = tf.sequential();
    model
.add(tf.layers.dense({units: 1, inputShape: [1]}));


   
// Prepare the model for training: Specify the loss and the optimizer.
    model
.compile({
      loss
: 'meanSquaredError',
      optimizer
: 'sgd'
   
});


   
// Generate some synthetic data for training. (y = 2x - 1)
   
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
   
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);


   
// Train the model using the data.
    model
.fit(xs, ys, {epochs: 500});


   
// Use the model to do inference on a data point the model hasn't seen.
   
// Should print approximately 39.
   
var predict=
        model
.predict(tf.tensor2d([20], [1, 1]));


    console
.log( "PREDICT\n", predict );
 
}


  myFirstTfjs
()
See here other examples coming next: https://github.com/loretoparisi/tensorflow-node-examples

This works fine, but I don't understand the
setBackend

Is it supposed to load a `cpu` or `webgl` backend from the docs here:https://js.tensorflow.org/api/0.10.0/#setBackend

while in the module the 

tf.setBackend('webgl');

will not work, so how to set a WebGL context backend? In Node.js there are some WebGL bindings, but I was aware that TF team was working to WASM / WebAssembly for that.

David Soergel

unread,
May 11, 2018, 9:33:38 AM5/11/18
to loreto...@gmail.com, TensorFlow.js Discussion
If you're running under node.js, then tf.setBackend('tensorflow') is correct and is the only option.  I see we need to update the API docs to explain that.  The 'cpu' and 'webgl' options only make sense in the browser context.

-ds

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/43e8aaff-d390-45ac-b22f-63e5e93e61be%40tensorflow.org.

Loreto Parisi

unread,
May 12, 2018, 6:12:02 AM5/12/18
to David Soergel, TensorFlow.js Discussion
Ok it makes sense, but at this time I don't understand if the node binding are using the GPU having this backend. Since the other are devices like cpu, webgl rather than "tensorflow", it seems to be that setting tensorflow.setBackend("tensorflow") it's a bit confusing, isn'it?
So, that said, the question is. Supposed I'm running Ubuntu + CUDA / NVIDIA, will tfjs-node see the GPU using this backend?
What happens on macOS when using NVIDIA? Supposed we run webGL, and we are on macOS, will Radeon be supported via WebGL 2 OpenCL? Thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+unsubscribe@tensorflow.org.



--
Dott. Ing. Loreto Parisi
Parisi Labs

Company: in...@parisilabs.com

Nick Kreeger

unread,
May 12, 2018, 3:52:27 PM5/12/18
to loreto...@gmail.com, David Soergel, tf...@tensorflow.org
Yeah sorry this is a little confusing. The TensorFlow node.js backend is CPU only right now. We're working on adding GPU support through CUDA soon. 

The WebGL backend is for the browser only. We are planning to make a change to the core library to automatically selecting the backend. This is why we are exposing tf.setBackend() for now.

Thanks,
Nick

To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.



--
Dott. Ing. Loreto Parisi
Parisi Labs

Company: in...@parisilabs.com

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Reply all
Reply to author
Forward
0 new messages