TensorFlow.js Models (Cont.)


TensorFlow Optimizers Training the Model
Train the model (using xs and ys) with 500 repeats (epochs) by using the fit() function:

model.fit( xs, ys, {epochs:500} ).then( ( ) => { myFunction( ) } );

Using the Model
After the model is trained, you can use it for many different purposes. This example predicts 10 y values, given 10 x values by using the function predict(), and calls a function to plot the predictions in a graph by using the function newPlot():

// Using the model
function myFunction( ) {
 const xMax = 10;
 const xArr = [ ];
 const yArr = [ ];
 for ( let x = 0; x <= xMax; x++ ) { 
  let result = model.predict( tf.tensor( [ Number(x) ] ) );
  result.data( ).then( y => {
    xArr.push( x );
    yArr.push( Number( y ) );
    if ( x == xMax ) { plot( xArr, yArr ) };
  } );
 }
 document.getElementById( 'message' ).style.display = "none";
}

An Example of Tensorflow Projects