从字符串的Tensor 返回子字符串。
用法
tf.compat.v1.substr(
input, pos, len, name=None, unit='BYTE'
)抛出
-
ValueError:如果第一个参数不能转换为dtype string的张量。InvalidArgumentError:如果索引超出范围。ValueError:如果pos和len不是同一个形状。
参数
-
inputTensor类型为string。弦张量 -
pos一个Tensor。必须是以下类型之一:int32,int64。标量定义每个子字符串中第一个字符的位置 -
len一个Tensor。必须与pos具有相同的类型。标量定义要包含在每个子字符串中的字符数 -
unit一个可选的string来自:"BYTE", "UTF8_CHAR"。默认为"BYTE"。用于创建子字符串的单位。以下之一:"BYTE"(用于按字节定义位置和长度)或"UTF8_CHAR"(用于 UTF-8 编码的 Unicode 代码点)。默认值为"BYTE"。如果unit=UTF8_CHAR和input字符串不包含结构上有效的 UTF-8,则结果未定义。 -
name操作的名称(可选)。
返回
-
Tensor类型为string。
对于输入 Tensor 中的每个字符串,创建一个从索引 pos 开始的子字符串,总长度为 len 。
如果len 定义了一个超出输入字符串长度的子字符串,或者如果len 为负数,则使用尽可能多的字符。
负数pos 表示字符串内从末端向后的距离。
如果 pos 指定的索引超出任何输入字符串的范围,则抛出 InvalidArgumentError。
pos 和 len 必须具有相同的形状,否则在创建操作时会抛出 ValueError。
注意: Substr最多支持二维广播。更多关于广播这里
例子
使用标量 pos 和 len :
input = [b'Hello', b'World']
position = 1
length = 3
output = [b'ell', b'orl']
使用与 input 形状相同的 pos 和 len :
input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen']]
position = [[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
length = [[2, 3, 4],
[4, 3, 2],
[5, 5, 5]]
output = [[b'en', b'eve', b'lve'],
[b'hirt', b'urt', b'te'],
[b'ixtee', b'vente', b'hteen']]
将 pos 和 len 广播到 input :
input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen'],
[b'nineteen', b'twenty', b'twentyone']]
position = [1, 2, 3]
length = [1, 2, 3]
output = [[b'e', b'ev', b'lve'],
[b'h', b'ur', b'tee'],
[b'i', b've', b'hte'],
[b'i', b'en', b'nty']]
将 input 广播到 pos 和 len :
input = b'thirteen'
position = [1, 5, 7]
length = [3, 2, 1]
output = [b'hir', b'ee', b'n']
相关用法
- Python tf.compat.v1.summary.merge用法及代码示例
- Python tf.compat.v1.summary.FileWriter用法及代码示例
- Python tf.compat.v1.summary.merge_all用法及代码示例
- Python tf.compat.v1.strings.length用法及代码示例
- Python tf.compat.v1.scatter_min用法及代码示例
- Python tf.compat.v1.size用法及代码示例
- Python tf.compat.v1.scatter_add用法及代码示例
- Python tf.compat.v1.scatter_div用法及代码示例
- Python tf.compat.v1.space_to_batch用法及代码示例
- Python tf.compat.v1.string_split用法及代码示例
- Python tf.compat.v1.squeeze用法及代码示例
- Python tf.compat.v1.set_random_seed用法及代码示例
- Python tf.compat.v1.sparse_to_dense用法及代码示例
- Python tf.compat.v1.sparse_segment_sum用法及代码示例
- Python tf.compat.v1.scatter_update用法及代码示例
- Python tf.compat.v1.sparse_split用法及代码示例
- Python tf.compat.v1.string_to_number用法及代码示例
- Python tf.compat.v1.saved_model.simple_save用法及代码示例
- Python tf.compat.v1.scatter_nd_sub用法及代码示例
- Python tf.compat.v1.sparse_reduce_max用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.substr。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
