目录
tf.dtypes的公共API名称空间。
QUANTIZED_DTYPES
bfloat16
bool
complex128
complex64
double
float16
float32
float64
half
int16
int32
int64
int8
qint16
qint32
qint8
quint16
quint8
resource
string
uint16
uint32
uint64
uint8
variant
二、函数和类详解
将给定的类型值转换为DType。
tf.dtypes.as_dtype(type_value)
参数:
返回值:
与type_value对应的DType。
可能产生的异常:
TypeError
: If type_value
cannot be converted to a DType
.将张量投射到一个新的类型上。
tf.dtypes.cast(
x,
dtype,
name=None
)
这个操作对x(对于张量)或x进行了强制转换。值(对于稀疏张量或索引切片)到dtype。
例:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.dtypes.cast(x, tf.int32) # [1, 2], dtype=tf.int32
该操作支持uint8、uint16、uint32、uint64、int8、int16、int32、int64、float16、float32、float64、complex64、complex128、bfloat16的数据类型(适用于x和dtype)。在将复杂类型(complex64、complex128)转换为实类型时,只返回x的实部份。在将实类型转换为复杂类型(complex64、complex128)时,返回值的虚部设置为0。这里对复杂类型的处理与numpy的行为相匹配。
参数:
返回值:
可能产生的异常:
TypeError
: If x
cannot be cast to the dtype
.将两个实数转换为复数。
tf.dtypes.complex(
real,
imag,
name=None
)
给定一个张量实数表示复数的实数部分,一个张量imag表示复数的虚数部分,这个操作返回形式的复数元素,其中a表示实数部分,b表示imag部分。输入张量实数和imag必须具有相同的形状。
参数:
real
:一个张量。必须是下列类型之一:float32、float64。imag
:张量。必须具有与实数相同的类型。返回值:
可能产生的异常:
TypeError
: Real and imag must be correct types表示张量中元素的类型。
函数的作用是:将numpy类型和字符串类型名称转换为DType对象。
__init__
__init__(type_enum)
创建一个新的数据类型。注意(mrry):在正常情况下,不应该直接构造数据类型对象。相反,使用tf.as_dtype()函数。
参数:
可能产生的异常:
TypeError
: If type_enum
is not a value types_pb2.DataType
.__eq__
__eq__(other)
如果此DType与其他类型引用相同的类型,则返回True。
__ne__
__ne__(other)
返回True iff self != other。
is_compatible_with
is_compatible_with(other)
如果将另一个DType转换为此DType,则返回True。转换规则如下:
DType(T) .is_compatible_with(DType(T)) == True
参数:
other
:一个DType(或可转换为DType的对象)。返回值:
将值安全饱和转换为dtype。
tf.dtypes.saturate_cast(
value,
dtype,
name=None
)
这个函数将输入转换为dtype,而不应用任何缩放。如果有一个危险值将超过或低于铸造,该op应用适当的夹紧之前的铸造。
参数:
返回值: