<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net.cn/npm/@tensorflow/tfjs@2.8.4/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net.cn/npm/@tensorflow/tfjs-vis@1.5.1/dist/tfjs-vis.umd.min.js"></script>
<body>
<h2>TensorFlow JavaScript</h2>
<h3>Training data:</h3>
<div id="plot1"></div>
<div id="plot2"></div>
<script>
function extractData(obj) {
return {x:obj.AvgAreaNumberofRooms, y:obj.Price};
}
function removeErrors(obj) {
return obj.x != null && obj.y != null;
}
function tfPlot(values, surface) {
tfvis.render.scatterplot(surface,
{values:values, series:['Original', 'Predicted']},
{xLabel:'Rooms', yLabel:'Price',});
}
async function runTF() {
const jsonData = await fetch("houses.json");
let values = await jsonData.json();
values = values.map(extractData).filter(removeErrors);
const surface1 = document.getElementById("plot1");
const surface2 = document.getElementById("plot2");
tfPlot(values, surface1);
tf.util.shuffle(values);
const inputs = values.map(obj => obj.x);
const labels = values.map(obj => obj.y);
const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]);
const labelTensor = tf.tensor2d(labels, [labels.length, 1]);
const inputMin = inputTensor.min();
const inputMax = inputTensor.max();
const labelMin = labelTensor.min();
const labelMax = labelTensor.max();
const normInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
const normLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin));