以十为单位广播SparseTensor

2024-04-25 21:00:03 发布

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

我想用[n, n, k]形的稠密张量和[n, n, 1]形的稀疏张量进行元素相乘。我希望稀疏张量中的值沿着轴重复,大小为s,就像我使用密集张量而依赖隐式广播一样。你知道吗

但是SparseTensor.__mul__操作不支持广播稀疏操作数。我没有找到一个操作符来显式地广播稀疏张量。我怎样才能做到这一点?你知道吗


Tags: 元素mul密集sparsetensor
1条回答
网友
1楼 · 发布于 2024-04-25 21:00:03

如果不希望仅将稀疏张量转换为稠密张量,可以从稠密张量中提取并选择正确的值以直接构建稀疏结果,如下所示:

import tensorflow as tf
import numpy as np

with tf.Graph().as_default(), tf.Session() as sess:
    # Input data
    x = tf.placeholder(tf.float32, shape=[None, None, None])
    y = tf.sparse.placeholder(tf.float32, shape=[None, None, 1])
    # Indices of sparse tensor without third index coordinate
    indices2 = y.indices[:, :-1]
    # Values of dense tensor corresponding to sparse tensor values
    x_sp = tf.gather_nd(x, indices2)
    # Values of the resulting sparse tensor
    res_vals = tf.reshape(x_sp * tf.expand_dims(y.values, 1), [-1])
    # Shape of the resulting sparse tensor
    res_shape = tf.shape(x, out_type=tf.int64)
    # Make sparse tensor indices
    k = res_shape[2]
    v = tf.size(y.values)
    # Add third coordinate to existing sparse tensor coordinates
    idx1 = tf.tile(tf.expand_dims(indices2, 1), [1, k, 1])
    idx2 = tf.tile(tf.range(k), [v])
    res_idx = tf.concat([tf.reshape(idx1, [-1, 2]), tf.expand_dims(idx2, 1)], axis=1)
    # Make sparse result
    res = tf.SparseTensor(res_idx, res_vals, res_shape)
    # Dense value for testing
    res_dense = tf.sparse.to_dense(res)
    # Dense operation for testing
    res_dense2 = x * tf.sparse.to_dense(y)
    # Test
    x_val = np.arange(48).reshape(4, 4, 3)
    y_val = tf.SparseTensorValue([[0, 0, 0], [2, 3, 0], [3, 1, 0]], [1, 2, 3], [4, 4, 1])
    res_dense_val, res_dense2_val = sess.run((res_dense, res_dense2),
                                             feed_dict={x: x_val, y: y_val})
    print(np.allclose(res_dense_val, res_dense2_val))
    # True

相关问题 更多 >

    热门问题