0

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

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

Yuval Atzmon
  • 5,645
  • 3
  • 41
  • 74

1 Answers1

3

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 (i.e., 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.]]