Python – tensorflow.math.floor()

Python – tensorflow.math.floor()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 floor()用于查找输入元素的底值,即不大于x的最大整数。

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

参数:

  • x:它是一个张量,这个张量允许的dtype是bfloat16, half, float32, float64。
  • name: 这是一个可选的参数,定义了操作的名称。

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

示例 1:

# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([1.5, 2.7, 3.9, 1.2, 1.8], dtype = tf.float64)
 
# printing the input
print('a: ',a)
 
# Finding the floor value
r = tf.math.floor(a)
 
# printing the result
print("Result: ",r)

输出:

a:  tf.Tensor([1.5 2.7 3.9 1.2 1.8], shape=(5,), dtype=float64)
Result:  tf.Tensor([1. 2. 3. 1. 1.], shape=(5,), dtype=float64))

例子2:在这个例子中使用了二维张量。

# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([[1.5, 2.7], [3.9, 1.2]], dtype = tf.float64)
 
# printing the input
print('a: ',a)
 
# Finding the floor value
r = tf.math.floor(a)
 
# printing the result
print('Result: ',r)

输出:

a:  tf.Tensor(
[[1.5 2.7]
 [3.9 1.2]], shape=(2, 2), dtype=float64)
Result:  tf.Tensor(
[[1. 2.]
 [3. 1.]], shape=(2, 2), dtype=float64)

例子3:在这个例子中使用了无效的dtype张量。它将引发NotFoundError。

# importing the library
import tensorflow as tf
 
# initializing the input
a = tf.constant([1.5, 2.7, 3.9, 1.2, 1.8], dtype = tf.complex128)
 
# printing the input
print('a: ',a)
 
# Finding the floor value
r = tf.math.floor(a)

输出:

a:  tf.Tensor([1.5+0.j 2.7+0.j 3.9+0.j 1.2+0.j 1.8+0.j], shape=(5,), dtype=complex128)

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

NotFoundError                             Traceback (most recent call last)

 in ()
      6 
      7 # Finding the floor value
----> 8 r = tf.math.floor(a)

2 frames

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

NotFoundError: Could not find valid device for node.
Node:{{node Floor}}
All kernels registered for op Floor :
  device='XLA_GPU'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]
  device='XLA_CPU'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_HALF]
  device='GPU'; T in [DT_FLOAT]
  device='CPU'; T in [DT_DOUBLE]
  device='CPU'; T in [DT_HALF]
  device='CPU'; T in [DT_FLOAT]
 [Op:Floor]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Tensorflow 数学函数