官术网_书友最值得收藏!

Declaring ragged tensors

A ragged tensor is a tensor with one or more ragged dimensions. Ragged dimensions are dimensions that have slices that may have different lengths.

There are a variety of methods for declaring ragged arrays, the simplest being a constant ragged array.

The following example shows how to declare a constant ragged array and the lengths of the individual slices:

ragged =tf.ragged.constant([[5, 2, 6, 1], [], [4, 10, 7], [8], [6,7]])

print(ragged)
print(ragged[0,:])
print(ragged[1,:])
print(ragged[2,:])
print(ragged[3,:])
print(ragged[4,:])

The output is as follows:

<tf.RaggedTensor [[5, 2, 6, 1], [], [4, 10, 7], [8], [6, 7]]>
tf.Tensor([5 2 6 1], shape=(4,), dtype=int32)
tf.Tensor([], shape=(0,), dtype=int32)
tf.Tensor([ 4 10  7], shape=(3,), dtype=int32)
tf.Tensor([8], shape=(1,), dtype=int32)
tf.Tensor([6 7], shape=(2,), dtype=int32)

Note the shape of the individual slices.

A common way of creating a ragged array is by using the tf.RaggedTensor.from_row_splits() method, which has the following signature:

@classmethod
from_row_splits
(
cls
,
values
,
row_splits
,
name
=None
)

Here, values is a list of the values to be turned into the ragged array, and row_splits is a list of the positions where the value list is to be split, so that the values for row ragged[i] are stored in ragged.values[ragged.row_splits[i]:ragged.row_splits[i+1]]:

print(tf.RaggedTensor.from_row_splits(values=[5, 2, 6, 1, 4, 10, 7, 8, 6, 7],
row_splits=[0, 4, 4, 7, 8, 10]))

RaggedTensor is as follows:

<tf.RaggedTensor [[5, 2, 6, 1], [], [4, 10, 7], [8], [6, 7]]>
主站蜘蛛池模板: 云和县| 五华县| 定结县| 封开县| 衡阳市| 宁化县| 阳春市| 寿阳县| 宁安市| 双柏县| 武平县| 嘉兴市| 望城县| 塔河县| 万山特区| 全南县| 法库县| 荆门市| 吉隆县| 镇康县| 宁海县| 和龙市| 当涂县| 九江市| 邵东县| 淮安市| 江达县| 大安市| 册亨县| 张家港市| 丰顺县| 荥经县| 乐清市| 肥东县| 丹江口市| 肃宁县| 赤城县| 霍林郭勒市| 潼南县| 东乌珠穆沁旗| 绿春县|