当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tensorflow.bitcast()用法及代码示例


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

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

用法:
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.
 
返回: It returns a tensor of type type.

注意:位广播不能用于将实际dtype转换为复杂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)

输出:



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)

输出:

相关用法


注:本文由纯净天空筛选整理自aman neekhara大神的英文原创作品 Python | tensorflow.bitcast() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。