📜  Python – tensorflow.math.sign()

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

Python – tensorflow.math.sign()

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

sign()用于查找数字符号的元素明智指示。具体来说,

y = 符号(x) = -1 如果 x < 0;如果 x == 0,则为 0;如果 x > 0,则为 1。

对于复数,y = sign(x) = x / |x|如果 x != 0,否则 y = 0。

示例 1:

Python3
# 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)


Python3
# 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)


输出:

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

示例 2:

Python3

# 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)

输出:

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)