@@ -25,25 +25,24 @@ async function myFirstTfjs() {
2525 // Create a simple model.
2626 const model = tf . sequential ( ) ;
2727 model . add ( tf . layers . dense ( { units : 1 , inputShape : [ 1 ] } ) ) ;
28+
2829 // Prepare the model for training: Specify the loss and the optimizer.
29- model . compile ( { loss : 'meanSquaredError' ,
30- optimizer : 'sgd' ,
31- useBias : 'true' } ) ;
30+ model . compile ( {
31+ loss : 'meanSquaredError' ,
32+ optimizer : 'sgd'
33+ } ) ;
34+
3235 // Generate some synthetic data for training. (y = 2x - 1)
3336 const xs = tf . tensor2d ( [ - 1 , 0 , 1 , 2 , 3 , 4 ] , [ 6 , 1 ] ) ;
3437 const ys = tf . tensor2d ( [ - 3 , - 1 , 1 , 3 , 5 , 7 ] , [ 6 , 1 ] ) ;
38+
3539 // Train the model using the data.
3640 await model . fit ( xs , ys , { epochs : 250 } ) ;
41+
3742 // Use the model to do inference on a data point the model hasn't seen.
38- // Perform within a tf.tidy block to perform cleanup on intermediate tensors.
39- tf . tidy ( ( ) => {
40- // Should print approximately 39.
41- document . getElementById ( 'micro_out_div' ) . innerText += model . predict (
42- tf . tensor2d ( [ 20 ] , [ 1 , 1 ] ) ) ;
43- } ) ;
44- // Manually clean up the memory for these variables.
45- xs . dispose ( ) ;
46- ys . dispose ( ) ;
43+ // Should print approximately 39.
44+ document . getElementById ( 'micro_out_div' ) . innerText +=
45+ model . predict ( tf . tensor2d ( [ 20 ] , [ 1 , 1 ] ) ) ;
4746}
4847
4948myFirstTfjs ( ) ;
0 commit comments