Python - tensorflow.math.nextafter ()

Опубликовано: 29 Марта, 2022

TensorFlow - это библиотека Python с открытым исходным кодом, разработанная Google для разработки моделей машинного обучения и нейронных сетей глубокого обучения. nextafter () используется для нахождения элемента wisenext, представляющего значение x1 в направлении x2.

Syntax: tf.math.nextafter(x1, x2, name)

Parameter:

  • x1: It’s the input tensor. Allowed dtype for this tensor are float64, float32.
  • x2: It’s the input tensor of same dtype as x1.
  • name(optional): It’s defines the name for the operation.

Returns:
It returns a tensor of dtype as x1.

Пример 1:

Python3

# Importing the library
import tensorflow as tf
  
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float64)
  
# Printing the input tensor
print("x1: ", x1)
print("x2: ", x2)
  
# Calculating result
res = tf.math.nextafter(x1, x2)
  
# Printing the result
print("Result: ", res)

Выход:

 x1: tf.Tensor ([1. 2. -3. -4.], shape = (4,), dtype = float64)
x2: tf.Tensor ([5. -7. 3. -8.], shape = (4,), dtype = float64)
Результат: tf.Tensor ([1. 2. -3. -4.], Shape = (4,), dtype = float64)


Example 2: This example uses different dtype for x1 and x2. It will raise InvalidArgumentError.

Python3

# importing the library
import tensorflow as tf
  
# Initializing the input tensor
x1 = tf.constant([1, 2, -3, -4], dtype = tf.float64)
x2 = tf.constant([5, -7, 3, -8], dtype = tf.float32)
  
# Printing the input tensor
print("x1: ", x1)
print("x2: ", x2)
  
# Calculating result
res = tf.math.nextafter(x1, x2)
  
# Printing the result
print("Result: ", res)

Выход:

 x1: tf.Tensor ([1. 2. -3. -4.], shape = (4,), dtype = float64)
x2: tf.Tensor ([5. -7. 3. -8.], shape = (4,), dtype = float32)

-------------------------------------------------- -------------------------

InvalidArgumentError Traceback (последний вызов последним)

 в ()
      8 
      9 # Расчет результата
---> 10 res = tf.math.nextafter (x1, x2)
     11 
     12 # Печать результата

2 кадра

/usr/local/lib/python3.6/dist-packages/six.py в raise_from (значение, from_value)

InvalidArgumentError: невозможно вычислить NextAfter, поскольку ожидалось, что вход №1 (отсчитываемый от нуля) будет двойным тензором, но является тензором с плавающей запятой [Op: NextAfter]

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.