如何仅通过张量操作将已知连通分量转换为邻接矩阵?
How to convert known connected component to adjacency matrix with tensor manipulations only?
假设我有 N 个元素并且我知道它们是如何连接的。
这意味着有一个向量 [c1, c2, ..., cn],其中 [0...CC] 中的 ci CC 组件计数。
我想从中获取邻接矩阵。
简单的例子
输入
[0, 0, 1, 2, 0, 1]
输出
1 1 0 0 1 0
1 1 0 0 1 0
0 0 1 0 0 1
0 0 0 1 0 0
1 1 0 0 1 0
0 0 1 0 0 1
是否可以只用线性代数再加上TF的一些额外的张量运算来得到它?
这可以简单地通过与自身的广播比较来完成:
import tensorflow as tf
input = [0, 0, 1, 2, 0, 1]
adjacency_matrix = tf.cast(tf.equal(tf.expand_dims(input, 1), input), tf.int32)
print(sess.run(adjacency_matrix))
输出:
[[1 1 0 0 1 0]
[1 1 0 0 1 0]
[0 0 1 0 0 1]
[0 0 0 1 0 0]
[1 1 0 0 1 0]
[0 0 1 0 0 1]]
假设我有 N 个元素并且我知道它们是如何连接的。 这意味着有一个向量 [c1, c2, ..., cn],其中 [0...CC] 中的 ci CC 组件计数。
我想从中获取邻接矩阵。
简单的例子 输入
[0, 0, 1, 2, 0, 1]
输出
1 1 0 0 1 0
1 1 0 0 1 0
0 0 1 0 0 1
0 0 0 1 0 0
1 1 0 0 1 0
0 0 1 0 0 1
是否可以只用线性代数再加上TF的一些额外的张量运算来得到它?
这可以简单地通过与自身的广播比较来完成:
import tensorflow as tf
input = [0, 0, 1, 2, 0, 1]
adjacency_matrix = tf.cast(tf.equal(tf.expand_dims(input, 1), input), tf.int32)
print(sess.run(adjacency_matrix))
输出:
[[1 1 0 0 1 0]
[1 1 0 0 1 0]
[0 0 1 0 0 1]
[0 0 0 1 0 0]
[1 1 0 0 1 0]
[0 0 1 0 0 1]]