【问题标题】:Broadcast SparseTensor in tensorflow在 tensorflow 中广播 SparseTensor
【发布时间】:2019-08-06 12:02:11
【问题描述】:

我想将形状为[n, n, k] 的密集张量与形状为[n, n, 1] 的稀疏张量相乘。我希望稀疏张量的值沿轴重复,大小为s,就像我使用密集张量并依赖隐式广播时一样。

但是SparseTensor.__mul__ 操作不支持广播稀疏操作数。我没有找到明确广播稀疏张量的运算符。我怎样才能做到这一点?

【问题讨论】:

  • 你需要把它作为稀疏张量吗?您总是可以使其密集和相乘(无论如何它都会比其他密集张量小)。
  • 稠密张量是从形状[n, k]广播出来的,所以实际上也更小了。

标签: python tensorflow


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多