Multiplies a scalar times a `Tensor` or `IndexedSlices` object. Intended for use in gradient code which might deal with `IndexedSlices` objects, which are easy to multiply by a scalar but more expensive to multiply with arbitrary tensors. Args: scalar: A 0-D scalar `Tensor`. Must have
(scalar, x, name=None)
| 394 | |
| 395 | @tf_export(v1=["math.scalar_mul", "scalar_mul"]) |
| 396 | def scalar_mul(scalar, x, name=None): |
| 397 | """Multiplies a scalar times a `Tensor` or `IndexedSlices` object. |
| 398 | |
| 399 | Intended for use in gradient code which might deal with `IndexedSlices` |
| 400 | objects, which are easy to multiply by a scalar but more expensive to |
| 401 | multiply with arbitrary tensors. |
| 402 | |
| 403 | Args: |
| 404 | scalar: A 0-D scalar `Tensor`. Must have known shape. |
| 405 | x: A `Tensor` or `IndexedSlices` to be scaled. |
| 406 | name: A name for the operation (optional). |
| 407 | |
| 408 | Returns: |
| 409 | `scalar * x` of the same type (`Tensor` or `IndexedSlices`) as `x`. |
| 410 | |
| 411 | Raises: |
| 412 | ValueError: if scalar is not a 0-D `scalar`. |
| 413 | """ |
| 414 | scalar = ops.convert_to_tensor( |
| 415 | scalar, dtype=x.dtype.base_dtype, name="scalar") |
| 416 | shape = scalar.get_shape() |
| 417 | if shape.ndims == 0: |
| 418 | if isinstance(x, ops.IndexedSlices): |
| 419 | return ops.IndexedSlices( |
| 420 | gen_math_ops.mul(scalar, x.values, name), x.indices, x.dense_shape) |
| 421 | else: |
| 422 | return gen_math_ops.mul(scalar, x, name) |
| 423 | else: |
| 424 | raise ValueError("Only scalar multiply works, got shape %s" % shape) |
| 425 | |
| 426 | |
| 427 | @tf_export("math.scalar_mul", "scalar_mul", v1=[]) |
no test coverage detected