📜  Python – tensorflow.math.scalar_mul()

📅  最后修改于: 2022-05-13 01:55:10.174000             🧑  作者: Mango

Python – tensorflow.math.scalar_mul()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

scalar_mul()用于将张量与标量相乘。

示例 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)


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)


输出:

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)



示例 2:此示例使用复张量。

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)

输出:

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)