How to do elementwise multiplication between a sparse and a dense tensors in tensorflow?

时光毁灭记忆、已成空白 提交于 2019-12-24 11:43:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!