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

📅  最后修改于: 2023-12-03 14:46:07.936000             🧑  作者: Mango

Python - tensorflow.math.rint()

Introduction

The tensorflow.math.rint() function is a part of TensorFlow's Math API. It returns the element-wise integer value closest to the input. If a value is exactly halfway between two integers, it rounds the value towards the even integer.

Syntax
tensorflow.math.rint(x, name=None)
Parameters
  • x: Input tensor of any shape.
  • name: Optional name for the operation.
Return Value

The function returns a tensor with the same shape as the input x containing element-wise integer value closest to the input.

Example
import tensorflow as tf

x = tf.constant([1.4, 2.2, 3.6, 4.5, 5.9], dtype=tf.float32)
y = tf.math.rint(x)

print(y)

Output:

tf.Tensor([1. 2. 4. 4. 6.], shape=(5,), dtype=float32)

In the above example, we first create a tensor x with 5 float values. We then apply the tensorflow.math.rint() function to it and store the result in the y variable. Finally, we print y to the console to see the output.

The output shows that the rint() function has rounded the values in x to the closest integers. For example, 1.4 has been rounded down to 1, while 2.2 has been rounded up to 2. The value 4.5 has been rounded up to 4, which follows the rounding to the nearest even number rule.