Python ``tensorflow.math.sign()`` 符号函数与实数 / 复数实例


发布日期 : 2024-02-03 11:43:36 UTC

访问量: 10 次浏览

Python – tensorflow.math.sign()

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

sign()是用来寻找一个数字的符号的元素指示。具体来说。

\(y = sign(x) = -1\) 如果x< 0; 0 if \(x == 0\); 1 if x >0。

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

语法:
tensorflow.math.sign(x, name)

参数:

  • x:它是一个张量。允许的d类型是bfloat16, half, float32, float64, int32, int64, complex64, complex128。
  • name(可选):它定义了该操作的名称。

返回:它返回一个与x具有相同 dtype 的张量。

示例 1:

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

输出:

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:

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