Skip to content

Commit 08a0e60

Browse files
bileschitafsiri
authored andcommitted
Add minimal non-transpiled example
1 parent dd81cd4 commit 08a0e60

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

getting_started/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TensorFlow.js Example: Getting Started
2+
3+
This minimal example loads tfjs from a CDN, builds and trains a minimal model,
4+
and uses it to predict. Edit `index.js` and load `index.html` in your
5+
browser to test small snippets.

getting_started/index.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!--
2+
Copyright 2018 Google LLC. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================
16+
-->
17+
18+
<!-- This tiny example illustrates how little code is necessary build
19+
/ train / predict from a model in TensorFlow.js. Edit the associated
20+
index.js code and refresh this file in your browser to quickly
21+
explore the API. -->
22+
23+
24+
<html>
25+
<head>
26+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
27+
</head>
28+
<body>
29+
Tiny TFJS example.<hr>
30+
<div id="micro_out_div"></div>
31+
<script src="./index.js"> </script>
32+
</body>
33+
</html>

getting_started/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http:// www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
* ==============================================================================
17+
*/
18+
19+
// This tiny example illustrates how little code is necessary build /
20+
// train / predict from a model in TensorFlow.js. Edit this code
21+
// and refresh the index.html to quickly explore the API.
22+
23+
// Tiny TFJS train / predict example.
24+
async function myFirstTfjs() {
25+
// Create a simple model.
26+
const model = tf.sequential();
27+
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
28+
// Prepare the model for training: Specify the loss and the optimizer.
29+
model.compile({loss: 'meanSquaredError',
30+
optimizer: 'sgd',
31+
useBias: 'true'});
32+
// Generate some synthetic data for training. (y = 2x - 1)
33+
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
34+
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
35+
// Train the model using the data.
36+
await model.fit(xs, ys, {epochs: 250});
37+
// 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();
47+
}
48+
49+
myFirstTfjs();

getting_started/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "tfjs-examples-micro",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"license": "Apache-2.0",
7+
"private": true,
8+
"engines": {
9+
"node": ">=8.9.0"
10+
},
11+
"dependencies": { },
12+
"scripts": {
13+
"watch": "npm run build && node_modules/http-server/bin/http-server dist -p 1234 ",
14+
"build": "mkdir -p dist/ && cp index.html dist/ && cp index.js dist/"
15+
},
16+
"devDependencies": {
17+
"clang-format": "~1.2.2",
18+
"http-server": "~0.10.0"
19+
}
20+
}

0 commit comments

Comments
 (0)