WWW Tensorflow Org Guide Ragged - Tensor
WWW Tensorflow Org Guide Ragged - Tensor
TensorFlow Core
Tutorials Guide Migrate to TF2 TF 1 ↗
filter_list Filter
Batches of variable-length
sequential inputs, such as
sentences or video clips.
print(digits + 3)
<tf.RaggedTensor [[6, 4, 7, 4], [], [8
print(digits + tf.ragged.constant([[1
times_two_plus_one = lambda x: x * 2 +
print(tf.ragged.map_flat_values(times_
digits.to_list()
[[3, 1, 4, 1], [], [5, 9, 2], [6], []
digits.numpy()
sentences = tf.ragged.constant([
["Let's", "build", "some", "ragged
["We", "can", "use", "tf.ragged.co
print(sentences)
paragraphs = tf.ragged.constant([
[['I', 'have', 'a', 'cat'], ['His
[['Do', 'you', 'want', 'to', 'come
])
print(paragraphs)
tf.RaggedTensor.from_value_rowi
ds
print(tf.RaggedTensor.from_value_rowid
values=[3, 1, 4, 1, 5, 9, 2],
value_rowids=[0, 0, 0, 0, 2, 2, 3
tf.RaggedTensor.from_row_length
s
print(tf.RaggedTensor.from_row_lengths
values=[3, 1, 4, 1, 5, 9, 2],
row_lengths=[4, 0, 2, 1]))
tf.RaggedTensor.from_row_splits
print(tf.ragged.constant([["Hi"], ["Ho
<tf.RaggedTensor [[b'Hi'], [b'How', b
print(tf.ragged.constant([[[1, 2], [3
try:
tf.ragged.constant([["one", "two"],
except ValueError as exception:
print(exception)
try:
tf.ragged.constant(["A", ["B", "C"]
except ValueError as exception:
print(exception)
queries = tf.ragged.constant([['Who',
['Pause
['Will'
bigram_buckets = tf.strings.to_hash_bu
bigram_embeddings = tf.nn.embedding_lo
TensorShape([2, None])
The method
tf.RaggedTensor.bounding_shape
can be used to find a tight bounding
shape for a given RaggedTensor :
print(tf.ragged.constant([["Hi"], ["Ho
Ragged vs sparse
A ragged tensor should not be thought
of as a type of sparse tensor. In
particular, sparse tensors are efficient
encodings for tf.Tensor that model
the same data in a compact format;
but ragged tensor is an extension to
tf.Tensor that models an expanded
class of data. This difference is
crucial when defining operations:
Applying an op to a sparse or
dense tensor should always give
the same result.
Applying an op to a ragged or
sparse tensor may give different
results.
ragged_x = tf.ragged.constant([["John
ragged_y = tf.ragged.constant([["fell
print(tf.concat([ragged_x, ragged_y],
sparse_x = ragged_x.to_sparse()
sparse_y = ragged_y.to_sparse()
sparse_result = tf.sparse.concat(sp_in
print(tf.sparse.to_dense(sparse_result
tf.Tensor(
[[b'John' b'' b'' b'fell' b'asleep']
[b'a' b'big' b'dog' b'barked' b'']
[b'my' b'cat' b'' b'is' b'fuzzy']], s
TensorFlow APIs
Keras
tf.keras is TensorFlow's high-level API
for building and training deep learning
models. It doesn't have ragged
support. But it does support masked
tensors. So the easiest way to use a
ragged tensor in a Keras model is to
convert the ragged tensor to a dense
tensor, using .to_tensor() and then
using Keras's builtin masking:
hashed_words.to_tensor()
<tf.Tensor: shape=(4, 8), dtype=int64
array([[940, 203, 668, 387, 790, 320,
[315, 515, 791, 181, 939, 787,
[564, 205, 0, 0, 0, 0,
[820, 180, 993, 739, 0, 0,
tf.keras.Input?
keras_model.compile(loss='binary_cross
keras_model.fit(hashed_words.to_tenso
Epoch 1/5
WARNING: All log messages before absl
I0000 00:00:1724721648.311913 10235
I0000 00:00:1724721648.311945 10235
I0000 00:00:1724721648.311949 10235
I0000 00:00:1724721648.311952 10235
I0000 00:00:1724721648.311955 10235
1/1 ━━━━━━━━━━━━━━━━━━━━ 4s 4s/step -
Epoch 2/5
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 47ms/step
Epoch 3/5
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 44ms/step
Epoch 4/5
1/1 0s 44ms/step
print(keras_model.predict(hashed_words
tf.Example
tf.Example is a standard protobuf
encoding for TensorFlow data. Data
encoded with tf.Example s often
includes variable-length features. For
example, the following code defines a
batch of four tf.Example messages
with different feature lengths:
import google.protobuf.text_format as
def build_tf_example(s):
return pbtext.Merge(s, tf.train.Exam
example_batch = [
build_tf_example(r'''
features {
feature {key: "colors" value {by
feature {key: "lengths" value {i
build_tf_example(r'''
features {
feature {key: "colors" value {by
feature {key: "lengths" value {i
build_tf_example(r'''
features {
feature {key: "colors" value {by
feature {key: "lengths" value {i
build_tf_example(r'''
features {
feature {key: "colors" value {by
feature {key: "lengths" value {i
feature_specification = {
'colors': tf.io.RaggedFeature(tf.s
'lengths': tf.io.RaggedFeature(tf
}
feature_tensors = tf.io.parse_example
for name, value in feature_tensors.ite
print("{}={}".format(name, value))
Datasets