Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
 <!-- Lets call the tensorflow library -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
  
</head>
    
<body>
    <h1> <a href="https://www.piecex.com/">PieceX <a/> Article Tensorflow JS for Beginners</h1>
  
  
    <p>Check the Console -> Info section! </p>
    <p>When X=20, Y = <span id="result"> "training.... Please wait" </span></p>
</body>


</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                  // Remember to make an async function. We pass the model and start the training (fit the model)
        async function startTraining(model){
            const history = 
            //We will use 500 epochs and after each epoch we will print it into the console with the loss 
            // This builds the model for the first time:
                  await model.fit(xs, ys, 
                        { epochs: 500,
                          callbacks:{
                              onEpochEnd: async(epoch, logs) =>{
                                  console.log("Epoch:"  + epoch  + " Loss:"  + logs.loss);
                              }
                          }
                        });
        }
        //Initialize the model as sequential, Linear stack of layers. 
        const model = tf.sequential();
        //Add 1 layer (1 neuron), we are specifying the shape 
        model.add(tf.layers.dense({units: 1, inputShape: [1]}));
        //Here we add the loss funtion to the Mean Square error with Stochastic Gradient Decent as the Optimizer 
        model.compile({loss:'meanSquaredError', 
                       optimizer:'sgd'});
        //This will give us a summary of the results of our NN 
        model.summary();
        //Pass the data for training as tensors 2 dimensional. First an array for the data and then the lenght of the array (in this case we have 9 numbers, 1 dimensional array) and the dimension of the array 
        const xs = tf.tensor2d([-4.0,-3.0,-2.0,-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], [9, 1]);
        const ys = tf.tensor2d([-12.0,-10.0,-8.0,-6.0,-4.0,-2.0,0.0,2.0,4.0], [9, 1]);
        //Start the training  
        startTraining(model).then(() => {
          //After ending, Alert the result and update the span with the result as Tensor. Notice that since we work as statistics the prediction is not an integer but a decimal 
            document.getElementById("result").innerHTML = tf.tensor2d([20], [1,1]);
            alert(model.predict(tf.tensor2d([20], [1,1])));

        });
              
            
!
999px

Console