📜  Python| tensorflow.bitcast() 方法

📅  最后修改于: 2022-05-13 01:54:19.462000             🧑  作者: Mango

Python| tensorflow.bitcast() 方法

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

bitcast() 是 tensorflow 库中的方法,用于将张量从一种类型比特转换为另一种类型。它不会复制数据。

Syntax:
tf.bitcast(
    input, type, name
)

Arguments: 
1. input: It is the Tensor and the allowed type for this tensor are
          bfloat16, half, float32, float64, int64, int32, uint8, uint16, uint32,
          uint64, int8, int16, complex64, complex128, qint8, quint8, qint16, quint16, qint32.
2. type: It defines the dtype in which input need to be bitcasted.
3. name: It is an optional argument. It is used to give a name to operation.
 
Return: It returns a tensor of type type.

注意: bitcast 不能用于将 real dtype 转换为 complex dtype。它将引发 InvalidArgumentError。

示例 1:

Python3
# importing the library
import tensorflow
  
# initializing the constant tensor of dtype unit32
a = tensorflow.constant(0xffffffff, dtype=tensorflow.uint32)
  
# Checking the initialized tensor
print('a:',a)
  
# bitcasting to dtype unit8
b = tensorflow.bitcast(a, tensorflow.uint8)
  
# Checking the bitcasted tensor
print('b:',b)


Python3
# importing the library
import tensorflow
  
# initializing the constant tensor of dtype unit32
a = tensorflow.constant(0xffffffff, dtype=tensorflow.uint32)
  
# Checking the initialized tensor
print('a:',a)
  
# bitcasting to dtype complex128
b = tensorflow.bitcast(a, tensorflow.complex128)


输出:

a: tf.Tensor(4294967295, shape=(), dtype=uint32)
b: tf.Tensor([255 255 255 255], shape=(4,), dtype=uint8)

示例 2:

此示例尝试将真实 dtype 比特转换为复杂 dtype

Python3

# importing the library
import tensorflow
  
# initializing the constant tensor of dtype unit32
a = tensorflow.constant(0xffffffff, dtype=tensorflow.uint32)
  
# Checking the initialized tensor
print('a:',a)
  
# bitcasting to dtype complex128
b = tensorflow.bitcast(a, tensorflow.complex128)

输出: