问题
Tensorflow has the implementation tf.sparse_tensor_dense_matmul
of sparse to dense matrix multiplication, but does it have sparse to dense elementwise multiplication (the two tensors having the same shape)? I would like to avoid converting my sparse tensor to a dense one as it wouldn't fit in memory.
回答1:
I don't believe there is a built-in function, but you can do this by hand relatively easily, at least if you don't intend to support broadcasting. If x
and y
are resp. your sparse and dense tensor,
res = tf.SparseTensor(x.indices, tf.gather_nd(y, x.indices) * x.values, x.dense_shape)
You may also want to check that the shapes of y
and x
are the same before multiplying them.
来源:https://stackoverflow.com/questions/45467758/how-to-do-elementwise-multiplication-between-a-sparse-and-a-dense-tensors-in-ten