📜  Python – tensorflow.math.sign()(1)

📅  最后修改于: 2023-12-03 15:04:11.159000             🧑  作者: Mango

Python – tensorflow.math.sign()

The tensorflow.math.sign() function is used to compute the element-wise sign of a given input tensor, returning a tensor with values of -1, 0 or 1, depending on the sign of the input.

Syntax
tensorflow.math.sign(x, name=None)
Parameters
  • x: A Tensor.
  • name: A name for the operation (optional).
Returns

A Tensor with the same shape as the input tensor x, with values of -1, 0 or 1, depending on the sign of each element.

Example
import tensorflow as tf 
   
x = tf.constant([-10, -5, -1, 0, 1, 5, 10], dtype=tf.float32) 
   
sign_x = tf.math.sign(x) 
   
with tf.Session() as sess: 
    print("Input Tensor: \n", sess.run(x)) 
    print("Element-wise Sign Tensor: \n", sess.run(sign_x))

Output:

Input Tensor: 
 [-10.  -5.  -1.   0.   1.   5.  10.]
Element-wise Sign Tensor: 
 [-1. -1. -1.  0.  1.  1.  1.]

This function can be used in various machine learning applications, specifically in the computation of gradients of activation functions in neural networks. It is also used to convert raw inputs to binary or simple classification tasks.

In summary, the tensorflow.math.sign() function is a versatile tool for computing element-wise signs of a given tensor, which is useful in various machine learning applications.