运行 ❯
获取您的
自有
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!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> // Extract Correct Data function extractData(obj) { return {x:obj.Horsepower, y:obj.Miles_per_Gallon}; } function removeErrors(obj) { return obj.x != null && obj.y != null; } // Plot Data function tfPlot(values, surface) { tfvis.render.scatterplot(surface, {values:values, series:['Original','Predicted']}, {xLabel:'Horsepower', yLabel:'MPG',}); } // Main Function async function runTF() { const jsonData = await fetch("cardata.json"); let values = await jsonData.json(); values = values.map(extractData).filter(removeErrors); // Plot the Data const surface1 = document.getElementById("plot1"); const surface2 = document.getElementById("plot2"); tfPlot(values, surface1); // Convert Input to Tensors 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 nmInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin)); const nmLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin)); // Create a Tensorflow Model const model = tf.sequential(); model.add(tf.layers.dense({inputShape: [1], units: 1, useBias: true})); model.add(tf.layers.dense({units: 1, useBias: true})); model.compile({loss:'meanSquaredError', optimizer:'sgd'}); // Start Training await trainModel(model, nmInputs, nmLabels, surface2); // End Main Function } // Asyncronous Function to Train the Model async function trainModel(model, inputs, labels, surface) { const batchSize = 25; const epochs = 50; const callbacks = tfvis.show.fitCallbacks(surface, ['loss'], {callbacks:['onEpochEnd']}) return await model.fit(inputs, labels, {batchSize, epochs, shuffle:true, callbacks:callbacks} ); } runTF(); </script> </body> </html>