Tensorflow.js полиномиальная регрессия

#javascript #tensorflow.js

Вопрос:

Я создаю небольшую веб-страницу по визуализации прогнозирования полиномиальной регрессии с помощью math.js и Tensorflow.js и, похоже, у меня это не очень хорошо получается.

Он должен работать на sgd с линейной и полиномиальной регрессией, но sgd вообще не работает, и когда я использую adam для тренировки, он проскакивает даже с 200 точками на чем-то таком простом, как y=2x, и если я переключусь на полиномиальную регрессию с adam, числа бессмысленны. Вот что у меня пока есть:

 let xTry = undefined; let yTry = undefined;    function draw() {  try {  // compile the expression once  const expression = document.getElementById('eq').value  const expr = math.compile(expression)   // evaluate the expression repeatedly for different values of x  const xValues = math.range(-100, 100, 1).toArray()  xTry = xValues;  const yValues = xValues.map(function (x) {  return expr.evaluate({x: x})  })  console.log(yValues);  yTry = yValues;  console.log(xValues);  // render the plot using plotly  const trace1 = {  x: xValues,  y: yValues,  line: {shape: 'spline'},  type: 'scatter'  }  const data = [trace1]  Plotly.newPlot('plot', data)  }  catch (err) {  console.error(err)  }  }   document.getElementById('form').onsubmit = function (event) {  event.preventDefault()  draw()  }    async function learnLinear() {    const model = tf.sequential();  model.add(tf.layers.dense({ units: 1, inputShape: [1], useBias: true }));  //beschrijving dimensies netwerk    model.compile({  loss: "meanSquaredError",  optimizer: "adam",  });    const xtest = tf.tensor2d(xTry, [200, 1])  const ytest = tf.tensor2d(yTry, [200, 1])  // grafiek, x en y waarde met matrix grootte erachter   await model.fit(xtest, ytest, {epochs: 200});  //trainen model 250 epochs  document.getElementById("output_field").innerText = model.predict(  tf.tensor2d([20], [1, 1])  );    }   document.getElementById('form2').onsubmit = function (event) {  event.preventDefault()  learnLinear();  console.log(learnLinear()) }    //heel deze functie is het model trainen   uittesten       //script 1 gemakkelijk trainen   /*   https://mathjs.org/docs/expressions/parsing.html  https://plotly.com/javascript/plotly-fundamentals/  https://plotly.com/javascript/plotly-fundamentals/  https://stackoverflow.com/questions/21518381/proper-way-to-wait-for-one-function-to-finish-before-continuing     */  
 lt;!DOCTYPE htmlgt; lt;html lang="en"gt; lt;headgt;  lt;meta charset="UTF-8"gt;  lt;meta http-equiv="X-UA-Compatible" content="IE=edge"gt;  lt;meta name="viewport" content="width=device-width, initial-scale=1.0"gt;  lt;script defer src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2" type="text/javascript"gt;lt;/scriptgt;  lt;script defer src="https://unpkg.com/mathjs@9.5.1/lib/browser/math.js"gt;lt;/scriptgt;  lt;script defer src="https://cdn.plot.ly/plotly-1.35.2.min.js"gt;lt;/scriptgt;  lt;script defer src="./js/basic_plot.js"gt;lt;/scriptgt;    lt;titlegt;tfjs Hoofdstuk 1lt;/titlegt; lt;/headgt; lt;bodygt;  lt;form id="form"gt;  lt;label for="eq"gt;Enter an equation:lt;/labelgt;  lt;input type="text" id="eq" placeholder="4 * sin(x)   5 * cos(x/2)" /gt;  lt;input id="eqsubm" type="submit" value="Draw" /gt;  lt;/formgt;  lt;form id="form2"gt;  lt;input id="help" type="submit" value="calc" /gt;  lt;/formgt;   lt;div class="test" id="output_field"gt;lt;/divgt;  lt;div id="output2"gt;lt;/divgt;   lt;div id="plot"gt;lt;/divgt; lt;/bodygt; lt;/htmlgt;