@@ -25,25 +25,24 @@ async function myFirstTfjs() {
25
25
// Create a simple model.
26
26
const model = tf . sequential ( ) ;
27
27
model . add ( tf . layers . dense ( { units : 1 , inputShape : [ 1 ] } ) ) ;
28
+
28
29
// 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
+
32
35
// Generate some synthetic data for training. (y = 2x - 1)
33
36
const xs = tf . tensor2d ( [ - 1 , 0 , 1 , 2 , 3 , 4 ] , [ 6 , 1 ] ) ;
34
37
const ys = tf . tensor2d ( [ - 3 , - 1 , 1 , 3 , 5 , 7 ] , [ 6 , 1 ] ) ;
38
+
35
39
// Train the model using the data.
36
40
await model . fit ( xs , ys , { epochs : 250 } ) ;
41
+
37
42
// 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 ] ) ) ;
47
46
}
48
47
49
48
myFirstTfjs ( ) ;
0 commit comments