TensorFlow.js快速入门

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/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393872.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mountain Number FZU-2109数位dp

Mountain NumberFZU-2109 题目大意:一个大于0的数字x,分写成xa[0]a[1]a[2][3]..a[n]的形式,(比如x1234,a[0]1,a[1]2,a[3]3,a[3]4),Mountain Number要满足对于a[2*i1]要大于等于a[2*i]和a[2*i2],给定范围l,r…

[10.5模拟] dis

题意:给你一个主串,两个分串,要求两个分串的距离最大,两个分串的距离定义为第一个分串的最右边的字符和第二个分串的最左边的字符之间的字符数 题解: 直接kmp匹配两个分串即可 注:kmp匹配时,当分…

什么是非集计模型_集计与非集计模型的关系

集计与非集计模型的关系Wardrop第一.第二平衡原理集计模型在传统的交通规划或交通需求预测中,通常首先将对象地区或群体划分为若干个小区或群体等特定的集合体,然后以这些小区或群体为基本单位,展开问题的讨论。因此,在建立模型或…

微软dns能做cname吗_为什么域的根不能是CNAME以及有关DNS的其他花絮

微软dns能做cname吗This post will use the above question to explore DNS, dig, A records, CNAME records, and ALIAS/ANAME records from a beginner’s perspective. So let’s get started.这篇文章将使用上述问题从初学者的角度探讨DNS , dig , A…

Java Timestamp Memo

timestamp的构造函数,把微妙作为纳秒存储,所以 Java.util.date.comepareTo(Timestamp) 结果肯定是1另外,​Timestamp.equal(object) 如果参数不是Timestamp,肯定返回false。Timestamps nanos value is NOT the number of nanoseco…

oracle虚拟机字符集,更改虚拟机上的oracle字符集

修改oracle上边的字符集,需要用到DBA数据库管理员的权限,再修改字符集时要注意到修改后的字符集只能范围变大(例如:当前的字符集是GBK,那你修改后可以是UTF-8就是说后者只能比前者大,不能小.因为字符集都是向下兼容的)步骤:第一步:使用DBA身份登录先以绕过日志的方式登录在以然…

mybaits自连接查询

看不太懂,先记录再查,有没有大大解释下 resultmap里的collection设置select字段,看着像递归,没见过这种用法,#{pid}从何而来? 转载于:https://www.cnblogs.com/haon/p/10808739.html

token要加编码decode吗_彻底弄明白Base64 编码

Base64 encoding/decoding常见于各种authentication和防盗链的实现当中。彻底搞懂它绝对提升团队troubleshooting的底气。我们从纯手工方式编码解码开始,然后看看学到的技能怎么样应用在实际的troubleshooting 中。准备工作:我们应知道一个byte有8个bits…

oracle的oradata,Oracle使用oradata恢复数据库

SQL> host del D:\oracle\ora92\database\PWDoracle.ORASQL> host orapwd fileD:\oracle\ora92\DATABASE\PWDoracle.ORA passwordsystem entries10SQL> alter database open;数据库已更改。SQL> conn system/system as sysdba已连接。SQL> shutdown immediate数…

Jenkins连接TFS出现错误:“jenkins com.microsoft.tfs.core.exceptions.TECoreException”的问题收集...

没成功解决过,下面提供一些收集的链接地址,因为这个问题真的很少。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/1a75a0b2-4591-4edd-999a-9696149c8144/integration-with-jenkins?forumtfsintegration http://www.itgo.me/a/900879197026…

leetcode842. 将数组拆分成斐波那契序列(回溯)

给定一个数字字符串 S&#xff0c;比如 S “123456579”&#xff0c;我们可以将它分成斐波那契式的序列 [123, 456, 579]。 形式上&#xff0c;斐波那契式序列是一个非负整数列表 F&#xff0c;且满足&#xff1a; 0 < F[i] < 2^31 - 1&#xff0c;&#xff08;也就是…

react fiber_让我们爱上React Fiber

react fiberby Ryan Yurkanin瑞安尤卡宁(Ryan Yurkanin) 让我们爱上React Fiber (Let’s fall in love with React Fiber) TLDR, React Fiber is an internal engine change that allows React to break the limits of the call stack. It’s creation enables React to pause…

Ajax爬取豆瓣电影目录(Python)

下面的分析相当于一个框架&#xff0c;搞懂之后&#xff0c;对于类似的文字爬取&#xff0c;我们也可以实现。就算不能使用Ajax方法&#xff0c;我们也能够使用相同思想去爬取我们想要的数据。 豆瓣电影排行榜分析 网址&#xff1a;https://movie.douban.com/explore#!typemovi…

到底死不死我就请了七天假_“你到底死不死?我只请了7天假”

这两天看到一条令人心酸的新闻&#xff0c;在国内某地铁站内&#xff0c;一位57岁的大妈突发心脏病&#xff0c;被紧急救醒后&#xff0c;第一句话竟是请求工作人员不要打电话通知她远在德国的儿子。看完这条新闻&#xff0c;掌柜特别心酸&#xff0c;孤身一人在国内&#xff0…

正面管教PHP沙龙,正面管教沙龙体会

接触到正面管教这个理念是我们南宁行动派伙伴圈 的圈主西西给大家带来的分享&#xff0c;谢谢西西[爱你]图片发自简书App同时也很感谢亲切温柔&#xff0c;知性优雅的Liliane老师&#xff0c;让我明白表扬和鼓励的区别&#xff0c;非暴力教育……教书育人这个道路上我需要学习的…

FB面经Prepare: Dot Product

Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间&#xff0c;如果一个很大&#xff0c;一个很小&#xff0c;适合scan小的&#xff0c;并且在大的里面做binary search 1 package fb;2 3 public class DotProduct {4 5 publi…

leetcode1291. 顺次数(回溯)

我们定义「顺次数」为&#xff1a;每一位上的数字都比前一位上的数字大 1 的整数。 请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表&#xff08;从小到大排序&#xff09;。 示例 1&#xff1a; 输出&#xff1a;low 100, high 300 输出&#xff1a;[123,234] …

20175223 MySQL

目录 完成结果要求 1 &#xff1a;导入world.sql要求 2 &#xff1a;CityWanna.javaCityWanna.java要求 3 &#xff1a;CountryWanna.javaCountryWanna.java要求 4 &#xff1a;LifeWanna.javaLifeWanna.java过程中问题及解决1. XAMPP无法启用 MySQL 程序。目录 完成结果 要求 …

2020运动相机推荐_2020年超有价值入门级微单相机推荐,超高性价比几款入门级微单相机(选购指南)...

学习摄影专业已经3年多啦&#xff0c;自己喜欢拍摄照片&#xff0c;自己还帮助过一些想学习摄影的朋友快速入门&#xff0c;最近发现周围学习摄影的朋友也越来越多了&#xff0c;有一些朋友咨询关于入门微单相机的问题&#xff0c;想让推荐几款不错的入门的微单相机。这篇文章带…

javascript入门_JavaScript代理快速入门

javascript入门What is a JavaScript proxy? you might ask. It is one of the features that shipped with ES6. Sadly, it seems not to be widely used.什么是JavaScript代理&#xff1f; 你可能会问。 这是ES6附带的功能之一。 可悲的是&#xff0c;它似乎并未得到广泛使用…