如何将张量的每一行乘以张量流中元素明智的其余行 [英] how to multiply each row of a tensor to the rest of rows element wise in tensorflow

查看:26
本文介绍了如何将张量的每一行乘以张量流中元素明智的其余行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的张量:

tf_docs = tf.constant([[0, 2, 1],
                       [1, 2, 2],
                       [2, 1, 3],
                       [5, 2, 2]], dtype=tf.int32)

我需要将每一行乘以其余行,元素明智,然后总结结果.

I need to multiply each row by rest of the rows, element wise and then sum up result.

处理完第一行后,我们将处理第二行和其余行,然后...

When done with first row, we will do with second row and rest of the rows and...

所以结果将是这样的:它将是 4*4 因为我们有 4 行

So the result will be like this: It will be 4*4 because we have 4 rows

result_tf =[[0,  6,  5, 6 ],
            [6,  0, 10, 13],
            [5, 10,  3, 18],
            [6,  13, 18, 0]]

让我解释一下result_tf中的每个元素(矩阵是对称的.

Let me explain each element in result_tf (the matrix is symmetrical.

第一行:

0*1 + 2*2 + 1*2 = 6
0*2 + 2*1 + 1*3 = 5
0*5 + 2*2 + 1*2 = 6

第二行:

1*2 + 2*1 + 2*3 = 10
1*5 + 2*2 + 2*2 = 13

第三行:

2*5 + 1*2 + 3*2 = 18

这些就是我如何形成矩阵的上侧.

These are how I formed the upper side of the matrix.

那么诊断中的值是:

(0,0) 在任何列中都没有 co-occurred 所以 0

(0,0) has not co-occurred in any column so 0

(1,1) 在任何列中都没有 co-occurred 所以 0

(1,1) has not co-occurred in any column so 0

(2,2) 在第二列有 co-occurred 2 次,在第三列 1 次 所以 3

(2,2) has co-occurred 2 time in second column and 1 time third column so 3

(3,3) 没有在任何列中同时出现,所以 0

(3,3) has not co-occurred in any column so 0

我觉得这比了解技术需要更多的创造力来解决.(如果您知道共现的概念,我在这里计算的是同一个矩阵的共现)

I feel this needs more creativity to solve than knowing techniques. (Literally here I am computing the co-occurrence over the same matrix if you are aware of the concept of co-occurrence)

我做了什么:

我可以使用 for 循环轻松地做到这一点.但是我需要完成 tensorflow 操作.我找不到与此问题类似的任何内容.我还考虑使用 gather 每次获取指定的行并将它们连接起来.但这种方式不是动态的,我的行和列都比这大,所以这个解决方案不可行

I can do this easily using for loop. But I need to be done with the tensorflow operation.and I could not find anything similar to this problem. I was also thinking use gather to get specified rows each time and concat them. But this way is not dynamic and my rows and columns are larger than this so this solution will not be feasible

推荐答案

这里有一个方法:

import tensorflow as tf

tf_docs = tf.constant([[0, 2, 1],
                       [1, 2, 2],
                       [2, 1, 3],
                       [5, 2, 2]], dtype=tf.int32)

# Non-diagonal elements
nondiag = tf.matmul(tf_docs, tf_docs, transpose_b=True)
# Compute diagonal
r = tf.range(tf.shape(tf_docs, out_type=tf_docs.dtype)[0])
# Compare each index against each value
cmp = tf.equal(tf.expand_dims(tf_docs, axis=-1), r)
# Count appearances of each index in each column
count = tf.math.count_nonzero(cmp, axis=0, dtype=tf_docs.dtype)
# Sum number of appearances over one
diag = tf.reduce_sum(tf.maximum(count - 1, 0), axis=0)
# Set diagonal elements
result_tf = tf.linalg.set_diag(nondiag, diag)
print(result_tf.numpy())
# [[ 0  6  5  6]
#  [ 6  0 10 13]
#  [ 5 10  3 18]
#  [ 6 13 18  0]]

这篇关于如何将张量的每一行乘以张量流中元素明智的其余行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆