将元素列表转换为映射张量

时间:2021-05-26 22:15:39

标签: python pytorch tensor

我有一个字符串列表和一个返回每个文本中相等文本比例的函数。

示例:

def proportion(text1, text2):
  ...

list_text = ['hey', 'hello', ..., 'bye'] (size n)

我想要的是用函数 N x N 元素的结果计算一个 n x n 的张量。示例:

result = [ 1/7 (hey, hey)   ...   3/4 (hey bye)]
         [ ...                                 ]
         [ 3/4 (hey bye)  ...     3/3 (bye bye)]

有什么简单的方法吗?

1 个答案:

答案 0 :(得分:0)

n = len(list_text)
A = torch.empty(n,n)

for i in range(n):
    for j in range(n):
        A[i,j] = proportion(list_text[i], list_text[j])