什么是笛卡尔积?
待补充
数组的笛卡尔积
>>> a = [1, 2, 3]
>>> b = [4, 5]
>>> list(itertools.product(a, b))
[(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]
>>> tensor_a = torch.tensor(a)
>>> tensor_b = torch.tensor(b)
>>> torch.cartesian_prod(tensor_a, tensor_b)
tensor([[1, 4],
[1, 5],
[2, 4],
[2, 5],
[3, 4],
[3, 5]])
参考: PyTorch - torch.cartesian_prod 对给定的序列张量做笛卡尔乘积。 (runebook.dev)
矩阵的笛卡尔积
import torch
bs, seq_len, dim = 2, 2, 4
x = torch.randn(bs, seq_len, dim)
--------------------------------------------------------
print(x)
tensor([[[-1.3130, -0.7720, 0.1829, 0.0936],
[-1.2790, -0.7727, 0.0088, 0.0801]],
[[-0.7498, 0.2078, -1.4679, 1.4730],
[ 0.8118, -0.4901, 1.1376, -0.6495]]])
--------------------------------------------------------
idx = torch.arange(0, seq_len).long()
iidx = torch.cartesian_prod(idx, idx)
-----------------------------------------
print(iidx)
tensor([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
-----------------------------------------
ii = x[:, iidx]
print(ii)
print(ii.shape)
tensor([[[[-1.3130, -0.7720, 0.1829, 0.0936],
[-1.3130, -0.7720, 0.1829, 0.0936]],
[[-1.3130, -0.7720, 0.1829, 0.0936],
[-1.2790, -0.7727, 0.0088, 0.0801]],
[[-1.2790, -0.7727, 0.0088, 0.0801],
[-1.3130, -0.7720, 0.1829, 0.0936]],
[[-1.2790, -0.7727, 0.0088, 0.0801],
[-1.2790, -0.7727, 0.0088, 0.0801]]],
[[[-0.7498, 0.2078, -1.4679, 1.4730],
[-0.7498, 0.2078, -1.4679, 1.4730]],
[[-0.7498, 0.2078, -1.4679, 1.4730],
[ 0.8118, -0.4901, 1.1376, -0.6495]],
[[ 0.8118, -0.4901, 1.1376, -0.6495],
[-0.7498, 0.2078, -1.4679, 1.4730]],
[[ 0.8118, -0.4901, 1.1376, -0.6495],
[ 0.8118, -0.4901, 1.1376, -0.6495]]]])
torch.Size([2, 4, 2, 4])