第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > tf.dtypes

tf.dtypes

时间:2020-09-20 19:37:33

相关推荐

tf.dtypes

目录

一、概述

1、类

2、函数

3、别的成员

二、函数和类详解

1、tf.dtypes.as_dtype

2、tf.dtypes.cast

3、plex

4、tf.dtypes.DType

1、__init__

2、__eq__

3、__ne__

4、is_compatible_with

5、tf.dtypes.saturate_cast

一、概述

tf.dtypes的公共API名称空间。

1、类

class DType: 表示张量中元素的类型。

2、函数

as_dtype(...): 将给定的类型值转换为DType。cast(...): 将张量投射到一个新的类型上。complex(...): 将两个实数转换为复数。saturate_cast(...): 将值安全饱和转换为dtype。

3、别的成员

QUANTIZED_DTYPESbfloat16boolcomplex128complex64doublefloat16float32float64halfint16int32int64int8qint16qint32qint8quint16quint8resourcestringuint16uint32uint64uint8variant

二、函数和类详解

1、tf.dtypes.as_dtype

将给定的类型值转换为DType。

tf.dtypes.as_dtype(type_value)

参数:

type_value:可以转换为tf的值。DType对象。这可能是当前的tf。对象、数据类型枚举、字符串类型名称或numpy.dtype。

返回值:

与type_value对应的DType。

可能产生的异常:

TypeError: Iftype_valuecannot be converted to aDType.

2、tf.dtypes.cast

将张量投射到一个新的类型上。

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的行为相匹配。

参数:

x:数值型张量或稀疏张量或索引切片。可以是uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, complex64, complex128, bfloat16。dtype:目标类型。支持的dtypes列表与x相同。name:操作的名称(可选)。

返回值:

张量或稀疏张量或索引切片,其形状与x相同,类型与d类型相同。

可能产生的异常:

TypeError: Ifxcannot be cast to thedtype.

3、plex

将两个实数转换为复数。

plex(real,imag,name=None)

给定一个张量实数表示复数的实数部分,一个张量imag表示复数的虚数部分,这个操作返回形式的复数元素,其中a表示实数部分,b表示imag部分。输入张量实数和imag必须具有相同的形状。

参数:

real:一个张量。必须是下列类型之一:float32、float64。imag:张量。必须具有与实数相同的类型。name:操作的名称(可选)。

返回值:

复64或复128型张量。

可能产生的异常:

TypeError: Real and imag must be correct types

4、tf.dtypes.DType

表示张量中元素的类型。

tf.float16: 16-bit half-precision floating-point.tf.float32: 32-bit single-precision floating-point.tf.float64: 64-bit double-precision floating-point.tf.bfloat16: 16-bit truncated floating-plex64: 64-bit single-precision plex128: 128-bit double-precision complex.tf.int8: 8-bit signed integer.tf.uint8: 8-bit unsigned integer.tf.uint16: 16-bit unsigned integer.tf.uint32: 32-bit unsigned integer.tf.uint64: 64-bit unsigned integer.tf.int16: 16-bit signed integer.tf.int32: 32-bit signed integer.tf.int64: 64-bit signed integer.tf.bool: Boolean.tf.string: String.tf.qint8: Quantized 8-bit signed integer.tf.quint8: Quantized 8-bit unsigned integer.tf.qint16: Quantized 16-bit signed integer.tf.quint16: Quantized 16-bit unsigned integer.tf.qint32: Quantized 32-bit signed integer.tf.resource: Handle to a mutable resource.tf.variant: Values of arbitrary types.

函数的作用是:将numpy类型和字符串类型名称转换为DType对象。

1、__init__

__init__(type_enum)

创建一个新的数据类型。注意(mrry):在正常情况下,不应该直接构造数据类型对象。相反,使用tf.as_dtype()函数。

参数:

type_enum: types_pb2。数据类型枚举值。

可能产生的异常:

TypeError: Iftype_enumis not a valuetypes_pb2.DataType.

2、__eq__

__eq__(other)

如果此DType与其他类型引用相同的类型,则返回True。

3、__ne__

__ne__(other)

返回True iff self != other。

4、is_compatible_with

is_compatible_with(other)

如果将另一个DType转换为此DType,则返回True。转换规则如下:

DType(T) .is_compatible_with(DType(T)) == True

参数:

other:一个DType(或可转换为DType的对象)。

返回值:

如果另一个d类型的张量将隐式地转换成这个d类型,则为真。

5、tf.dtypes.saturate_cast

将值安全饱和转换为dtype。

tf.dtypes.saturate_cast(value,dtype,name=None)

这个函数将输入转换为dtype,而不应用任何缩放。如果有一个危险值将超过或低于铸造,该op应用适当的夹紧之前的铸造。

参数:

value:一个张量。dtype:所需的输出dtype。name:操作的名称(可选)。

返回值:

值安全转换为dtype。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。