-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from snowkylin/javascript
Javascript chapter improvements (#6)
- Loading branch information
Showing
5 changed files
with
298 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
source/_static/code/zh/deployment/javascript/mobilenet.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<html> | ||
|
||
<head> | ||
<script src="https://unpkg.com/@tensorflow/tfjs"></script> | ||
<script src="https://unpkg.com/@tensorflow-models/mobilenet"> </script> | ||
</head> | ||
|
||
<video width=400 height=300></video> | ||
<p></p> | ||
<img width=400 height=300 /> | ||
|
||
<script> | ||
const video = document.querySelector('video') | ||
const image = document.querySelector('img') | ||
const status = document.querySelector("p") | ||
|
||
const canvas = document.createElement('canvas') | ||
const ctx = canvas.getContext('2d') | ||
|
||
let model | ||
|
||
main() | ||
|
||
async function main () { | ||
status.innerText = "Model loading..." | ||
model = await mobilenet.load() | ||
status.innerText = "Model is loaded!" | ||
|
||
const stream = await navigator.mediaDevices.getUserMedia({ video: true }) | ||
video.srcObject = stream | ||
await video.play() | ||
|
||
canvas.width = video.videoWidth | ||
canvas.height = video.videoHeight | ||
|
||
refresh() | ||
} | ||
|
||
async function refresh(){ | ||
ctx.drawImage(video, 0,0) | ||
image.src = canvas.toDataURL('image/png') | ||
|
||
await model.load() | ||
const predictions = await model.classify(image) | ||
|
||
const className = predictions[0].className | ||
const percentage = Math.floor(100 * predictions[0].probability) | ||
|
||
status.innerHTML = percentage + '%' + ' ' + className | ||
|
||
setTimeout(refresh, 100) | ||
} | ||
|
||
</script> | ||
|
||
</html> |
38 changes: 38 additions & 0 deletions
38
source/_static/code/zh/deployment/javascript/regression.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<html> | ||
<head> | ||
<script src="http://unpkg.com/@tensorflow/tfjs/dist/tf.min.js"></script> | ||
<script> | ||
const xsRaw = tf.tensor([2013, 2014, 2015, 2016, 2017]) | ||
const ysRaw = tf.tensor([12000, 14000, 15000, 16500, 17500]) | ||
|
||
// 归一化 | ||
const xs = xsRaw.sub(xsRaw.min()) | ||
.div(xsRaw.max().sub(xsRaw.min())) | ||
const ys = ysRaw.sub(ysRaw.min()) | ||
.div(ysRaw.max().sub(ysRaw.min())) | ||
|
||
const a = tf.scalar(Math.random()).variable() | ||
const b = tf.scalar(Math.random()).variable() | ||
|
||
// y = a * x + b. | ||
const f = (x) => a.mul(x).add(b) | ||
const loss = (pred, label) => pred.sub(label).square().mean() | ||
|
||
const learningRate = 1e-3 | ||
const optimizer = tf.train.sgd(learningRate) | ||
|
||
// 训练模型 | ||
for (let i = 0; i < 10000; i++) { | ||
optimizer.minimize(() => loss(f(xs), ys)) | ||
} | ||
|
||
// 预测 | ||
console.log(`a: ${a.dataSync()}, b: ${b.dataSync()}`) | ||
const preds = f(xs).dataSync() | ||
const trues = ys.arraySync() | ||
preds.forEach((pred, i) => { | ||
console.log(`x: ${i}, pred: ${pred.toFixed(2)}, true: ${trues[i].toFixed(2)}`) | ||
}) | ||
</script> | ||
</head> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.