Skip to content

Instantly share code, notes, and snippets.

@Orbifold
Created August 24, 2018 18:33
Show Gist options
  • Select an option

  • Save Orbifold/04011f36b6af9e942863a32845b9b7e2 to your computer and use it in GitHub Desktop.

Select an option

Save Orbifold/04011f36b6af9e942863a32845b9b7e2 to your computer and use it in GitHub Desktop.
Learning the cosine function with TensorFlow.js
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]">
</script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div class="container">
<h1>Learning a cosine using TF.js </h1>
<div id="done" style="height: 20px; background-color: limegreen; margin: 50px 0;"></div>
<div id="loss"></div>
<div id="result"></div>
</div>
<script>
const N = 100;
const M = 10;
var loss = [];
async function plot_loss(h) {
loss.push(h.history.loss[0]);
Plotly.newPlot('loss', [{
x: loss.length,
y: loss,
name: "loss",
type: 'scatter'
}], {
title: "Current loss: " + Math.round(h.history.loss[0]*10000)/10000,
xaxis: {
title: "run"
},
yaxis: {
title: "loss"
}
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function run() {
const b = _.range(N);
const model = tf.sequential();
model.add(tf.layers.dense({
name: "input",
units: 1,
inputShape: [1]
}));
model.add(tf.layers.dense({
name: "learning_stack_1",
activation: "tanh",
kernelInitializer: "randomNormal",
units: 15
}));
model.add(tf.layers.dense({
name: "learning_stack_2",
activation: "tanh",
kernelInitializer: "randomNormal",
units: 15
}));
model.add(tf.layers.dense({
name: "learning_stack_3",
activation: "tanh",
kernelInitializer: "randomNormal",
units: 15
}));
model.add(tf.layers.dense({
name: "outputter",
activation: "linear",
kernelInitializer: "randomNormal",
units: 1
}));
model.compile({
loss: 'meanSquaredError',
optimizer: 'adam'
});
const xs = tf.tensor2d(b, [N, 1]);
const ys = tf.tensor2d(_.map(b, x => Math.cos(2 * Math.PI * x / N)), [N, 1]);
for (let i = 1; i < M; ++i) {
const h = await model.fit(xs, ys, {
batchSize: 10,
epochs: 10
});
//console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
plot_loss(h);
$("#done").css("width", `${100*i/(M-1)}%`).text(`${Math.round(100*i/(M-1))}%`);
await sleep(100);
}
model.predict(xs).data().then(function(p) {
var actual = {
x: b,
y: _.map(b, x => Math.cos(2 * Math.PI * x / N)),
name: "actual data",
type: 'scatter',
mode: 'lines',
};
var predicted = {
x: b,
y: p,
name: "predicted data",
type: 'scatter'
};
Plotly.newPlot('result', [actual, predicted]);
});;
}
//$("#go").click(run);
run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment