---
title: tensordot
description: "沿着指定的轴计算张量点积。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1116.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_1116.html
indexId: 94ecc85eb518494db6fe538ad3fd413b0134b2bd17d244d86ea7ab9b03f16b0061
---
# tensordot

#### 功能描述

沿着指定的轴计算张量点积。

给定两个张量a和b，以及一个整数或包含两个array_like的对象（a_axes，b_axes），求a和b在a_axes和b_axes指定的轴上的乘积。


#### 必选输入参数

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| a | array\_like | 第一个张量。 |
| b | array\_like | 第二个张量。 |
| axes | int or (2,) array\_like | 如果为整数，则按顺序，对a的最后N个轴和b的前N个轴求和。相应轴的尺寸必须匹配。 如果为两个array，则为要求和的轴列表，第一个序列适用于a，第二个序列适用于b。两个元素array\_like必须具有相同的长度。元素array\_like里的元素代表序列a、b要删除的轴。 |


#### 可选输入参数

无。


#### 返回数据

| 类型 | 说明 |
| --- | --- |
| ndarray | 输入张量的点积。 |


#### 示例

```
>>> import numpy as np
>>> a = np.arange(60).reshape(3,4,5)
>>> b = np.arange(24).reshape(4,3,2)
>>> a
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]]])
>>> b
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15],
        [16, 17]],

       [[18, 19],
        [20, 21],
        [22, 23]]])
>>> c = np.tensordot(a, b, axes=([1,0],[0,1]))
>>> c
array([[4400, 4730],
       [4532, 4874],
       [4664, 5018],
       [4796, 5162],
       [4928, 5306]])
>>> c.shape
(5, 2)
>>>
>>> d = np.zeros((5,2))
>>> for i in range(5):
...     for j in range(2):
...         for k in range(3):
...             for n in range(4):
...                 d[i,j] += a[k,n,i] * b[n,k,j]
>>> c == d
array([[ True,  True],
       [ True,  True],
       [ True,  True],
       [ True,  True],
       [ True,  True]])
>>>
```
