2

I am fairly new to javascript. I am working through the tensorflow-js tutorial on using tensorflow-js with node.js however when I run the example code:

const tf = require('@tensorflow/tfjs');

// Load the binding:
require('@tensorflow/tfjs-node');  // Use '@tensorflow/tfjs-node-gpu' if running with GPU.

// Train a simple model:
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

const xs = tf.randomNormal([100, 10]);
const ys = tf.randomNormal([100, 1]);

model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: async (epoch, log) => {
      console.log(`Epoch ${epoch}: loss = ${log.loss}`);
    }
  }
});

I have already installed the tensorflowjs package: npm install @tensorflow/tfjs-node-gpu

However I get the error:

module.js:549
throw err;
Error: Cannot find module '@tensorflow/tfjs-node'

I am not sure what is causing this error.

1 Answer 1

7

You need to:

npm install @tensorflow/tfjs-node
Sign up to request clarification or add additional context in comments.

5 Comments

This worked! Would you mind adding a small comment about what this command does - as it is not listed in the tensorflowjs installation instructions
Its right there in the installation instructions github.com/tensorflow/…
it does not mention --save
Right, you probably don't need that if installing from an existing package.json
// Load the binding: require('@tensorflow/tfjs-node'); // Use '@tensorflow/tfjs-node-gpu' if running with GPU. Right there in the comments of the code. You can use @tensorflow/tfjs-node or if using a GPU with CUDA you can use @tensorflow/tfjs-node-gpu - install the approriate one. They are mutually exclusive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.