Skip to content

Commit

Permalink
Support Deno-client
Browse files Browse the repository at this point in the history
  • Loading branch information
nurlicht committed Jan 6, 2024
1 parent 87f3d57 commit 9294d5e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Interactive data generation and fitting with TensorFlow.js [![](https://github.com/nurlicht/TensorFlow/actions/workflows/node.js.yml/badge.svg)](https://github.com/nurlicht/TensorFlow/actions)
# Interactive data generation and fitting [![](https://github.com/nurlicht/TensorFlow/actions/workflows/node.js.yml/badge.svg)](https://github.com/nurlicht/TensorFlow/actions)[![](https://github.com/nurlicht/TensorFlow/actions/workflows/deno.yml/badge.svg)](https://github.com/nurlicht/TensorFlow/actions)

### Background
See the codelab [TensorFlow.js — Making Predictions from 2D Data](https://codelabs.developers.google.com/codelabs/tfjs-training-regression) for an introduction to TenosrFlow.js and also to the use-case and the code here.
Expand All @@ -17,10 +17,18 @@ See the codelab [TensorFlow.js — Making Predictions from 2D Data](https://code

### Execution
- Browser client
- Double-click on the file [index.html](./src/html/index.html) (or open it with your browser of choice). No server is needed. There is no node.js dependency.
- Double-click on the file [index.html](./src/html/index.html) (or open it with your browser of choice). No server is needed.
- Node.js client
- Run the script ```npm install``` and then ```npm test```.
- Run the command ```npm install``` and then ```npm test```.
- An example of this client is the [GitHub Action](./.github/workflows/node.js.yml) of this project.
- Deno client
- Run the command ```Deno test ./tests/javascript/deno/test.js --allow-read```.
- An example of this client is the [GitHub Action](./.github/workflows/deno.yml) of this project.
- If the main client is a Deno-client, it is recommended that the main code be imported in the test file. The current implementation is based on the restriction to support 3 different clients.

### CI/CD
- GitHub Action for [node.js](./.github/workflows/node.js.yml)
- GitHub Action for [Deno](./.github/workflows/deno.yml)

### Snapshot of Control-Parameters and Outputs
The sliders for ```epochs``` (250 vs. 400) and ```noise``` (2.5% vs. 5%) provide a dynamic and user-defined compromise between <i>accuracy</i> and <i>speed</i> (Note that these stochastic results are reproducible only in a statistical sense).
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
"description": "",
"main": "./src/javascript/index.js",
"type": "module",
"directories": {
"test": "./test/javascript"
},
"directories": {},
"scripts": {
"test": "jest"
"test": "jest ./tests/javascript/node"
},
"keywords": [],
"author": "",
Expand Down
17 changes: 15 additions & 2 deletions src/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ class Controller {

class App {
static async run(inputDataParameters) {
// Set inputDataParameters if not done
if (typeof inputDataParameters !== 'object') {
inputDataParameters = InputDataParameters.default();
console.warn('inputDataParameters was set to defaults.')
}

// Initialize the scatter-plot
View.showScatterPlot([]);

Expand Down Expand Up @@ -385,5 +391,12 @@ class App {
}
}

exports.App = App;
exports.InputDataParameters = InputDataParameters;
if (typeof Deno !== 'undefined' && Deno.version) {
console.log('Deno is defined.');
} else if (typeof module !== 'undefined' && module.exports) {
console.log('Node is defined.');
module.exports.App = App;
module.exports.InputDataParameters = InputDataParameters;
} else {
console.log('Browser is defined.');
}
20 changes: 20 additions & 0 deletions tests/javascript/deno/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import tfjs from 'npm:@tensorflow/tfjs';
import { assert } from "https://deno.land/std@0.211.0/assert/mod.ts";
const codeAsString = Deno.readTextFileSync('./src/javascript/index.js');

Deno.test('Health-Check', async () => {
assert((await new Promise((res, rej) => res(4))) === 4);
}, 1000);

Deno.test('TFJS app can be started and the fitted results are valid.', async () => {
const tfjsApi = {tf: tfjs, tfvis: {render: {scatterplot: ()=>{}}}};
const code = codeAsString + 'App.start(undefined, tfjsApi);';
const result = await eval(code);
assert(typeof result === 'object')
assert(Array.isArray(result));
assert(result.length === 2);
assert(Array.isArray(result[0]));
assert(Array.isArray(result[1]));
assert(result[1].length === 400);
assert(result[1][0].x === 0.5);
}, 50000);
2 changes: 1 addition & 1 deletion test/javascript/test.js → tests/javascript/node/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const tfjs = require('@tensorflow/tfjs');
const AppModule = require('../../src/javascript/index.js');
const AppModule = require('../../../src/javascript/index.js');
const App = AppModule.App;
const InputDataParameters = AppModule.InputDataParameters;

Expand Down

0 comments on commit 9294d5e

Please # to comment.