由于tensorflow中的函数名称和种类众多,在对tensorflow的函数有一个整体的认识之前,只能将函数分类后按照类别的字典顺序进行介绍,这一系列全部结束之后会有 一些总结。
本文介绍的是Asserts and boolean checks(断言函数)。断言函数主要用于判断输入参数是否满足各种预设条件,例如是否大于零,是否在某个范围内,是否为规定的数据类型等,常见于程序 的调试过程中。Tensorflow中的断言函数主要包括:
tf.assert_negative(用于判断数据x中的每个变量是否都小于零,x为空时亦满足条件)
tf.assert_positive
(用于判断数据x中的每个变量是否都大于零,x为空时亦满足条件)
tf.assert_proper_iterable
(用于判断变量是否可迭代)
tf.assert_non_negative
(用于判断数据x中的每个变量是否都非负,x为空时亦满足条件)
tf.assert_non_positive
(用于判断数据x中的每个变量是否都非正,x为空时亦满足条件)
tf.assert_equal
(用于判断数据x和y中的每个变量是否都完全相同,x和y同时为空时亦满足条件)
tf.assert_integer(判断数据的类型是否为整型)
tf.assert_less
(用于判断数据x中的每个变量是否都小于y中对应位置的变量
,x和y同时为空时亦满足条件)
tf.assert_less_equal
(用于判断数据x中的每个变量是否都小于等于y中对应位置的变量
,x和y同时为空时亦满足条件)
tf.assert_greater
(用于判断数据x中的每个变量是否都大于y中对应位置的变量
,x和y同时为空时亦满足条件)
tf.assert_greater_equal
(用于判断数据x的每个变量是否都大于等于y中对应位置的变量
,x和y同时为空时亦满足条件)
tf.assert_rank(用于判断张量x的深度是否等于rank)
tf.assert_rank_at_least
(用于判断张量x的深度是否大于等于rank)
tf.assert_type
(用于判断张量x的数据是否为指定类型)
tf.is_non_decreasing
(用于判断张量x的第i个数据是否小于等于第i+1个数据)
tf.is_numeric_tensor(用于判断x的数据类型是否为数值)
tf.is_strictly_increasing
(用于判断张量x的第i个数据是否小于第i+1个数据)
1 tf.assert_negative
tf.assert_negative、tf.assert_positive、tf.assert_non_negative、tf.assert_non_positive的函数原型相同,以tf.assert_negative为例,其原型为:
tf.debugging.assert_negative(
x,
data=None,
summarize=None,
message=None,
name=None
)
其中各输入参数的含义分别为:x为储存数据的张量,data表示如果条件不满足需要打印的数据,默认为出错信息和x的前几个分量,summarize表示打印的每个张量的分量数目(默认为3个),message为自定义的信息,加在错误信息之前作为前缀,name表示自定义的该操作的名称,默认为assert_negative。
如果不满足条件,该函数的返回值InvalidArgumentError,
并根据用户自定义执行相关操作。
2 tf.assert_proper_iterable
函数原型为
tf.debugging.assert_proper_iterable(values)
values为待检测的变量,当value不可迭代时返回TypeError。
3 tf.assert_equal
tf.assert_equal、tf.assert_less、tf.assert_less_equal、tf.assert_greater、tf.assert_greater_equal的函数原型相同,以tf.assert_equal为例。
函数原型为:
tf.debugging.assert_equal(
x,
y,
message=None,
summarize=None,
name=None
)
其中x和y为待比较的两个张量,其余参数定义与 1中相同。当x不等于y时返回InvalidArgumentError。
4.tf.assert_integer
函数原型为
tf.debugging.assert_integer(
x,
message=None,
name=None
)
x为基类型为整数的张量并且非量化,如果x.dtype不是非量化整数,返回TypeError。
5.tf.assert_rank
函数原型为:
tf.debugging.assert_rank(
x,
rank,
message=None,
name=None
)
x为输入张量,rank为一个标量,当x的深度不等于rank时,反会InvalidArgumentError
。
6.tf.assert_rank_at_least
函数原型为:
tf.debugging.assert_rank_at_least(
x,
rank,
data=None,
summarize=None,
message=None,
name=None
)
参数定义及返回值与5相同。
7tf.assert_type
函数原型
tf.debugging.assert_type(
tensor,
tf_type,
message=None,
name=None
)
其中,tensor为输入参数,tf_type为tensorflow中的数据类型,如dtypes.float32
, tf.int64
, dtypes.bool
等。当数据类型不相同时,返回TypeError。
8.tf.is_non_decreasing
tf.is_non_decreasing
与tf.is_strictly_increasing
函数原型相同,以tf.is_non_decreasing
以为例。
函数原型为:
tf.math.is_non_decreasing(
x,
name=None
)
其中x为数值张量,当x的数据类型不为数值时,返回TypeError,否则返回一个布尔型的张量。
9.tf.is_numeric_tensor
函数原型为:
tf.debugging.is_numeric_tensor(tensor)
如果tensor的数据类型为
其中之一,则返回True,否则返回False。