Getting Started with TensorFlow.js
使用TensorFlow.js,您不仅可以在浏览器中运行深度学习模型进行推理,你还能够训练它们。在这个简单的样例中,将展示一个相当于“Hello World”的示例。
1、引入TensorFlow.js
使用CDN上的文件,你就可以使用TensorFlow APIs。
<html>
<head>
<!-- Load TensorFlow.js -->
<!-- Get latest version at https://github.com/tensorflow/tfjs -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"> </script>
这里使用的版本是 0.11.2,你可以去github上找最新的。
2、创建一个简单的神经网络
由于这只有一个输入值和输出值,因此它可以是单个节点。在JavaScript中,我们可以创建一个tf.sequential,并向其添加layers。
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
我们还需要指定损失函数和优化器:
model.compile({
loss: 'meanSquaredError',
optimizer: 'sgd'
});
为了训练模型,我们需要数据集来训练模型。构造几个符合Y=2X-1的点,那么当X取[-1, 0, 1, 2, 3],Y取[-3, -1, 1, 3, 5]
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
调用fit函数进行训练,传入X和Y并指定训练轮数。请注意,只是异步的,在进入下一步之前必须等待返回值,所以这些代码都要写在一个async函数中。
await model.fit(xs, ys, {epochs: 500});
最后传入一个值进行预测。
完整的代码如下:
<html> <head>
<!-- Load TensorFlow.js -->
<!-- Get latest version at https://github.com/tensorflow/tfjs -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2">
</script>
</head> <body>
<div id="output_field"></div>
</body> <script>
async function learnLinear(){ const model = tf.sequential();
model.add(tf.layers.dense({
units: 1,
inputShape: [1]
})); model.compile({
loss: 'meanSquaredError',
optimizer: 'sgd'
}); const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]); await model.fit(xs, ys, {epochs: 100}); document.getElementById('output_field').innerText =
model.predict( tf.tensor2d([10], [1, 1]) );
} learnLinear();
</script> <html>
参考链接:
1. https://medium.com/tensorflow/getting-started-with-tensorflow-js-50f6783489b2
2.https://blog.csdn.net/aliceyangxi1987/article/details/80743590
Getting Started with TensorFlow.js的更多相关文章
- TensorFlow.js入门(一)一维向量的学习
TensorFlow的介绍 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着 ...
- 转《在浏览器中使用tensorflow.js进行人脸识别的JavaScript API》
作者 | Vincent Mühle 编译 | 姗姗 出品 | 人工智能头条(公众号ID:AI_Thinker) [导读]随着深度学习方法的应用,浏览器调用人脸识别技术已经得到了更广泛的应用与提升.在 ...
- TensorFlow.js之根据数据拟合曲线
这篇文章中,我们将使用TensorFlow.js来根据数据拟合曲线.即使用多项式产生数据然后再改变其中某些数据(点),然后我们会训练模型来找到用于产生这些数据的多项式的系数.简单的说,就是给一些在二维 ...
- TensorFlow.js之安装与核心概念
TensorFlow.js是通过WebGL加速.基于浏览器的机器学习js框架.通过tensorflow.js,我们可以在浏览器中开发机器学习.运行现有的模型或者重新训练现有的模型. 一.安装 ...
- 大前端技术系列:TWA技术+TensorFlow.js => 集成原生和AI功能的app
大前端技术系列:TWA技术+TensorFlow.js => 集成原生和AI功能的app ( 本文内容为melodyWxy原作,git地址:https://github.com/melodyWx ...
- TensorFlow.js入门:一维向量的学习
转载自:https://blog.csdn.net/weixin_34061042/article/details/89700664 一维向量及其运算 tensor 是 TensorFlow.js 的 ...
- [ML] Tensorflow.js + Image segmentPerson
<!DOCTYPE html> <html> <head> <title>Parcel Sandbox</title> <meta c ...
- 【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具
示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 目录 一. 上手TensorFlow.js 二. ...
- 在浏览器中进行深度学习:TensorFlow.js (八)生成对抗网络 (GAN
Generative Adversarial Network 是深度学习中非常有趣的一种方法.GAN最早源自Ian Goodfellow的这篇论文.LeCun对GAN给出了极高的评价: “There ...
随机推荐
- Paper | Noise2Noise: Learning Image Restoration without Clean Data
目录 故事背景 算法原理 点估计 神经网络算法与点估计的关系 核心思想 回头品味 实验 高斯 其他生成噪声 发表在2018 ICML. 摘要 We apply basic statistical re ...
- strcspn()函数
函数描述: 检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符. 函数声明: #include<string.h> size_t strcspn(const char ...
- vue 路由跳转到本页面,ts 监听路由变化
@Watch('$route') routechange(to: any, from: any) { //参数不相等 if (to.query.name!=from.query.name) { //t ...
- guava(四)区间Ranges
一.构建区间 (a..b) open(C, C) [a..b] closed(C, C) [a..b) closedOpen(C, C) (a..b] openClosed(C, C) (a..+∞) ...
- DFS(三):八皇后问题
[例1]八皇后问题. 在一个8×8国际象棋盘上,放置8个皇后,每个皇后占一格,要求皇后间不会出现相互“攻击”的现象,即不能有两个皇后处在同一行.同一列或同一对角线上.问共有多少种不同的放置方法? (1 ...
- Gin框架 - 使用 Logrus 进行日志记录
概述 上篇文章分享了 Gin 框架的路由配置,这篇文章分享日志记录. 查了很多资料,Go 的日志记录用的最多的还是 github.com/sirupsen/logrus. Logrus is a st ...
- 我在生产项目里是如何使用Redis发布订阅的?(一)使用场景
转载请注明出处! 导语 Redis是我们很常用的一款nosql数据库产品,我们通常会用Redis来配合关系型数据库一起使用,弥补关系型数据库的不足. 其中,Redis的发布订阅功能也是它的一大亮点.虽 ...
- Date以及LocalDateTime格式化
public static void main(String[] args) { LocalDateTime local = LocalDateTime.now(); Date date = new ...
- 也作一下装配脑袋的Expression习题【转】
一.习题 http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html 二.参考 http://msdn.micro ...
- 修改linux内核加载顺序
修改内核启动顺序:1.查看当前系统所有的内核# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/gr ...