I have a tensor like this in which the values are the frequency and the rows are the index(0 to 6):
tf_docs =
[[0, 2],
[1, 2],
[2, 1],
[5, 0],
[0, 1],
[7, 8],
[9, 6]]
I have a constant tensor, in which values of the tensor are the index:
tf_topics = tf.constant([[1 2]
[1 3]
[1 0]
[2 3]
[2 0]
[3 0]
[3 4]
[3 2]
[3 1]
[4 2]
[4 1]
[2 1]], shape=(12, 2), dtype=int32)
I need to check these indexes row-wise in tf_docs and the result matrix would be the number of columns in the tf_docs in which they are not zero (in both indexes).
For example, We have [1 2] in the tf_topics. It means check the values in row index 1 and 2 in tf_docs. In tf_docs the first and second column both values are non-zero. thats why for [1 2] the frequency would be 2.
On the other hand, [1,3] get 1 as the frequency. Because one of the value in the second column of the index 3 is zero.
So the result will be a tensor like this(This is obviously symmetrical). The diagonal will be the sum of frequency of each index:
[[2, 1, 1, 0, null],
[1, 3, 2, 1, 1 ],
[1, 2, 3, 1, 1 ],
[0, 1, 1, 5, 0 ],
[null,1, 1, 0, 1 ]]
What I have done so far:
I decided to use tf.gather and tf.count_nonzero over the two matrices. because I wanted to split the index in the topics and see if these indexes co-occurred in tf_docs
tf.math.count_nonzero(tf.gather(tf_docs, tf_topics, axis=0), axis=1)
Though, this seems does not give me the result that I want.