Skip to content

Instantly share code, notes, and snippets.

@rish-16
Created April 8, 2018 05:26
Show Gist options
  • Save rish-16/17b62729424e35dd8427bed89d3d0728 to your computer and use it in GitHub Desktop.
Save rish-16/17b62729424e35dd8427bed89d3d0728 to your computer and use it in GitHub Desktop.
// 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: 1,
inputShape: [1],
activation: 'softmax'
}));
// Specify the loss type and optimizer for training.
model.compile({loss: 'meanSquaredError', optimizer: 'adam'});
// Generate some synthetic data for training.
const xs = tf.tensor2d([[1], [2], [3], [4], [5], [6], [7]], [7, 1]);
const ys = tf.tensor2d([[1], [3], [5], [7], [9], [11], [13]], [7, 1]);
// Train the model.
model.fit(xs, ys, {epochs: 1000});
// After the training, perform inference.
const pred = model.predict(tf.tensor2d([[8]], [1, 1]));
pred.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment