什么是笛卡尔积?
待补充
数组的笛卡尔积
>>> 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