【问题标题】:Tensorflow dense tensor to sparse binarized hash trick tensorTensorflow 密集张量到稀疏二值化哈希技巧张量
【发布时间】:2018-11-23 16:34:35
【问题描述】:

我想以这样一种方式转换这个数据集,即每个张量都具有给定的大小n,并且当且仅当存在i 时,这个新张量的索引i 处的特征设置为1在原始特征中(模 n)。

我希望下面的例子能让事情更清楚

假设我有一个像这样的数据集:

t = tf.constant([
  [0, 3, 4],
  [12, 2 ,4]])
ds = tf.data.Dataset.from_tensors(t)

我想得到 (if n = 9) 的稀疏等价物

t = tf.constant([
  [1, 0, 0, 1, 1, 0, 0, 0, 0], # index set to 1 are 0, 3 and 4
  [0, 0, 1, 1, 1, 0, 0, 0, 0]]) # index set to 1 are 2, 4, and 12%9 = 3

我已经知道如何获得非稀疏表示 (Tensorflow: tensor binarization),因为我最终会得到 n > 100 万,我不想通过密集张量来获得稀疏张量

谢谢

【问题讨论】:

  • 所以在这种情况下输入仍然是密集的,对吧?
  • 是的,输入仍然很密集

标签: python tensorflow sparse-matrix


【解决方案1】:

这是一个可能的实现:

import tensorflow as tf

def binarization_sparse(t, n):
    # Input size
    t_shape = tf.shape(t)
    t_rows = t_shape[0]
    t_cols = t_shape[1]
    # Make sparse row indices for each value
    row_idx = tf.tile(tf.range(t_rows)[: ,tf.newaxis], [1, t_cols])
    # Sparse column indices
    col_idx = t % n
    # "Flat" indices - needed to discard repetitions
    total_idx = row_idx * n + col_idx
    # Remove repeated elements
    out_idx, _ = tf.unique(tf.reshape(total_idx, [-1]))
    # Back to row and column indices
    sparse_idx = tf.stack([out_idx // n, out_idx % n], axis=-1)
    # Sparse values
    sparse_values = tf.ones([tf.shape(sparse_idx)[0]], dtype=t.dtype)
    # Make sparse tensor
    out = tf.sparse.SparseTensor(tf.cast(sparse_idx, tf.int64),
                                 sparse_values,
                                 [t_rows, n])
    # Reorder indices
    out = tf.sparse.reorder(out)
    return out

# Test
with tf.Graph().as_default(), tf.Session() as sess:
    t = tf.constant([
        [ 0,  3,  4],
        [12,  2,  4]
    ])
    # Sparse result
    t_m1h_sp = binarization_sparse(t, 9)
    # Convert to dense to check output
    t_m1h = tf.sparse.to_dense(t_m1h_sp)
    print(sess.run(t_m1h))

输出:

[[1 0 0 1 1 0 0 0 0]
 [0 0 1 1 1 0 0 0 0]]

我添加了删除重复元素的逻辑,因为原则上它可能会发生,但如果您保证没有重复(包括模数),您可以跳过该步骤。另外,我在最后对稀疏张量重新排序。这在这里并不是绝对必要的,但是(我认为)稀疏操作有时会期望索引是有序的(而sparse_idx 可能没有有序)。

此外,此解决方案特定于 2D 输入。对于一维输入会更简单,如果需要,也可以为更高维输入编写。我认为一个完全通用的解决方案是可能的,但它会更复杂(特别是如果你想考虑具有未知维数的张量)。

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    相关资源
    最近更新 更多