Python – tensorflow.math.sign()

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

sign() is used to find element wise indication of the sign of a number. Specifically,

y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.

For complex numbers, y = sign(x) = x / |x| if x != 0, otherwise y = 0.

Syntax: tensorflow.math.sign(x, name)



Parameters:

  • x: It’s a tesnor. Allowed dtypes are bfloat16, half, float32, float64, int32, int64, complex64, complex128. 
  • name(optional): It defines the name for the operation.

Return: It return a tensor of same dtype as x.

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the library
import tensorflow as tf
  
# Initializing the input tensor
a = tf.constant([0, 1, -2, 3, -4], dtype = tf.float64)
  
# Printing the input tensor
print('a: ', a)
  
# Calculating result
res = tf.math.sign(x = a)
  
# Printing the result
print('Result: ', res)

chevron_right


Output:

a:  tf.Tensor([ 0.  1. -2.  3. -4.], shape=(5, ), dtype=float64)
Result:  tf.Tensor([ 0.  1. -1.  1. -1.], shape=(5, ), dtype=float64)


Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the library
import tensorflow as tf
  
# Initializing the input tensor
a = tf.constant([ 1-5j, -2 + 3j, -3-7j, -4 + 8j], dtype = tf.complex128)
  
# Printing the input tensor
print('a: ', a)
  
# Calculating result
res = tf.math.sign(x = a)
  
# Printing the result
print('Result: ', res)

chevron_right


Output:

a:  tf.Tensor([ 1.-5.j -2.+3.j -3.-7.j -4.+8.j], shape=(4, ), dtype=complex128)
Result:  tf.Tensor(
[ 0.19611614-0.98058068j -0.5547002 +0.83205029j -0.3939193 -0.91914503j
 -0.4472136 +0.89442719j], shape=(4, ), dtype=complex128)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.