我想以这样一种方式转换这个数据集:每个张量都有一个给定的大小n
,并且这个新张量的索引i
处的一个特征被设置为1当且仅当原始特征(模n)中有一个i
。在
我希望下面的例子能让事情更清楚
假设我有一个数据集,比如:
t = tf.constant([
[0, 3, 4],
[12, 2 ,4]])
ds = tf.data.Dataset.from_tensors(t)
我想得到(ifn
=9)的稀疏等价物
我已经知道如何得到一个非稀疏表示(Tensorflow: tensor binarization),而且我将得到n>;100万,我不想通过稠密张量来得到稀疏张量
谢谢
下面是一个可能的实现方法:
输出:
^{pr2}$我添加了删除重复元素的逻辑,因为原则上可能会发生这种情况,但如果您保证不存在重复(包括模),则可以跳过该步骤。另外,我在末尾重新排列稀疏张量。这在这里不是严格必要的,但是(我认为)稀疏操作有时希望索引是有序的(并且
sparse_idx
可能不排序)。在此外,这种解决方案是特定于二维输入。对于1D输入将更简单,并且如果需要,也可以为更高维的输入编写。我认为一个完全通用的解是可能的,但它会更复杂(特别是如果你想考虑维数未知的张量)。在
相关问题 更多 >
编程相关推荐