python - 如何从张量流中的两个张量中创建频率张量
问题描述
我有一个这样的张量,其中值是频率,行是索引(0 to 6
):
tf_docs =
[[0, 2],
[1, 2],
[2, 1],
[5, 0],
[0, 1],
[7, 8],
[9, 6]]
我有一个常数张量,其中张量的值是索引:
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)
我需要逐行检查这些索引tf_docs
,结果矩阵将是tf_docs
其中它们不为零的列数(在两个索引中)。
例如,我们[1 2]
在tf_topics
. 这意味着检查行索引1
和2
中的值tf_docs
。在tf_docs
第一列和第二列中,两个值都不为零。这就是为什么[1 2]
频率会是2
。
另一方面,[1,3]
得到1
作为频率。因为索引第二列中的值之一3
为零。
所以结果将是这样的张量(这显然是对称的)。对角线将是每个频率的总和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 ]]
到目前为止我做了什么:
我决定使用tf.gather
and tf.count_nonzero
over 这两个矩阵。因为我想把它们分开index
,topics
看看这些是否indexes
同时发生tf_docs
tf.math.count_nonzero(tf.gather(tf_docs, tf_topics, axis=0), axis=1)
虽然,这似乎并没有给我想要的结果。
解决方案
让我们nonzero_tf_docs
定义为:
zero_tf_docs = tf.cast(tf.equal(tf_docs, tf.zeros_like(tf_docs)), tf.int32)
nonzero_tf_docs = 1 - tf.reduce_max(zero_tf_docs, axis=-1)
OP 要求计算nonzero_tf_docs[i] + nonzero_tf_docs[j]
每对索引i, j
的总和,tf_topics
并将结果显示在矩阵中。这可以通过以下方式实现:
def compute_result(tf_topics_, nonzero_tf_docs, tf_docs):
# Find matrix lower part
values = tf.reduce_sum(tf.gather(nonzero_tf_docs, tf_topics_), axis=-1)
max_index = tf.reduce_max(tf_topics) + 1
out_sparse = tf.sparse.SparseTensor(indices=tf_topics_, values=values, dense_shape=[max_index, max_index])
out_sparse = tf.cast(out_sparse, dtype=tf.int32)
out_sparse = tf.sparse.reorder(out_sparse)
out_dense = tf.sparse.to_dense(out_sparse, default_value=-1)
out_lower = tf.matrix_band_part(out_dense, -1, 0)
# Compute diagonal
diag_values = tf.reduce_sum(tf_docs, axis=-1)
diag = tf.slice(diag_values,
begin=[0],
size=[max_index])
# Construct output matrix
out = out_lower + tf.transpose(out_lower)
mask = tf.eye(max_index, dtype=tf.int32)
out = (1 - mask) * out + mask * diag
return out
# Find docs without zeros
zero_tf_docs = tf.cast(tf.equal(tf_docs, tf.zeros_like(tf_docs)), tf.int32)
nonzero_tf_docs = 1 - tf.reduce_max(zero_tf_docs, axis=-1)
# Transform counts into matrix format
tf_topics = tf.cast(tf_topics, dtype=tf.int64)
tf_topics_reversed = tf.reverse(tf_topics, [-1])
tf_topics_ = tf_topics_reversed
out_1 = compute_result(tf_topics_, nonzero_tf_docs, tf_docs)
out_2 = compute_result(tf_topics, nonzero_tf_docs, tf_docs)
out = tf.maximum(out_1, out_2)
with tf.Session() as sess:
r = sess.run(out)
print(r) # prints [[ 2 1 1 0 -1]
# [ 1 3 2 1 1]
# [ 1 2 3 1 1]
# [ 0 1 1 5 0]
# [-1 1 1 0 1]]
推荐阅读
- python - 通过发出 POST 请求来更新图像 - Flask
- javascript - 如何在元素的 innerHTML 中呈现 [object HTMLCollection].length?
- javascript - Javascript如何从其页面中提取令牌
- bash - 尝试从 shell 脚本运行 jmx 文件时预期 Jmeter 错误整数表达式
- html - 如何在 html 中使用 express 中的变量?
- android - 快速xy雷达图android
- docker - MacOS 上的 Docker 安装问题 - 构建失败并且 world/authserver 可执行文件似乎丢失
- python - {% if user.is_authenticated %} 仅针对特定用户
- java - 消耗所有服务器资源的多任务 Spring Boot RESTful API
- java - 如何在java中处理handshake_failure