介绍
用于计算 两个一维张量点积(内积) 的核心函数,也是线性代数中最基础的运算之一,常出现在注意力机制、特征相似度计算等场景中。
功能
torch.dot(a, b) 计算两个一维张量的点积,数学定义为:
\text{dot}(a, b) = \sum_{i=0}^{n-1} a_i \times b_i
简单说就是:两个一维张量 对应位置元素相乘后求和,结果是一个标量(0维张量)。
语法
torch.dot(input, other, *, out=None)
| 参数 | 要求 & 说明 |
|---|---|
input |
一维张量(必须),数据类型需和other一致 |
other |
一维张量(必须),长度需和input完全相同 |
out |
可选,指定输出张量(需是0维张量),一般不用 |
例子
1. 基础用法(整数/浮点张量)
import torch
# 1. 一维整数张量点积
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
dot_result = torch.dot(a, b)
print("整数点积结果:", dot_result) # 1×4 + 2×5 + 3×6 = 4+10+18 = 32
print("结果维度:", dot_result.ndim) # 0(标量)
# 2. 一维浮点张量点积
c = torch.tensor([0.5, 1.0, 1.5])
d = torch.tensor([2.0, 3.0, 4.0])
dot_float = torch.dot(c, d)
print("浮点点积结果:", dot_float) # 0.5×2 + 1×3 + 1.5×4 = 1+3+6 = 10.0
2. 注意:必须是一维张量
torch.dot() 仅支持一维张量,传入多维张量会报错,这是最容易踩的坑:
import torch
# 错误示例:传入二维张量
a_2d = torch.tensor([[1,2], [3,4]])
b_2d = torch.tensor([[5,6], [7,8]])
try:
torch.dot(a_2d, b_2d)
except RuntimeError as e:
print("报错信息:", e) # 报错:1D tensors expected, got 2D tensors instead
# 正确做法:先展平为一维(如需计算二维张量的点积,用torch.matmul)
a_flat = a_2d.flatten() # [1,2,3,4]
b_flat = b_2d.flatten() # [5,6,7,8]
dot_flat = torch.dot(a_flat, b_flat)
print("展平后点积:", dot_flat) # 1×5 + 2×6 + 3×7 + 4×8 = 5+12+21+32 = 70
3. 与torch.matmul的区别
很多人会混淆dot和matmul,核心区别:
| 函数 | 输入要求 | 输出类型 | 适用场景 |
|---|---|---|---|
torch.dot |
仅支持一维张量 | 标量(0维) | 一维张量点积 |
torch.matmul |
支持任意维度张量 | 张量(维度随输入) | 矩阵乘法、批量矩阵乘法 |
import torch
# 一维张量:dot和matmul结果相同(但matmul返回一维张量)
x = torch.tensor([1,2,3])
y = torch.tensor([4,5,6])
print("dot结果:", torch.dot(x, y)) # tensor(32)(0维)
print("matmul结果:", torch.matmul(x, y)) # tensor(32)(1维)
# 二维张量:matmul支持矩阵乘法,dot不支持
x_2d = torch.tensor([[1,2], [3,4]])
y_2d = torch.tensor([[5,6], [7,8]])
print("matmul矩阵乘法:\n", torch.matmul(x_2d, y_2d))
# 输出:
# tensor([[19, 22],
# [43, 50]])
应用场景
1. 计算向量相似度(余弦相似度的核心)
点积是计算余弦相似度的基础(余弦相似度 = 点积 / (向量模长乘积)):
import torch
# 两个向量
vec1 = torch.tensor([1.0, 2.0, 3.0])
vec2 = torch.tensor([1.1, 1.9, 3.1])
# 计算点积
dot = torch.dot(vec1, vec2)
# 计算模长
norm1 = torch.norm(vec1) # 向量1的模长
norm2 = torch.norm(vec2) # 向量2的模长
# 余弦相似度
cos_sim = dot / (norm1 * norm2)
print("余弦相似度:", cos_sim) # 接近1,说明两个向量高度相似
2. 注意力机制中的打分计算
Transformer自注意力的“Q·K^T”本质是批量点积,是torch.dot的扩展:
import torch
# 模拟单个Query和Key的点积打分
query = torch.tensor([0.5, 0.3, 0.8]) # 一维Query向量
key = torch.tensor([0.4, 0.2, 0.9]) # 一维Key向量
score = torch.dot(query, key) # 注意力打分
print("注意力打分:", score) # 0.5×0.4 + 0.3×0.2 + 0.8×0.9 = 0.2+0.06+0.72 = 0.98
注意事项
- 维度限制:仅支持一维张量,多维张量需先展平(或用
matmul); - 长度一致:两个输入张量的长度必须完全相同,否则报错;
- 数据类型:两个张量的dtype需一致(如都是float32),否则会隐式转换或报错;
- 与NumPy的区别:NumPy的
np.dot支持多维张量,而PyTorch的torch.dot仅支持一维,需注意迁移代码时的差异。
总结
torch.dot()专用于计算两个一维张量的点积,结果是标量;- 核心规则:输入必须是一维、长度相同的张量,否则报错;
- 高频场景:向量相似度计算、注意力打分、线性代数基础运算;
- 易混点:多维张量乘法用
torch.matmul,而非torch.dot。