by Pau Pavón

通过保罗·帕文(PauPavón)

TensorFlow.js快速入门 (A quick introduction to TensorFlow.js)

TensorFlow has been around for a while now. Until last month, though, it was only available for Python and a few other programming languages, like C and Java. And you might think those would be more than enough.

TensorFlow已经存在了一段时间。 但是直到上个月,它仅适用于Python和其他一些编程语言,例如C和Java。 您可能会认为这些绰绰有余。

I had heard of TensorFlow in the past. Even though I didn’t know anything about machine or deep learning, I was pretty sure it was one of the most used frameworks for those purposes. I had seen lots of cool things done with it: object detection in images, speech recognition, and even music composition!

我过去曾听说过TensorFlow。 即使我对机器或深度学习一无所知,但我很确定它是用于这些目的的最常用的框架之一。 我看到了很多很棒的东西:图像中的对象检测,语音识别甚至音乐创作!

Imagine being able to do all these cool ML things in the browser, without installing any libraries and having to compile all those lines of code again and again. Well, that’s what TensorFlow.js has come to do.

想象一下,能够在浏览器中完成所有这些很酷的ML事情,而无需安装任何库,而不必一次又一次地编译所有这些代码行。 好吧,这就是TensorFlow.js要做的事情。

If you want to learn more about how and why this amazing framework has come to life, you can check out TensorFlow here on Medium!

如果您想更多地了解这个惊人的框架是如何实现的以及为什么得以实现,可以在Medium上查看TensorFlow !

So as soon as I discovered this, I wanted to start learning how TensorFlow.js works. And that’s exactly what I’ve started to do: I’ve found a set of tutorials by The Coding Train on Youtube, which are still ongoing (in fact, they’ve just started) and I’ve began to mess around a bit with things.

因此,一旦我发现了这一点,便想开始学习TensorFlow.js的工作原理。 这就是我刚开始做的事情:我在YouTube上找到了The Coding Train的一组教程,这些教程仍在进行中(实际上,它们才刚刚开始),而且我开始变得有点混乱与事物。

I’d like to give you a quick introduction to TensorFlow (TF) so you can follow me along my journey and learn with me.

我想给您快速介绍TensorFlow(TF),以便您跟随我的旅程并与我一起学习。

TensorFlow.js的基础 (The basics of TensorFlow.js)

Let’s get started! First of all, you should know that all the documentation is on TF’s website, under the API Reference section.

让我们开始吧! 首先,您应该知道所有文档都在TF的网站上的API Reference部分中。

But wait, why is it called TensorFlow? What even is a tensor?

但是等等,为什么叫TensorFlow? 张量甚至是什么?

Glad you asked. A tensor is basically a structure of numbers. In math, there are different ways of representing numbers. You can have just the number itself, a vector, a matrix, and so on. A tensor is just a general term for all these different representations of data.

很高兴你问。 张量基本上是数字的结构。 在数学中,有多种表示数字的方式。 您可以只包含数字本身,向量,矩阵等。 张量只是所有这些数据表示形式的总称。

In TF, tensors are differentiated because of their rank, or, in other words, the number of dimensions they have.

在TF中,张量因其等级 (即它们具有的数)而有所区别。

These are the most common ones:

这些是最常见的:

标量(等级0) (Scalar (rank-0))

Just a number. This is how you can create and console-log one:

只是一个数字。 这是创建和登录控制台的方法:

tf.scalar(4.5).print();

And the output is the following:

输出如下:

Tensor  4.5

Tensor1d,tensor2d,tensor3d和ten​​sor4d(分别为1、2、3和4) (Tensor1d, tensor2d, tensor3d and tensor4d (rank- 1, 2, 3 and 4 respectively))

These are higher dimensional tensors. If you wanted to create a rank-1 tensor, for instance, you could simply do:

这些是高维张量。 例如,如果您想创建1级张量,则可以简单地执行以下操作:

tf.tensor1d([3, 7, 8]).print();

Which would output:

哪个会输出:

Tensor  [3, 7, 8]

张量(rank-n) (Tensor (rank-n))

If you don’t know the dimensions of your tensor, you can simply create one with the following function (notice how the above two examples work just as well with this other method):

如果您不知道张量的尺寸,则可以使用以下函数简单地创建一个张量(请注意,以上两个示例如何与其他方法一样工作):

tf.tensor(4.5).print();tf.tensor([3, 7, 8]).print();

This outputs exactly the same as before.

输出与以前完全相同。

You can, additionally, pass a couple more parameters to these functions.

另外,您可以将更多参数传递给这些函数。

tf.tensor(values,shape ?, dtype?) (tf.tensor(values, shape?, dtype?))

Let’s look at values first. This is the only compulsory parameter, and the only one we’ve been passing on in the previous examples. You can either pass a flat array of values (or even a single number in scalars) and specify the shape yourself, or you can pass a nested array.

首先让我们看一下 。 这是唯一的强制性参数,也是我们在前面的示例中传递的唯一参数。 您可以传递值的平面数组(甚至是标量中的单个数字)并自己指定形状,也可以传递嵌套数组。

Now you might be wondering what shape is. So, let’s say you want to output the following tensor:

现在您可能想知道什么形状 。 因此,假设您要输出以下张量:

[[1, 5], [4, 7]]

That is, you guessed right, a 2x2 matrix. You can either create this tensor by passing a flat array and specifying the shape as the second parameter of the function

也就是说,您猜对了,一个2x2矩阵。 您可以通过传递平面数组并将形状指定为函数的第二个参数来创建此张量

tf.tensor([1, 5, 4, 7], [2, 2]).print();

or by passing a nested array

或通过传递嵌套数组

tf.tensor([[1, 5], [4, 7]]).print();

Lastly, we have dtype. This specifies the data type. As for now, int32, float32 and bool are the three supported types.

最后,我们有dtype 。 这指定数据类型。 就目前而言, int32,float32bool是三种受支持的类型。

运作方式 (Operations)

But, what can you do with these tensors? Well, among other things, you can perform mathematical computation on them, such as arithmetic operations:

但是,您可以使用这些张量做什么? 好吧,除其他外,您可以对它们执行数学计算,例如算术运算:

Note: the following operations are performed element-wise, which means that every term of the first tensor involved is associated with the term in its same place in the other tensor.

注意 :以下操作是逐元素执行的,这意味着所涉及的第一个张量的每个项在另一个张量中的相同位置都与该项相关联。

const a = tf.tensor1d([4, 7, 2, 1]);const b = tf.tensor1d([20, 30, 40, 50]);

There are two ways these two can be added:

这两种添加方式有两种:

a.add(b).print();

or,

要么,

tf.add(a, b);

Both output:

两种输出:

Tensor  [24, 37, 42, 51]

Here’s how they work for subtraction,

他们是如何进行减法的,

tf.sub(a, b).print();
Tensor //output  [-16, -23, -38, -49]

multiplication,

乘法,

tf.mul(a, b).print();
Tensor //output  [80, 210, 80, 50]

and division:

和划分:

tf.div(a, b).print();
Tensor //output  [0.2, 0.2333333, 0.05, 0.02]

It’s pretty simple and straightforward.

这非常简单明了。

自己尝试! (Try it yourself!)

If you haven’t already, I encourage you to try the above yourself. This is the most basic stuff in TF, but these concepts are key in order to understand the more complex (and more fun) parts of it.

如果您还没有,我鼓励您自己尝试上述方法。 这是TF中最基本的内容,但是这些概念对于理解其中更复杂(更有趣)的部分至关重要。

Thanks for reading!

谢谢阅读!

Edit: Check out ADL’s Youtube playlist on TensorFlow, I’m sure it’ll help you out!

编辑:在TensorFlow上查看ADL的Youtube播放列表 ,我相信它会为您提供帮助!

翻译自: https://www.freecodecamp.org/news/a-quick-introduction-to-tensorflow-js-a046e2c3f1f2/

TensorFlow.js快速入门相关推荐

  1. 54 Node.js快速入门

    技术交流QQ群:1027579432,欢迎你的加入! 欢迎关注我的微信公众号:CurryCoder的程序人生 1.Node开发概述 1.1 为什么要学习服务器端开发基础 能够与后端程序员更加紧密的配合 ...

  2. (vue基础试炼_01)使用vue.js 快速入门hello world

    文章目录 一.需求案例 二.案例实现 2.1. 原始js写法 2.2. 怎样使用vue.js ? 2.3. 使用vue.js 写法 三.案例vue简述? 四.案例趣味延伸 五.表达值作用及嘱咐语 一. ...

  3. Vue.js快速入门之八:实现登录功能

    系统登录是指用户必须提供满足一定条件的信息后,才可以进入系统.最早系统一般是指用户名和密码,如今,登录方式已多元化,系统一般登录方式有:用户名+密码.二维码扫码登录.第三方授权登录.手机号+短信登录等 ...

  4. vue.js快速入门

    以前看到多少天学习系列,我都深感烦躁,短短几天怎么可以精通,那是对于他们而言.但是,今天我要写一个快速入门,本人觉得还是有一点用处,因为,我不可能在一天之内精通某种东西,却可以在一两小时入门. 回到v ...

  5. doodoo.js快速入门教程

    快速入门 我们通过3步演示如何快速创建一个doodoo项目 第一步 # 创建doodoo-demo目录 mkdir doodoo-demo && cd doodoo-demo# 初始化 ...

  6. doodoo.js快速入门教程 1

    快速入门 我们通过3步演示如何快速创建一个doodoo项目 第一步 # 创建doodoo-demo目录 mkdir doodoo-demo && cd doodoo-demo# 初始化 ...

  7. Three.js快速入门

    ThreeJS快速入门 ThreeJS开发 学习准备 安装 基本使用 效果图 控制器 物体移动 效果图 物体缩放与旋转 Clock跟踪时间 Gsap动画 自适应 Gui 效果图 BufferGeome ...

  8. html+css+js+快速入门

    html 1.a标签 <!--使用name作为标记--> <a name="top">顶部</a> <!--target:表示窗口在哪里打 ...

  9. Node.js快速入门

    一.简介 1.什么是Node.js 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎, ...

最新文章

  1. sqlite在c++中的使用方法
  2. css 设置table样式
  3. 扩展Ext2类 Extending Ext2 Class
  4. Codeforces 766E
  5. 日记20190416
  6. 用matlab仿真pwm整流器的方法,MATLAB-PWM整流仿真模块建立详解.pdf
  7. 如何利用.NET Core搭建跨平台的控制台应用程序
  8. Python TCP服务器
  9. Android开发,你应该知道的
  10. ⭐️ vue项目使用微信表情;vue引入微信表情emoji;vue中使用微信表情包emoji;
  11. C/C++的刷题练习之牛客网,一个友好的网站
  12. 织梦建站教程:文章列表隔行换色 隔5行横线
  13. mysql dual表用法_详解Oracle数据库中DUAL表的使用
  14. “竞速”智能网联汽车,领头雁为何是长沙?
  15. SimpleDateFormat类的使用
  16. ASP.NET Core 基础(十三)——模型绑定与模型验证
  17. Kafka(生产者)
  18. gdb函数相关7——选择函数堆栈帧
  19. 安卓手机安装charles证书后,抓包依然提示unkown问题(An unknown issue occurred processing the certificate )
  20. 使用 mysql_use_result 还是使用 mysql_store_result

热门文章

  1. 小程序自定义组件中observer函数的应用
  2. JDK 下载相关资料
  3. Tengine HTTPS原理解析、实践与调试【转】
  4. datagrid页面获取表单一条数据的例子
  5. ios7 苹果原生二维码扫描(和微信类似)
  6. Visual Studio无法查找或打开 PDB 文件解决办法
  7. ldconfig deferred processing now taking place
  8. 基于 Spring Cloud 完整的微服务架构实战
  9. 透明的WinForm窗体
  10. docker操作之mysql容器