使用 node.js 加载 tensorflow.js 时出错

Error when loading tensorflow.js with node.js

我是 javascript 的新手。我正在研究 tensorflow-js tutorial 将 tensorflow-js 与 node.js 结合使用,但是当我 运行 示例代码时:

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}`);
    }
  }
});

我已经安装了tensorflowjs包: npm 安装@tensorflow/tfjs-node-gpu

但是我收到错误:

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

我不确定是什么导致了这个错误。

您需要:

npm install @tensorflow/tfjs-node