tensorflow 零基础吃透:tf.data 中 RaggedTensor 的核心用法(数据集流水线)

零基础吃透:tf.data中RaggedTensor的核心用法(数据集流水线)

这份内容会拆解 tf.data.Dataset 与 RaggedTensor 结合的四大核心场景------构建数据集、批处理/取消批处理、非规则张量转Ragged批处理、数据集转换,全程用「通俗解释+代码拆解+原理+结果解读」,帮你理解"可变长度数据"在TF输入流水线中的最优处理方式。

核心背景(先理清)

tf.data.Dataset 是TensorFlow的输入流水线核心工具,负责数据加载、预处理、批处理、迭代等全流程;RaggedTensor 则是处理"可变长度数据"的原生类型,两者结合能完美解决"可变长度数据"在流水线中的处理问题(无需补0,保留原生结构)。

先补全示例依赖(确保代码可运行):

python 复制代码
import tensorflow as tf
import google.protobuf.text_format as pbtext

# 先重建之前的feature_tensors(tf.Example解析后的RaggedTensor字典)
def build_tf_example(s):
  return pbtext.Merge(s, tf.train.Example()).SerializeToString()

example_batch = [
  build_tf_example(r'''features {feature {key: "colors" value {bytes_list {value: ["red", "blue"]} } } feature {key: "lengths" value {int64_list {value: [7]} } } }'''),
  build_tf_example(r'''features {feature {key: "colors" value {bytes_list {value: ["orange"]} } } feature {key: "lengths" value {int64_list {value: []} } } }'''),
  build_tf_example(r'''features {feature {key: "colors" value {bytes_list {value: ["black", "yellow"]} } } feature {key: "lengths" value {int64_list {value: [1, 3]} } } }'''),
  build_tf_example(r'''features {feature {key: "colors" value {bytes_list {value: ["green"]} } } feature {key: "lengths" value {int64_list {value: [3, 5, 2]} } } }''')]

feature_specification = {
    'colors': tf.io.RaggedFeature(tf.string),
    'lengths': tf.io.RaggedFeature(tf.int64),
}
feature_tensors = tf.io.parse_example(example_batch, feature_specification)

# 文档中的辅助打印函数
def print_dictionary_dataset(dataset):
  for i, element in enumerate(dataset):
    print("Element {}:".format(i))
    for (feature_name, feature_value) in element.items():
      print('{:>14} = {}'.format(feature_name, feature_value))

场景1:使用RaggedTensor构建数据集

核心逻辑

tf.data.Dataset.from_tensor_slices 是构建数据集的核心方法,对RaggedTensor的支持和普通张量完全一致------按"第一个维度(样本维度)"切分,每个元素对应一个样本的RaggedTensor(保留原始可变长度)。

代码+解析

python 复制代码
# 从RaggedTensor字典构建数据集(feature_tensors是{colors: RaggedTensor, lengths: RaggedTensor})
dataset = tf.data.Dataset.from_tensor_slices(feature_tensors)
# 打印数据集元素
print("=== 构建的RaggedTensor数据集 ===")
print_dictionary_dataset(dataset)

运行结果+解读

复制代码
Element 0:
        colors = [b'red' b'blue']
       lengths = [7]
Element 1:
        colors = [b'orange']
       lengths = []
Element 2:
        colors = [b'black' b'yellow']
       lengths = [1 3]
Element 3:
        colors = [b'green']
       lengths = [3 5 2]
  • 每个Element对应一个样本,colors/lengths保留该样本的原始长度(比如样本1的lengths为空列表,样本3的lengths有3个元素);
  • 对比普通张量:如果是补0的密集张量,样本1的lengths会是[0,0,0](补到最长长度),而RaggedTensor无冗余。

关键原理

from_tensor_slices 对RaggedTensor的切分规则:

  • 只切分最外层的均匀维度(样本维度),内层的不规则维度保持不变;
  • 比如feature_tensors['lengths']是形状[4, None]的RaggedTensor,切分后每个元素是形状[None]的RaggedTensor(单个样本的长度列表)。

场景2:批处理/取消批处理RaggedTensor数据集

2.1 批处理(Dataset.batch)

核心逻辑

Dataset.batch(n)n个连续样本合并成一个批次,批次内的RaggedTensor会自动合并为更高维的RaggedTensor(批次维度是均匀的,内部维度仍不规则)。

代码+解析
python 复制代码
# 按2个样本为一批进行批处理
batched_dataset = dataset.batch(2)
print("\n=== 批处理后的RaggedTensor数据集(batch=2) ===")
print_dictionary_dataset(batched_dataset)
运行结果+解读
复制代码
Element 0:
        colors = <tf.RaggedTensor [[b'red', b'blue'], [b'orange']]>
       lengths = <tf.RaggedTensor [[7], []]>
Element 1:
        colors = <tf.RaggedTensor [[b'black', b'yellow'], [b'green']]>
       lengths = <tf.RaggedTensor [[1, 3], [3, 5, 2]]>
  • 每个Element是一个批次(2个样本),colors/lengths变成二维RaggedTensor(第一维是批次内的样本索引,第二维是样本内的元素);
  • 对比密集张量批处理:无需补0到"批次内最长长度",比如批次0的colors中,第一个样本2个元素、第二个样本1个元素,直接保留原始长度。

2.2 取消批处理(Dataset.unbatch)

核心逻辑

Dataset.unbatch() 把批处理后的数据集拆回"单个样本"的形式,完全恢复批处理前的结构。

代码+解析
python 复制代码
# 取消批处理
unbatched_dataset = batched_dataset.unbatch()
print("\n=== 取消批处理后的数据集 ===")
print_dictionary_dataset(unbatched_dataset)
运行结果

和场景1的原始数据集完全一致(4个单个样本,保留原始长度)。

关键对比(Ragged批处理 vs 密集张量批处理)

方式 特点 冗余性
RaggedTensor.batch 合并为高维RaggedTensor,保留原始长度 无冗余
密集张量.batch 补0到批次内最长长度,生成固定形状张量 有冗余

场景3:非Ragged张量(可变长度)转Ragged批处理

核心场景

如果数据集的元素是长度不同的密集张量 (不是RaggedTensor),直接用batch会报错(长度不匹配),此时用dense_to_ragged_batch把每个批次转成RaggedTensor,避免补0。

代码+解析

python 复制代码
# 步骤1:构建"长度不同的密集张量"数据集
# 原始数据:[1,5,3,2,8] → 每个元素用tf.range生成长度不同的密集张量
non_ragged_dataset = tf.data.Dataset.from_tensor_slices([1, 5, 3, 2, 8])
non_ragged_dataset = non_ragged_dataset.map(tf.range)  # 映射后:[0], [0,1,2,3,4], [0,1,2], [0,1], [0-7]

# 步骤2:用dense_to_ragged_batch批处理(每2个样本为一批,转成RaggedTensor)
batched_non_ragged_dataset = non_ragged_dataset.apply(
    tf.data.experimental.dense_to_ragged_batch(2))

# 打印结果
print("\n=== 非Ragged张量转Ragged批处理 ===")
for element in batched_non_ragged_dataset:
  print(element)

运行结果+解读

复制代码
<tf.RaggedTensor [[0], [0, 1, 2, 3, 4]]>
<tf.RaggedTensor [[0, 1, 2], [0, 1]]>
<tf.RaggedTensor [[0, 1, 2, 3, 4, 5, 6, 7]]>
  • 第一批:2个样本[0][0,1,2,3,4] → 合并为二维RaggedTensor;
  • 第二批:2个样本[0,1,2][0,1] → 合并为二维RaggedTensor;
  • 第三批:只剩1个样本[0-7] → 一维RaggedTensor;
  • 核心价值:不用补0,直接按原始长度合并为RaggedTensor,解决"长度不同的密集张量无法直接batch"的问题。

关键原理

tf.data.experimental.dense_to_ragged_batch(n)

  • 每次取n个长度不同的密集张量;
  • 自动将其转换为一个n行的RaggedTensor(每行对应一个样本的原始长度);
  • 替代方案:如果不用这个方法,需要先把每个元素转成RaggedTensor,再batch,步骤更繁琐。

场景4:转换RaggedTensor数据集(Dataset.map)

核心逻辑

Dataset.map 可以对数据集中的每个元素(RaggedTensor)进行任意转换(比如计算均值、生成新的RaggedTensor),TF原生支持RaggedTensor的运算。

代码+解析

python 复制代码
# 定义转换函数:处理每个样本的features字典
def transform_lengths(features):
  return {
      # 计算lengths的均值(空列表的均值为0)
      'mean_length': tf.math.reduce_mean(features['lengths']),
      # 对lengths中的每个值,生成0到该值-1的序列(返回RaggedTensor)
      'length_ranges': tf.ragged.range(features['lengths'])}

# 应用转换
transformed_dataset = dataset.map(transform_lengths)
# 打印结果
print("\n=== 转换后的RaggedTensor数据集 ===")
print_dictionary_dataset(transformed_dataset)

运行结果+解读

复制代码
Element 0:
   mean_length = 7
 length_ranges = <tf.RaggedTensor [[0, 1, 2, 3, 4, 5, 6]]>
Element 1:
   mean_length = 0
 length_ranges = <tf.RaggedTensor []>
Element 2:
   mean_length = 2
 length_ranges = <tf.RaggedTensor [[0], [0, 1, 2]]>
Element 3:
   mean_length = 3
 length_ranges = <tf.RaggedTensor [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1]]>
关键转换逻辑解读
  1. tf.math.reduce_mean(features['lengths'])

    • 样本0的lengths=[7] → 均值=7;
    • 样本1的lengths=[] → 均值=0(TF对空RaggedTensor的reduce_mean默认返回0);
    • 样本2的lengths=[1,3] → 均值=(1+3)/2=2;
    • 样本3的lengths=[3,5,2] → 均值=(3+5+2)/3=3。
  2. tf.ragged.range(features['lengths'])

    • 对lengths中的每个数值L,生成[0,1,...,L-1]的序列;
    • 样本0的lengths=[7] → 生成[0-6] → RaggedTensor[[0,1,2,3,4,5,6]]
    • 样本2的lengths=[1,3] → 生成[0][0,1,2] → RaggedTensor[[0], [0,1,2]]

关键优势

Dataset.map 处理RaggedTensor时:

  • 无需转换为密集张量,直接运算;
  • 所有TF内置运算(reduce_mean/range/concat等)都原生支持RaggedTensor;
  • 转换后的结果仍可保留RaggedTensor结构,无缝接入后续流水线。

核心总结(tf.data+RaggedTensor关键要点)

操作 核心价值 关键API
构建数据集 直接切分RaggedTensor,保留样本原始长度 tf.data.Dataset.from_tensor_slices
批处理 合并为高维RaggedTensor,无冗余补0 Dataset.batch(n)
取消批处理 恢复单个样本的RaggedTensor结构 Dataset.unbatch()
非Ragged批处理 解决长度不同的密集张量无法batch的问题 tf.data.experimental.dense_to_ragged_batch
数据集转换 原生支持RaggedTensor运算,无需转密集张量 Dataset.map(转换函数)

避坑关键

  1. Dataset.from_generator 暂不支持RaggedTensor(文档提示后续会支持),如需生成器构建,需先将RaggedTensor转成密集张量+Mask;
  2. 批处理后的RaggedTensor可直接传入Keras模型(需Input层设置ragged=True);
  3. 所有RaggedTensor的运算都遵循"只处理有效元素"的规则,无冗余计算。

这套组合是TF处理"可变长度数据"(文本、序列特征等)的最优流水线方案,既保证数据结构的原生性,又兼顾流水线的高效性。

相关推荐
老蒋新思维3 小时前
创客匠人 2025 万人峰会深度:AI+IP 信任三角重构知识变现 —— 从单次成交到终身绑定的生态逻辑
大数据·网络·人工智能·tcp/ip·重构·创始人ip·创客匠人
乾元3 小时前
从命令行到自动诊断:构建 AI 驱动的故障树与交互式排障机器人引言
运维·开发语言·网络·人工智能·华为·自动化
老蒋新思维3 小时前
创客匠人峰会总结:私域 AI 化引爆知识变现 —— 创始人 IP 的智能增长新范式
网络·人工智能·网络协议·tcp/ip·重构·创始人ip·创客匠人
clarance20153 小时前
ChatBI王者之争:ThoughtSpot、Databricks、Power BI等五大产品深度对决与选型指南
大数据·人工智能·信息可视化·数据挖掘·数据分析
阿达_优阅达3 小时前
Docusign × ChatGPT:一键完成合同的创建、签署与管理
人工智能·chatgpt·电子签·合同管理·docusign
Arenaschi3 小时前
安卓显示翻转
android·网络·人工智能·笔记·其他
jimmyleeee3 小时前
人工智能基础知识笔记二十六:常用的LLM的网站
人工智能·笔记
程序猿追3 小时前
昇腾NPU实战:Z-Image-Turbo-Fun-Controlnet-Union模型部署与测试全记录
大数据·服务器·人工智能·机器学习
九河云3 小时前
直播电商数字化:用户行为 AI 分析与选品推荐算法平台建设
人工智能·物联网·算法·推荐算法
大千AI助手3 小时前
NaturalQuestions:重塑开放域问答研究的真实世界基准
人工智能·机器学习·rag·大千ai助手·nq数据集·问答qa·自然语言阅读理解