<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 用 NumPy 輕松介紹機器學習的張量 > 原文: [https://machinelearningmastery.com/introduction-to-tensors-for-machine-learning/](https://machinelearningmastery.com/introduction-to-tensors-for-machine-learning/) 在深度學習中,通常會將圍繞張量的大量討論視為基礎數據結構。 Tensor 甚至出現在谷歌旗艦機器學習庫的名稱中:“TensorFlow”。 張量是線性代數中使用的一種數據結構,與向量和矩陣一樣,您可以使用張量計算算術運算。 在本教程中,您將了解哪些張量以及如何使用 NumPy 在 Python 中對它們進行操作 完成本教程后,您將了解: * 這些張量是矩陣的推廣,并使用 n 維數組表示。 * 如何使用張量實現元素操作。 * 如何執行張量產品。 讓我們開始吧。 ![A Gentle Introduction to Tensors for Machine Learning with NumPy](img/11c5b1d90e311f196e22db2365206864.jpg) 使用 NumPy 輕松介紹機器學習的張量 照片由[DanielTombra?aGonzález](https://www.flickr.com/photos/teleyinex/10074893905/)拍攝,保留一些權利。 ## 教程概述 本教程分為 3 個部分;他們是: 1. 什么是張量? 2. Python 中的張量 3. 元素智能張量操作 4. 張量產品 ## 什么是張量? 張量是向量和矩陣的推廣,很容易理解為多維數組。 > 在一般情況下,布置在具有可變軸數的規則網格上的數字陣列被稱為張量。 - 第 33 頁,[深度學習](http://amzn.to/2B3MsuU),2016 年。 向量是一維或一階張量,矩陣是二維或二階張量。 張量表示法很像矩陣表示法,大寫字母表示張量和小寫字母,下標整數表示張量中的標量值。 ``` t111, t121, t131 t112, t122, t132 t113, t123, t133 T = (t211, t221, t231), (t212, t222, t232), (t213, t223, t233) t311, t321, t331 t312, t322, t332 t313, t323, t333 ``` 可以使用標量,向量和矩陣執行的許多操作可以重新表示為使用張量執行。 作為一種工具,張量和張量代數廣泛應用于物理和工程領域。在深度學習模型的訓練和操作中,機器學習中已知的術語和一組技術可以用張量來描述。 ## Python 中的張量 與向量和矩陣一樣,張量可以使用 N 維數組(ndarray)在 Python 中表示。 張量可以作為列表列表與 array()的構造函數內聯定義。 下面的示例將 3x3x3 張量定義為 NumPy ndarray。三個尺寸更容易包裹你的頭。在這里,我們首先定義行,然后是作為列堆疊的行列表,然后是在多維數據集中作為級別堆疊的列的列表。 ``` # create tensor from numpy import array T = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) print(T.shape) print(T) ``` 首先運行示例打印張量的形狀,然后打印張量本身的值。 您可以看到,至少在三維空間中,張量被打印為一系列矩陣,每層一個矩陣。對于此 3D 張量,軸 0 指定級別,軸 1 指定列,軸 2 指定行。 ``` (3, 3, 3) [[[ 1 2 3] [ 4 5 6] [ 7 8 9]] [[11 12 13] [14 15 16] [17 18 19]] [[21 22 23] [24 25 26] [27 28 29]]] ``` ## 元素智能張量操作 與矩陣一樣,我們可以在張量之間執行逐元素算術。 在本節中,我們將完成四個主要的算術運算。 ### Tensor Addition 具有相同尺寸的兩個張量的元素相加得到具有相同尺寸的新張量,其中每個標量值是父張量中標量的元素加法。 ``` a111, a121, a131 a112, a122, a132 A = (a211, a221, a231), (a112, a122, a132) b111, b121, t131 b112, b122, b132 B = (b211, t221, t231), (b112, b122, b132) C = A + B a111 + b111, a121 + b121, a131 + b131 a112 + b112, a122 + b122, a132 + b132 C = (a211 + b211, a221 + b221, a231 + b231), (a112 + b112, a122 + b122, a132 + b132) ``` 在 NumPy 中,我們可以通過添加數組直接添加張量。 ``` # tensor addition from numpy import array A = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) B = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) C = A + B print(C) ``` 運行該示例將打印兩個父張量的添加。 ``` [[[ 2 4 6] [ 8 10 12] [14 16 18]] [[22 24 26] [28 30 32] [34 36 38]] [[42 44 46] [48 50 52] [54 56 58]]] ``` ### 張量減法 從具有相同尺寸的另一張量的一個張量的逐元素減法導致具有相同維度的新張量,其中每個標量值是父張量中標量的逐元素減法。 ``` a111, a121, a131 a112, a122, a132 A = (a211, a221, a231), (a112, a122, a132) b111, b121, t131 b112, b122, b132 B = (b211, t221, t231), (b112, b122, b132) C = A - B a111 - b111, a121 - b121, a131 - b131 a112 - b112, a122 - b122, a132 - b132 C = (a211 - b211, a221 - b221, a231 - b231), (a112 - b112, a122 - b122, a132 - b132) ``` 在 NumPy 中,我們可以通過減去數組直接減去張量。 ``` # tensor subtraction from numpy import array A = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) B = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) C = A - B print(C) ``` 運行該示例將打印從第二個減去第一個張量的結果。 ``` [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] ``` ### Tensor Hadamard 產品 一個張量與具有相同維度的另一張量的元素乘法導致具有相同維度的新張量,其中每個標量值是父張量中標量的元素乘法。 與矩陣一樣,該操作被稱為 Hadamard 乘積,以區別于張量乘法。在這里,我們將使用“o”運算符來指示張量之間的 Hadamard 產品操作。 ``` a111, a121, a131 a112, a122, a132 A = (a211, a221, a231), (a112, a122, a132) b111, b121, t131 b112, b122, b132 B = (b211, t221, t231), (b112, b122, b132) C = A o B a111 * b111, a121 * b121, a131 * b131 a112 * b112, a122 * b122, a132 * b132 C = (a211 * b211, a221 * b221, a231 * b231), (a112 * b112, a122 * b122, a132 * b132) ``` 在 NumPy 中,我們可以通過乘以數組直接乘以張量。 ``` # tensor Hadamard product from numpy import array A = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) B = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) C = A * B print(C) ``` 運行該示例將打印倍增張量的結果。 ``` [[[ 1 4 9] [ 16 25 36] [ 49 64 81]] [[121 144 169] [196 225 256] [289 324 361]] [[441 484 529] [576 625 676] [729 784 841]]] ``` ### Tensor Division 一個張量的元素劃分與具有相同維度的另一個張量導致具有相同維度的新張量,其中每個標量值是父張量中標量的元素劃分。 ``` a111, a121, a131 a112, a122, a132 A = (a211, a221, a231), (a112, a122, a132) b111, b121, t131 b112, b122, b132 B = (b211, t221, t231), (b112, b122, b132) C = A / B a111 / b111, a121 / b121, a131 / b131 a112 / b112, a122 / b122, a132 / b132 C = (a211 / b211, a221 / b221, a231 / b231), (a112 / b112, a122 / b122, a132 / b132) ``` 在 NumPy 中,我們可以通過劃分數組直接劃分張量。 ``` # tensor division from numpy import array A = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) B = array([ [[1,2,3], [4,5,6], [7,8,9]], [[11,12,13], [14,15,16], [17,18,19]], [[21,22,23], [24,25,26], [27,28,29]], ]) C = A / B print(C) ``` 運行該示例將打印分割張量的結果。 ``` [[[ 1\. 1\. 1.] [ 1\. 1\. 1.] [ 1\. 1\. 1.]] [[ 1\. 1\. 1.] [ 1\. 1\. 1.] [ 1\. 1\. 1.]] [[ 1\. 1\. 1.] [ 1\. 1\. 1.] [ 1\. 1\. 1.]]] ``` ## 張量產品 張量積算子通常表示為中間帶有小 x 的圓。我們在這里將其表示為“(x)”。 給定具有 q 維度的張量 A 和具有 r 維度的張量 B,這些張量的乘積將是具有 q + r 的量級的新張量,或者換言之,q + r 維度。 張量積不限于張量,也可以在矩陣和向量上執行,這可以是練習的好地方,以便開發更高維度的直覺。 我們來看看向量的張量積。 ``` a = (a1, a2) b = (b1, b2) c = a (x) b a1 * [b1, b2] c = (a2 * [b1, b2]) ``` 或者,展開: ``` a1 * b1, a1 * b2 c = (a2 * b1, a2 * b2) ``` 我們來看看矩陣的張量積。 ``` a11, a12 A = (a21, a22) b11, b12 B = (b21, b22) C = A (x) B b11, b12 b11, b12 a11 * (b21, b22), a12 * (b21, b22) C = [ b11, b12 b11, b12 ] a21 * (b21, b22), a22 * (b21, b22) ``` Or, unrolled: ``` a11 * b11, a11 * b12, a12 * b11, a12 * b12 a11 * b21, a11 * b22, a12 * b21, a12 * b22 C = (a21 * b11, a21 * b12, a22 * b11, a22 * b12) a21 * b21, a21 * b22, a22 * b21, a22 * b22 ``` 張量積可以使用 tensordot()函數在 NumPy 中實現。 該函數將兩個要乘以的張量作為參數,并將產品總和的軸作為參數,稱為總和減少。要計算張量積,也稱為 NumPy 中的張量點積,軸必須設置為 0。 在下面的示例中,我們定義了兩個 ord??er-1 張量(向量)并計算張量積。 ``` # tensor product from numpy import array from numpy import tensordot A = array([1,2]) B = array([3,4]) C = tensordot(A, B, axes=0) print(C) ``` 運行該示例將打印張量積的結果。 結果是 2 階張量(矩陣),長度為 2×2。 ``` [[3 4] [6 8]] ``` 張量積是您可能遇到的最常見的張量乘法形式,但存在許多其他類型的張量乘法,例如張量點乘積和張量收縮。 ## 擴展 本節列出了一些擴展您可能希望探索的教程的想法。 * 使用您自己的小設計張量數據更新每個示例。 * 使用小向量或矩陣數據實現本教程未涉及的其他三種張量乘法。 * 編寫自己的函數來實現每個張量操作。 如果你探索任何這些擴展,我很想知道。 ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 ### 圖書 * [學生向量和張量指南](http://amzn.to/2kmUvvF),2011。 * 第 12 章,專題,[矩陣計算](http://amzn.to/2B9xnLD),2012。 * [Tensor Algebra 和 Tensor Analysis for Engineers](http://amzn.to/2C6gzCu) ,2015。 ### API * [N 維數組](https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.ndarray.html) * [numpy.tensordot()API](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.tensordot.html) ### 用品 * [維基百科上的張量代數](https://en.wikipedia.org/wiki/Tensor_algebra) * [維基百科上的張量](https://en.wikipedia.org/wiki/Tensor) * 維基百科上的 [Tensor 產品](https://en.wikipedia.org/wiki/Tensor_product) * [維基百科上的外部產品](https://en.wikipedia.org/wiki/Outer_product) ### 其他 * [Tensor 訓練格式中大規模數據分析的基本張量操作](https://arxiv.org/abs/1405.7786),2016。 * 張量積,直接和,[量子力學 I](http://hitoshi.berkeley.edu/221a/tensorproduct.pdf) ,2006。 * [Tensorphobia 和外部產品](https://jeremykun.com/2016/03/28/tensorphobia-outer-product/),2016 年。 * [Tensor 產品](https://jmanton.wordpress.com/2011/10/12/the-tensor-product/),2011 ## 摘要 在本教程中,您了解了哪些張量以及如何使用 NumPy 在 Python 中操作它們。 具體來說,你學到了: * 這些張量是矩陣的推廣,并使用 n 維數組表示。 * 如何使用張量實現元素操作。 * 如何執行張量產品。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看