我没有找到*
记录在哪里。似乎它可以等同于tf.multiply
或tf.scalar_mul
。是这样吗?
最可靠的文档是source code:
def _mul_dispatch(x, y, name=None):
"""Dispatches cwise mul for "Dense*Dense" and "Dense*Sparse"."""
is_tensor_y = isinstance(y, ops.Tensor)
if is_tensor_y:
return gen_math_ops._mul(x, y, name=name)
else:
assert isinstance(y, sparse_tensor.SparseTensor) # Case: Dense * Sparse.
new_vals = gen_sparse_ops.sparse_dense_cwise_mul(y.indices, y.values,
y.dense_shape, x, name)
return sparse_tensor.SparseTensor(y.indices, new_vals, y.dense_shape)
...
_OverrideBinaryOperatorHelper(_mul_dispatch, "mul")
这意味着__mul__
运算符重载,它执行_mul_dispatch
。正如你所看到的,如果张量稀疏,它会调用gen_math_ops._mul
(这是tf.multiply
的底层核心函数)或sparse_dense_cwise_mul
。
顺便说一句,tf.scalar_mul
只是scalar * x
(source code)的包装器,所以它基本上是相同的,但依赖是另一种方式。