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:

1model.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():

01// Using the model
02function myFunction( ) {
03 const xMax = 10;
04 const xArr = [ ];
05 const yArr = [ ];
06 for ( let x = 0; x <= xMax; x++ ) {
07  let result = model.predict( tf.tensor( [ Number(x) ] ) );
08  result.data( ).then( y => {
09    xArr.push( x );
10    yArr.push( Number( y ) );
11    if ( x == xMax ) { plot( xArr, yArr ) };
12  } );
13 }
14 document.getElementById( 'message' ).style.display = "none";
15}

An Example of Tensorflow Projects

     




      My mother never saw the irony in calling me a son-of-a-bitch.