Giter VIP home page Giter VIP logo

tensorflowjs's Introduction

TensorflowJS - Template

This is a template for developing interactive machine-learning courses with LiaScript and TensorFlow.js.

Try it on LiaScript:

https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/tensorflowjs/master/README.md

See the project on Github:

https://github.com/liaTemplates/tensorflowjs

                     --{{1}}--

There are three ways to use this template. The easiest way is to use the import statement and the url of the raw text-file of the master branch or any other branch or version. But you can also copy the required functionionality directly into the header of your Markdown document, see therefor the last slide. And of course, you could also clone this project and change it, as you wish.

                       {{1}}
  1. Load the macros via

    import: https://raw.githubusercontent.com/liaTemplates/tensorflowjs/master/README.md

  2. Copy the definitions into your Project

  3. Clone this repository on GitHub

@TF.eval

                     --{{0}}--

Add the macro @TF.eval to the end of every JavaScript code-block that runs some TensorFlow code and that you want to make editable in LiaScript. The given code gets evaluated asynchroniously and the result is shown in a console below.

// Notice there is no 'import' statement. 'tf' is available on the index-page
// because of the script tag above.

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
  // Use the model to do inference on a data point the model hasn't seen before:
  // Open the browser devtools to see the output
  model.predict(tf.tensor2d([5], [1, 1])).print();
});

@TF.eval

Implementation

                     --{{0}}--

The code shows how the macro @TF.eval is implemented. The script command at the top loads the TensorFlowJS javascript library and onload is used to define function reportError at the initialization phase.

script:   https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js

@onload
window.reportError = function(error) {
  let line = getLineNumber(error);
  let details = [];
  let msg = "An error occured";

  if (line) {
    details = [[{ row : line-1,
               column : 0,
                 text : error.message,
                 type : "error" }]];

    msg += " on line " + line;
  }
  return [ msg + "\n" + error.message, details ];
};
@end

@TF.eval
<script>
async function eee(code) {
  let oldLog = window.console.log;

  window.console.log = console.log;

  try {
    const evalString = '(async function runner() { try { ' + code + '} catch (e) { reportError(e) } })()';

    await eval(evalString).catch(function(e) {
      window.console.log = oldLog;
      let [msg, details] = reportError(e);
      send.lia(msg, details, false);
      send.lia("LIA: stop");
    });
  }
  catch(e) {
    window.console.log = oldLog;
    let [msg, details] = reportError(e);
    send.lia(msg, details, false);
    send.lia("LIA: stop");
  }
  send.lia("LIA: stop");

};
setTimeout(function(e){ eee(`@input`+"\n") }, 10);
"LIA: wait";
</script>
@end
                     --{{1}}--

If you want to minimize loading effort in your LiaScript project, you can also copy this code and paste it into your main comment header, see the code in the raw file of this document.

                       {{1}}

https://raw.githubusercontent.com/liaTemplates/tensorflowjs/master/README.md

tensorflowjs's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.