ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

tensorflow/models NLP 训练数据怎么在离线预处理脚本与 TFDS 在线读取之间选型?

tensorflow/models NLP 训练数据怎么在离线预处理脚本与 TFDS 在线读取之间选型? tensorflow/models NLP 训练数据怎么在离线预处理脚本与 TFDS 在线读取之间选型【免费下载链接】modelsModels and examples built with TensorFlow项目地址: https://gitcode.com/GitHub_Trending/mode/models在 tensorflow/models 仓库的official/nlpTF-NLP里训练 BERT 等预训练模型时输入数据有两条被官方文档支持的供给方式一是用 Python 预处理脚本把原始语料离线 tokenize、构造tf.Example再落盘训练时通过DataConfig.input_path读文件二是直接指定 TFDS 数据集由 input_reader.py 在 tf.data 输入管线内用 TF.Text 完成 tokenize 和预处理。数据加工文档把这两种方式描述为“flexible ways to provide training data to the input pipeline”并且二者最终都收敛到同一个DataConfig对象上互斥使用。本文给出两条路径的完整操作步骤、可直接复用的命令以及依据仓库文档可核对的选型判断点。两条路径的共同落点DataConfig无论选哪条路径数据配置都写在实验配置YAML 或 Python factory的task.train_data/task.validation_data里类型是 config_definitions.py 中的DataConfig。与选型直接相关的字段input_path离线预处理产出的文件路径。可以是单个路径/通配符、逗号分隔的多路径a, b, c、列表或上述三者的字典用于多数据集混合。tfds_nameTFDS 数据集名指定了tfds_name时不能同时指定input_path。tfds_split从 TFDS 加载哪个 split指定tfds_name时为必填。tfds_data_dirTFDS 数据读写的目录。tfds_as_supervised、tfds_skip_decoding_featureTFDS 专属的加载行为开关。InputReader在初始化时强制这条互斥规则input_reader.pyif params.input_path and params.tfds_name: raise ValueError(At most one of input_path and tfds_name can be specified, but got %s and %s. % ...)也就是说“同时写input_path和tfds_name”会被直接拒绝选型必须在配置阶段二选一。路径一离线预处理脚本 input_path 读 tfrecord适合语料来自自定义来源、或想沿用原 BERT 论文 Wiki Books 配方的场景。pretrain.md给出的完整流程是第 1 步跑预处理脚本生成训练文件。脚本是 create_pretraining_data.py文档中的示例命令$WORKING_DIR、$BERT_DIR为文档示例中的环境变量需替换为你自己的磁盘位置export WORKING_DIRlocal disk or cloud location export BERT_DIRlocal disk or cloud location python models/official/nlp/data/create_pretraining_data.py \ --input_file$WORKING_DIR/input/input.txt \ --output_file$WORKING_DIR/output/tf_examples.tfrecord \ --vocab_file$BERT_DIR/wwm_uncased_L-24_H-1024_A-16/vocab.txt \ --do_lower_caseTrue \ --max_seq_length512 \ --max_predictions_per_seq76 \ --masked_lm_prob0.15 \ --random_seed12345 \ --dupe_factor5关键参数均来自脚本内abslflag 定义--input_file/--output_file两者都是必填项mark_flag_as_required--max_seq_length文档特别强调它必须与你后续预训练时指定的序列长度参数一致--tokenizationWordPieceCanonical BERT或SentencePieceALBERT配套--vocab_file或--sp_model_file--do_whole_word_mask、--max_ngram_sizen-gram 掩码要求同时设置--do_whole_word_maskTrue--masked_lm_prob默认 0.15、--short_seq_prob默认 0.1、--dupe_factor默认 10、--gzip_compress、--use_v2_feature_names。微调数据另有 create_finetuning_data.py用于分类/检索/序列标注等任务的离线构造。第 2 步把产出路径写进实验配置。以 wiki_books_pretrain.yaml 为例train_data.input_path形如input_path: [Your processed wiki data path]*,[Your processed books data path]*方括号内容是配置里预留的占位符需替换为你第 1 步的实际输出路径前缀文档说明当数据有多个 shard 时可以用*通配符把多个文件一次纳入。同时把seq_length等掩码相关超参与预处理参数对齐该 yaml 中seq_length: 512、max_predictions_per_seq: 76与上面的脚本参数一致。第 3 步启动训练。运行bert/pretraining实验pretraining_experiments.py文档示例命令export OUTPUT_DIRgs://some_bucket/my_output_dir export PARAMS$PARAMS,runtime.distribution_strategytpu python3 train.py \ --experimentbert/pretraining \ --modetrain_and_eval \ --model_dir$OUTPUT_DIR \ --config_fileconfigs/models/bert_en_uncased_base.yaml \ --config_fileconfigs/experiments/wiki_books_pretrain.yaml \ --tpu${TPU_NAME} \ --params_override$PARAMSpretrain.md中的gs://some_bucket/my_output_dir、${TPU_NAME}是文档示例值替换为你自己的输出目录与 TPU 实例名。train.py 对experiment、mode、model_dir三个 flag 做了mark_flags_as_required缺了任何一个都会直接报错。路径二TFDS 在线读取 TF.Text 管线内预处理input_reader.py内置了 TFDS 读取路径_read_tfds配合 wiki_tfds_pretrain.yaml数据在 tf.data 管线内直接 tokenize不需要先落盘。该 yaml 的关键字段tfds_name: wikipedia/20201201.en tfds_split: train vocab_file_path: Please provide the vocab file path.vocab_file_path是明确留给读者填写的占位符替换为你模型的词表路径。这条路径在 pretraining_experiments.py 中对应bert/text_wiki_pretraining实验内部使用pretrain_text_dataloader.BertPretrainTextDataConfig其数据管线pretrain_text_dataloader.py用tf_text.BertTokenizer、tf_text.WaterfallTrimmer、tf_text.RegexSplitter、tf_text.mask_language_model等算子在 GPU/TPU 上完成切分、拼接与掩码不依赖本地预处理产物。文档示例的启动命令$OUTPUT_DIR、$BERT_DIR、${TPU_NAME}均为文档示例变量替换后使用export OUTPUT_DIRgs://some_bucket/my_output_dir export BERT_DIR~/cased_L-12_H-768_A-12 export PARAMS$PARAMS,task.validation_data.vocab_file_path$BERT_DIR/vocab.txt export PARAMS$PARAMS,task.train_data.vocab_file_path$BERT_DIR/vocab.txt export PARAMS$PARAMS,runtime.distribution_strategytpu python3 train.py \ --experimentbert/text_wiki_pretraining \ --modetrain_and_eval \ --model_dir$OUTPUT_DIR \ --config_fileconfigs/experiments/wiki_tfds_pretrain.yaml \ --tpu${TPU_NAME} \ --params_override$PARAMS注意 pretrain.md 明确标注该示例“only wikipedia english corpus is used”即官方 TFDS 示例只覆盖维基百科英文语料。选型判断点文档没有给出通用“优劣”结论但提供了以下可核对的判断依据按你的实际情况对号入座判断维度离线预处理脚本路径TFDS 在线读取路径语料来源自定义原始文本Wiki dump、BookCorpus 或自有语料可组合多来源如input_path字典做命名混合语料已是 TFDS 数据集文档示例为wikipedia/20201201.entokenize 时机离线完成产出tf.Exampleproto 文件tfrecord在线完成TF.Text 算子在 tf.data 管线内 tokenize掩码/预处理控制参数masked_lm_prob、short_seq_prob、dupe_factor、whole-word/n-gram 掩码等在脚本 flag 中控制由BertPretrainTextDataConfig及 yaml 字段控制如use_whole_word_masking: true与 TPU 动态序列配合pretrain_dynamic_dataloader.py面向 tokenized 数据集配合--enable_tf_data_service与全局 bucketizing文档报告在典型文本上相对静态定长输入可获得 50%–90% 的训练加速文档的 TFDS 示例未包含动态序列路径数据读取细节文件级 shuffle/shard按input_path通配符匹配tfds_name以mldataset.开头时走tfds.load否则走tfds.builder当 split 的 shard 数小于输入管线数时先整读再在 host memory 里 shard归纳成两条可执行规则语料不是 TFDS 数据集、或需要自定义多源混合与掩码配方→ 走离线脚本把产出写进input_path语料在 TFDS 中、希望省去本地预处理步骤文档称 TFDS 路径是为 “convenience and consolidation” 提供统一读取→ 走tfds_name/tfds_split用 TF.Text 在管线内处理。另外data_processing.md 指出 TPU 训练时整个 DataLoader 的load方法会运行在 TPU worker 上函数内不能访问 task 属性等外部资源两条路径的 DataLoader 都要满足这条约束。处理原始文本特征时可参考 sentence_prediction_dataloader.pyGLUE 微调 TFDS 原始文本和 pretrain_dynamic_dataloader.pytokenized 数据集 tf.data service作为实现范本。验证与错误判定以下报错和日志都来自仓库源码可用于逐步确认配置是否生效预处理产出确认create_pretraining_data.py在写文件前会输出日志*** Writing to output files ***并逐个打印输出路径脚本 main 函数。看到输出文件列表、且磁盘/存储中出现对应tfrecord文件说明第 1 步完成。路径匹配检查input_path里的通配符若一个文件都匹配不到match_files会抛出ValueError: pattern does not match any files.input_reader.py。出现该错误说明预处理产物路径写错或 shard 命名对不上先回到第 1 步核对输出路径。TFDS 配置完整性指定了tfds_name却没指定tfds_split时InputReader抛出ValueError: tfds_name is ..., but tfds_split is not specified.同时指定input_path与tfds_name时抛出At most one of input_path and tfds_name can be specified。sentence_prediction_dataloader.py中还有同样的成对校验逻辑tfds_name与tfds_split必须同时给出、与input_path互斥。训练入口检查train.py必填--experiment、--mode、--model_dirTPU 场景通过--params_override传入runtime.distribution_strategytpu。训练能否正常进入 loop 即为该路径配置成立的最终信号。限制与边界文档示例中的 TFDS 预训练仅覆盖维基百科英文语料换成其他语料时tfds_name/tfds_split是否可用取决于 TFDS 侧是否有对应数据集仓库文档未提供更多现成示例。tfds_data_dir是 TFDS 数据“读/写”目录DataConfig字段说明首次从 TFDS 拉取数据时会占用该目录空间选择时注意存储位置。文件数少于输入管线数时input_reader.py会警告The number of files ... is less than the number of input pipelines ... Please consider sharding your data into more files.input_reader.py提示把离线预处理产物切分更多 shard。共享 tf.data service 等高级特性enable_shared_tf_data_service_between_parallel_trainers等属于跨并行 trainer 调参场景与本场景的基础选型无关这里不展开。【免费下载链接】modelsModels and examples built with TensorFlow项目地址: https://gitcode.com/GitHub_Trending/mode/models创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表