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.pswp.cn/news/393872.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393872.shtml
英文地址,請注明出處:http://en.pswp.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;它似乎并未得到廣泛使用…