是否有 tf.multiply 的稀疏版本? [英] Is there a sparse version of tf.multiply?

查看:35
本文介绍了是否有 tf.multiply 的稀疏版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tensorflow 有稀疏元素乘法吗?IE.tf.multiply() 的稀疏版本

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

我只找到了 tf.sparse_tensor_dense_matmul(),但它不是元素明智的乘法.

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

推荐答案

您可能正在寻找的函数是:__mul__

The function you might be looking for is: __mul__

来自官方文档的其他详细信息:

Additional details from official documentation:

稀疏张量中隐式零元素对应的输出位置将为零(即不会占用存储空间),无论稠密张量的内容如何(即使它是+/-INF 和 INF*0 == NaN).

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.

示例:

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())

输出:

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.]]

这篇关于是否有 tf.multiply 的稀疏版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆