當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.math.floor()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。 floor()用於查找輸入的按元素取底的值,即不大於x的最大整數

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

參數:

  • X:這是一個張量,該張量的允許dtype是bfloat16,half,float32,float64。
  • name:這是一個可選參數,用於定義操作的名稱。

返回值:它返回與x相同dtype的張量。

範例1:



Python3

# 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:在該示例中,使用了二維張量。

Python3

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

Python3

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




相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.floor()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。