{"id":38793,"date":"2022-12-26T15:48:32","date_gmt":"2022-12-26T15:48:32","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38793"},"modified":"2023-01-28T15:08:17","modified_gmt":"2023-01-28T15:08:17","slug":"numpy-tensordot","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-tensordot","title":{"rendered":"Numpy.tensordot() &#8211; How to Calculate Tensordot Product Using Numpy?"},"content":{"rendered":"\n<p>This article will help you to understand how to calculate a tensor dot product using the <em>tensordot( ) <\/em>function from the <em>numpy <\/em>library. Once a pair of tensors whose dot product is to be found are fed as inputs in the form of arrays, the <em>tensordot( ) <\/em>function sums the products of a\u2019s and b\u2019s elements over the axes specified.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-trace\" data-type=\"post\" data-id=\"38760\">Numpy Trace: A Guide to Calculating Trace Using Numpy in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p>Let\u2019s get started in understanding this function by first importing the&nbsp;<em>numpy&nbsp;<\/em>library using the below code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>We shall further explore the&nbsp;<em>tensordot( )&nbsp;<\/em>function through each of the following sections.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of&nbsp;<em>tensordot( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Calculating Tensor Dot Product<\/strong><\/li>\n\n\n\n<li><strong>Similarities &#038; Differences with <em>numpy.dot( )<\/em><\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of\u00a0tensordot( )\u00a0function<\/h2>\n\n\n\n<p>The following are the constructs that are required for the effective functioning of the <em>tensordot( ) <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.tensordot(a, b, axes)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>a \u2013&nbsp;<\/em><\/strong>array-like object which serves as the first tensor<\/li>\n\n\n\n<li><strong><em>b \u2013<\/em><\/strong>&nbsp;array-like object which serves as the second tensor<\/li>\n\n\n\n<li><strong><em>axes \u2013 <\/em><\/strong>can be a scalar or array-like object which specifies the axes along which the dot product is to be calculated<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Calculating Tensor Dot Product<\/h2>\n\n\n\n<p>In this section let us find the tensor dot product of a pair of tensors given in the form of N-dimensional arrays as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;1, 3, 7], &#x5B;2, 9, 4]])\nar2 = np.array(&#x5B;&#x5B;5, 6], &#x5B;8, 0], &#x5B;1, 7]])\n<\/pre><\/div>\n\n\n<p>Now let us deploy the <em>tensordot( ) <\/em>function specifying the direction of axes as \u20181\u2019.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.tensordot(ar1, ar2, axes=1)\n<\/pre><\/div>\n\n\n<p>Once the above code is run, the following computation happens in the back end for calculating the results which shall be returned in the form of an array.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first element from the first row of the first tensor is multiplied by the first element of the first column of the second tensor (i.e) 1&#215;5 = 5.<\/li>\n\n\n\n<li>The second element from the first row of the first tensor is multiplied by the second element of the first column of the second tensor (i.e) 3&#215;8 = 24.<\/li>\n\n\n\n<li>The same happens with the subsequent third elements in the first &#038; the second tensor too (i.e) 7&#215;1 = 7. To put things in perspective here are the inputs printed for the readers to comprehend better.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"164\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Input-Tensors-Printed-as-Arrays.jpg\" alt=\"Input Tensors Printed As Arrays\" class=\"wp-image-38802\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Input-Tensors-Printed-as-Arrays.jpg 378w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Input-Tensors-Printed-as-Arrays-300x130.jpg 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><figcaption class=\"wp-element-caption\">Input Tensors Printed As Arrays<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Once done, the product of all the elements deduced above are summed up to obtain the first element of the first row of the tensor dot product (i.e) 5+24+7 = 36.<\/li>\n\n\n\n<li>The same process is iterated with the subsequent rows and columns of the input tensors to find the other elements of the tensor dot product.\n<ul class=\"wp-block-list\">\n<li>The second element of the first row of the tensor dot product is retrieved from (1&#215;6) + (3&#215;0) + (7&#215;7) = 55<\/li>\n\n\n\n<li>The first element of the second row of the tensor dot product is retrieved from (2&#215;5) + (9&#215;8) + (4&#215;1) = 86<\/li>\n\n\n\n<li>The second element of the second row of the tensor dot product is retrieved from (2&#215;6) + (9&#215;0) + (4&#215;7) = 40<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Following is the result that we get after the code is run and one can find the striking similarity between the element-wise deductions detailed above with that which has been displayed below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"83\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes1.jpg\" alt=\"Tensor Dot Product Calculated With Axes1\" class=\"wp-image-38805\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes1.jpg 372w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes1-300x67.jpg 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><figcaption class=\"wp-element-caption\">Tensor Dot Product Calculated With Axes=1<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Similarities &#038; Differences with numpy.dot( )<\/h2>\n\n\n\n<p>When the same inputs are run through the <em>numpy.dot( ) <\/em>function the results obtained can bear an uncanny resemblance.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.dot(ar1, ar2)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"186\" height=\"49\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Results-of-numpy.dot-.jpg\" alt=\"Results Of Numpy Dot \" class=\"wp-image-38808\"\/><figcaption class=\"wp-element-caption\">Results Of <em>numpy.dot( )<\/em><\/figcaption><\/figure>\n\n\n\n<p>This is because setting the axes to \u20181\u2019 while running a <em>tensordot( ) <\/em>function results in a conventional matrix dot product, which explains the above similarity. However, changing the axes setting to, say zero (0) for instance, would dictate the direction in which the <em>tensordot( ) <\/em>needs to compute yielding a different result.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.tensordot(ar1, ar2, axes=0)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"402\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes0.jpg\" alt=\"Tensor Dot Product Calculated With Axes0\" class=\"wp-image-38811\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes0.jpg 318w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Tensor-Dot-Product-Calculated-with-Axes0-237x300.jpg 237w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><figcaption class=\"wp-element-caption\">Tensor Dot Product Calculated With Axes=0<\/figcaption><\/figure>\n\n\n\n<p>The underlying logic behind the computation of the above result is multiplying the entire array \u2018ar2\u2019 with each element of \u2018ar1\u2019. The very first sub-array is a result of multiplying \u2018ar2\u2019 with the first element of \u2018ar1\u2019 ~ 1!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on how to use the&nbsp;<em>tensordot( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library to calculate the tensordot product for the given pair of tensors. Here\u2019s another article that explains the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-trace\" target=\"_blank\" type=\"URL\" id=\"https:\/\/www.askpython.com\/python\/how-to-use-numpy-trace-in-python\" rel=\"noreferrer noopener\"><em>trace<\/em>( ) function from&nbsp;<em>numpy&nbsp;<\/em>in Python<\/a>. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are looking to level up in Python. Whilst you enjoy those,&nbsp;<em>hasta luego<\/em>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will help you to understand how to calculate a tensor dot product using the tensordot( ) function from the numpy library. Once a pair of tensors whose dot product is to be found are fed as inputs in the form of arrays, the tensordot( ) function sums the products of a\u2019s and b\u2019s [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":38794,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38793"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38794"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}