衣衫褴褛的张量
概述
你的数据有多种形式;你的张量也应该如此。衣衫褴褛的张量TensorFlow等价于嵌套变长列表。它们使存储和处理具有非均匀形状的数据变得容易,包括:
- 可变长度的特征,如电影中的一组演员
- 成批可变长度的顺序输入,如句子或视频剪辑。
- 分层输入,例如被细分为段落、段落、句子和单词的文本文档。
- 结构化输入中的单个字段,例如协议缓冲区。
用粗糙张量能做什么
不规则张量由超过100个TensorF卡塔尔世界杯欧洲预选赛赛程表low操作支持,包括数学操作(如tf增加美元
而且tf reduce_mean美元
)、数组操作(例如tf concat美元
而且tf美元瓦
),字符串操作操作(例如tf美元的子串
),以及其他许多:
图书馆(tensorflow)数字< -特遣部队$衣衫褴褛的$常数(列表(列表(3.,1,4,1),列表(),列表(5,9,2),列表(6),列表()))话说=特遣部队$衣衫褴褛的$常数(列表(列表(“所以”,“长”),列表(“谢谢”,“对”,“所有”,“的”,“鱼”)))特遣部队$添加(数字,3.)
# #特遣部队。RaggedTensor(values=张量("Add_1:0", shape=(8,), dtype=float32), row_partitions =张量("RaggedConstant/ raggedfromrow劈裂/ row_劈裂:0",shape=(6,), dtype=int64))
## Tensor("RaggedReduceMean/truediv:0", shape=(5,), dtype=float32)
特遣部队$concat(列表(数字,列表(列表(5,3.))),轴=0 l)
# #特遣部队。RaggedTensor(values=Tensor("RaggedConcat/concat:0", shape=(10,), dtype=float32), row_partitions =Tensor("RaggedConcat/concat_1:0", shape=(7,), dtype=int64))
特遣部队$瓷砖(数字,列表(1 l, 2 l))
# #特遣部队。RaggedTensor(values=Tensor("RaggedTile/Tile:0", shape=(?,), dtype=float32), row_partitions =Tensor("RaggedTile/concat_1:0", shape=(6,), dtype=int64))
# #特遣部队。RaggedTensor(values=Tensor("Substr_1:0", shape=(7,), dtype=string), row_fragments =Tensor("RaggedConstant_1/ raggedfromrowfragments / row_fragments:0", shape=(3,), dtype=int64))
还有许多方法和操作是针对不规则张量的,包括工厂方法、转换方法和值映射操作。
和普通张量一样,你可以使用r风格的索引来访问不规则张量的特定切片。有关更多信息,请参见关于索引在下面。
## Tensor("RaggedGetItem/strided_slice_5:0", shape=(4,), dtype=float32)
# #特遣部队。RaggedTensor(values=Tensor("RaggedGetItem_1/GatherV2:0", shape=(?,), dtype=float32), row_partitions =Tensor("RaggedGetItem_1/RaggedRange:0", shape=(6,), dtype=int64))
##警告:负数在subsetting tensorflow张量时被python样式解释。(他们从后面计数来选择物品)。要关闭此警告,请设置'options(tensorflow.extract. txt)。warn_negatives_pythonic = FALSE) '
# #特遣部队。RaggedTensor(values=Tensor("RaggedGetItem_2/GatherV2:0", shape=(?,), dtype=float32), row_partitions =Tensor("RaggedGetItem_2/RaggedRange:0", shape=(6,), dtype=int64))
就像普通张量一样,您可以使用Python算术和比较操作符来执行elementwise操作。有关更多信息,请参阅下面的重载操作符一节。
# #特遣部队。RaggedTensor(values=Tensor("Add_3:0", shape=(8,), dtype=float32), row_劈裂=张量("RaggedConstant/ raggedfromrow劈裂/ row_劈裂:0",shape=(6,), dtype=int64))
# #特遣部队。RaggedTensor(values=张量("Add_5:0", shape=(?,), dtype=float32), row_partitions =张量("RaggedTile_1/concat_1:0", shape=(6,), dtype=int64))
如果你需要执行一个elementwise转换到RaggedTensor的值,你可以使用tf衣衫褴褛map_flat_values美元
,它接受一个函数加上一个或多个参数,并应用该函数转换RaggedTensor的值。
# #特遣部队。RaggedTensor(values=张量("Add_6:0", shape=(8,), dtype=float32), row_partitions =张量("RaggedConstant/ raggedfromrowpartitions / row_partitions:0", shape=(6,), dtype=int64))
构造粗糙张量
构造粗糙张量最简单的方法是用tf衣衫褴褛的美元不变
,以建立RaggedTensor
对应一个给定的嵌套列表
:
句子< -特遣部队$衣衫褴褛的$常数(列表(列表(“我们”,“构建”,“一些”,“衣衫褴褛”,“张量”,“啊!”),列表(“我们”,“可以”,“使用”,“tf.ragged.constant”,“。”)))
段落< -特遣部队$衣衫褴褛的$常数(列表(列表(列表(“我”,“有”,“一个”,“猫”),列表(“他”,“名字”,“是”,“垫”)),列表(列表(“做”,“你”,“想要”,”到“,“来”,“访问”),列表(“我”,“免费”,“明天”))))段落
# # tf.RaggedTensor(值=特遣部队。RaggedTensor(值=张量("RaggedConstant_4/values:0", shape=(17,), dtype=string), row_splits=Tensor("RaggedConstant_4/RaggedFromRowSplits/row_splits:0", shape=(5,), dtype=int64)), row_splits=Tensor("RaggedConstant_4/RaggedFromRowSplits_1/row_splits:0", shape=(3,), dtype=int64))
粗糙张量也可以通过对平面来构造值张量与row-partitioning张量表明这些值应该如何划分成行,使用工厂类方法,如tf RaggedTensor from_value_rowids美元
,tf RaggedTensor from_row_lengths美元
,tf RaggedTensor from_row_splits美元
.
tf RaggedTensor from_value_rowids美元
如果您知道每个值属于哪一行,那么您就可以构建一个RaggedTensor
使用一个value_rowids
row-partitioning张量:
特遣部队$RaggedTensor$from_value_rowids(值=as.integer(c(3.,1,4,1,5,9,2,6)),value_rowids =as.integer(c(0,0,0,0,2,2,2,3.)))
# #特遣部队。RaggedTensor(values=Tensor("RaggedFromValueRowIds/values:0", shape=(8,), dtype=int32), row_劈裂=Tensor("RaggedFromValueRowIds/concat:0", shape=(5,), dtype=int64))
tf.RaggedTensor.from_row_lengths
如果你知道每一行有多长,那么你可以用arow_lengths
row-partitioning张量:
特遣部队$RaggedTensor$from_row_lengths(值=as.integer(c(3.,1,4,1,5,9,2,6)),row_lengths =as.integer(c(4,0,3.,1)))
# #特遣部队。RaggedTensor(values=Tensor(" raggedfromrowlength/ values:0", shape=(8,), dtype=int32), row_partitions =Tensor(" raggedfromrowlength/ concat:0", shape=(5,), dtype=int64))
tf.RaggedTensor.from_row_splits
如果知道每一行开始和结束的索引,则可以使用row_splits
row-partitioning张量:
特遣部队$RaggedTensor$from_row_splits(值=as.integer(c(3.,1,4,1,5,9,2,6)),row_splits =as.integer(c(0,4,4,7,8)))
# #特遣部队。RaggedTensor(values=Tensor(" raggedfromrowfragments /values:0", shape=(8,), dtype=int32), row_fragments =Tensor(" raggedfromrowfragments / row_fragments:0", shape=(5,), dtype=int64))
看到特遣部队。RaggedTensor
类文档,以获取工厂方法的完整列表。
你可以在不规则张量中存储什么
与正常张量
S, a中的值RaggedTensor
必须具有相同的类型;并且这些值必须都在相同的嵌套深度排名的张量):
特遣部队$衣衫褴褛的$常数(列表(列表(“嗨”),列表(“如何”,“是”,“你”)))# ok: type=string, rank=2
# #特遣部队。RaggedTensor(values=Tensor("RaggedConstant_5/values:0", shape=(4,), dtype=string), row_fragments =Tensor("RaggedConstant_5/ raggedfromrowfragments / row_fragments:0", shape=(3,), dtype=int64))
特遣部队$衣衫褴褛的$常数(列表(列表(“一”,“两个”),列表(3.,4)))# bad:多种类型
特遣部队$衣衫褴褛的$常数(列表(“一个”,列表(“B”,“C”)))# bad:多个嵌套深度
这是对TensorFlow中的不规则张量的一个简单介绍。参见完整教程(Python版本)在这里.