Code
Let explain the code..
As we mention we need to create a html file, and add two libraries the first one for tensorflow and the second for a chart library.
<html>
<head>
<!-- TensorFlow.js version 0.11.2 -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"> </script>
</head>
<body>We are set html design, where we define:
x , y as input value
Submit button
Canvas where we are going to display our graph.
<h3>Input your data and let TensorFlow give a best-fit line predict</h3>
x: <input type="text" id="x" />
y: <input type="text" id="y" />
<input type="button" id="append" value="submit">
<div style="padding:50px">
<canvas id="graph" width="80%" height="50%"></canvas>
</div>The next script block will handle populating the data, graphing the chart, and later calculating the best-fit line.
We will start to give values with data from the inputs. From there, we're just going to automatically increment x by 1 in the field with. Then with a .onclick function we start to populate those values when the submit button is pressed.
Then we start to define our model using TensorFlow object and then train the model based on the data the user has inpu.
In this part we could define which layer we are going to use: LeakyReLu or a Dense
TensorFlow works with JavaScript promises, which are just asynchronous operations that return either a successful result or an error. We want this to be asynchronous so that our application doesn't just hang.
After fitment, we want to calculate our bestfit line:
Then we could create our graph based on input data and the best-fit line, making our full fitment and graph function:
Last updated
Was this helpful?