Tensorflow稠密张量转换为稀疏二元哈希技巧张量

2024-10-04 15:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

假设我有一个数据集,比如:

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

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

^{pr2}$

我已经知道如何得到一个非稀疏表示(Tensorflow: tensor binarization),而且我将得到n>;100万,我不想通过稠密张量来得到稀疏张量

谢谢


Tags: 数据fromdatatftensorflow方式ds特征
1条回答
网友
1楼 · 发布于 2024-10-04 15:33:54

下面是一个可能的实现方法:

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))

输出:

^{pr2}$

我添加了删除重复元素的逻辑,因为原则上可能会发生这种情况,但如果您保证不存在重复(包括模),则可以跳过该步骤。另外,我在末尾重新排列稀疏张量。这在这里不是严格必要的,但是(我认为)稀疏操作有时希望索引是有序的(并且sparse_idx可能不排序)。在

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

相关问题 更多 >

    热门问题