Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
898 B
6
Indexable
Never
const tf = require('@tensorflow/tfjs-node')

async function run() {
  // 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, 5, 6, 20, 21, 22], [11, 1]);
  const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7, 9, 11, 39, 41, 43], [11, 1]);

  // Train the model using the data.
  await model.fit(xs, ys, {epochs: 250});
  console.log(xs.isNaN())
  // Use the model to do inference on a data point the model hasn't seen.
  // Should print approximately 39.

  console.log(model.predict(tf.tensor2d([20], [1, 1])).dataSync())
  console.log(model.predict(tf.tensor2d([21], [1, 1])).dataSync())
}

run();
Leave a Comment