- TensorFlow 2.0 Quick Start Guide
- Tony Holdroyd
- 203字
- 2021-06-24 16:02:04
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]]>
- 繪制進(jìn)程圖:可視化D++語言(第1冊)
- 機(jī)器學(xué)習(xí)及應(yīng)用(在線實驗+在線自測)
- 并行數(shù)據(jù)挖掘及性能優(yōu)化:關(guān)聯(lián)規(guī)則與數(shù)據(jù)相關(guān)性分析
- Visual FoxPro 6.0數(shù)據(jù)庫與程序設(shè)計
- AWS Certified SysOps Administrator:Associate Guide
- Linux服務(wù)與安全管理
- INSTANT Drools Starter
- 悟透JavaScript
- 激光選區(qū)熔化3D打印技術(shù)
- 生物3D打?。簭尼t(yī)療輔具制造到細(xì)胞打印
- 青少年VEX IQ機(jī)器人實訓(xùn)課程(初級)
- Mastering OpenStack(Second Edition)
- 機(jī)床電氣控制與PLC
- 機(jī)器人制作入門(第4版)
- 微計算機(jī)原理及應(yīng)用