张量流中记录了`*`在哪里?

问题描述 投票:5回答:1

我没有找到*记录在哪里。似乎它可以等同于tf.multiplytf.scalar_mul。是这样吗?

python python-3.x tensorflow multiplying
1个回答
4
投票

最可靠的文档是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 * xsource code)的包装器,所以它基本上是相同的,但依赖是另一种方式。

© www.soinside.com 2019 - 2024. All rights reserved.