简体   繁体   中英

Is there a sparse version of tf.multiply?

Does Tensorflow has a sparse element wise multiplication? Ie A sparse version of tf.multiply()

I only found tf.sparse_tensor_dense_matmul(), but it's not element wise multiplication.

The function you might be looking for is: __mul__

Additional details from official documentation:

The output locations corresponding to the implicitly zero elements in the sparse tensor will be zero (ie, will not take up storage space), regardless of the contents of the dense tensor (even if it's +/-INF and that INF*0 == NaN).

Limitation : this Op only broadcasts the dense side to the sparse side, but not the other direction.

Example:

sp_mat = tf.SparseTensor([[0,0],[0,2],[1,2],[2,1]], np.ones(4), [3,3])
const1 = tf.constant([[1,2,3],[4,5,6],[7,8,9]], dtype=tf.float64)
const2 = tf.constant(np.array([1,2,3]),dtype=tf.float64)

elementwise_result = sp_mat.__mul__(const1)
broadcast_result   = sp_mat.__mul__(const2)

print("Sparse Matrix:\n",tf.sparse_tensor_to_dense(sp_mat).eval())
print("\n\nElementwise:\n",tf.sparse_tensor_to_dense(elementwise_result).eval())
print("\n\nBroadcast:\n",tf.sparse_tensor_to_dense(broadcast_result).eval())

Output:

Sparse Matrix:
 [[ 1.  0.  1.]
 [ 0.  0.  1.]
 [ 0.  1.  0.]]


Elementwise:
 [[ 1.  0.  3.]
 [ 0.  0.  6.]
 [ 0.  8.  0.]]


Broadcast:
 [[ 1.  0.  3.]
 [ 0.  0.  3.]
 [ 0.  2.  0.]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM