Open In App

Python – tensorflow.math.scalar_mul()

Last Updated : 11 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.

scalar_mul() is used to multiply a tensor with a scalar.

Syntax: tf.math.scalar_mul( scalar, x, name )

Parameters:

  • scalar: It is a 0-D scalar tensor of known shape.
  • x: It’s a tensor that need to be scaled.
  • name(optional): It defines the name for operation.

Returns:

It returns a tensor of same dtype as x.

Example 1:

Python3




# importing the library
import tensorflow as tf
  
# Initializing the input tensor
scalar = (5)
a = tf.constant([2.5, 5.5, 1.5, 6.5], dtype = tf.float64)
  
# Printing the input tensor
print('scalar: ', scalar)
print('a: ', a)
  
# Calculating result
res = tf.math.scalar_mul(scalar, a)
  
# Printing the result
print('Result: ', res)


Output:

scalar:  5
a:  tf.Tensor([2.5 5.5 1.5 6.5], shape=(4, ), dtype=float64)
Result:  tf.Tensor([12.5 27.5  7.5 32.5], shape=(4, ), dtype=float64)



Example 2: This example uses complex tensor.

Python3




# importing the library
import tensorflow as tf
  
# Initializing the input tensor
scalar = (5)
a = tf.constant([2.5 + 3j, 5.5 + 1j, 1.5 + 7j, 6.5 + 8j], dtype = tf.complex128)
  
# Printing the input tensor
print('scalar: ', scalar)
print('a: ', a)
  
# Calculating result
res = tf.math.scalar_mul(scalar, a)
  
# Printing the result
print('Result: ', res)


Output:

scalar:  5
a:  tf.Tensor([2.5+3.j 5.5+1.j 1.5+7.j 6.5+8.j], shape=(4, ), dtype=complex128)
Result:  tf.Tensor([12.5+15.j 27.5 +5.j  7.5+35.j 32.5+40.j], shape=(4, ), dtype=complex128)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads