Skip to content

Instantly share code, notes, and snippets.

@rish-16
Created April 8, 2018 03:30
Show Gist options
  • Save rish-16/d65225f876d863808d1109b19e2795e9 to your computer and use it in GitHub Desktop.
Save rish-16/d65225f876d863808d1109b19e2795e9 to your computer and use it in GitHub Desktop.
import * as tf from '@tensorflow/tfjs';
// A sequential model is a container which you can add layers to.
const model = tf.sequential();
// Add a dense layer with 1 output unit.
model.add(tf.layers.dense({units: 10,
inputShape: [1],
activation='relu'
}));
model.add(tf.layers.dense({units: 5,
activation='relu'
}));
model.add(tf.layers.dense({units: 1,
activation='softmax'
}));
// Specify the loss type and optimizer for training.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
// Train the model.
await model.fit(xs, ys, {epochs: 500});
// After the training, perform inference.
const pred = model.predict(tf.tensor2d([[5]], [1, 1]));
pred.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment