问题描述
求两个矩阵对应维度的元素交集:
a = [[1,2,3],[4,5,6]]
b = [[0,3,4,5],[4,7,6,8]]
a,b 取交集,即:[1,2,3] ∩ [0,3,4,5], [4,5,6] ∩ [4,7,6,8],输出结果:
c = [[3], [4,6]],行不足长度末尾可补充0,即:
c = [[3,0],[4,6]]
Tensorflow 计算
a = tf.constant([[1,2,3], [4,5,6]])
b = tf.constant([[0,3,4,5], [4,7,6,8]])
d = tf.sets.intersection(a, b)
c = tf.sparse.to_dense(d)
print(c)
############ output ##############
tf.Tensor(
[[3 0]
[4 6]], shape=(2, 2), dtype=int32)