Tensor2Tensor 踩坑记录

 

Tensor2Tensor(Transformer)使用方法

安装环境

  1. 安装CUDA 9.0 (一定是9.0,不能是9.2)
  2. 安装TensorFlow (现在是1.8)
  3. 安装Tensor2Tensor

开始使用

  1. 数据预处理:这一步骤是根据自己任务自己编写一些预处理的代码,比如字符串格式化,生成特征向量等操作。
  2. 编写自定义problem:
    1. 编写自定义的problem代码,一定需要在自定义类名前加装饰器。(@registry.registry_problem)
    2. 自定义problem的类名一定是驼峰式命名,py文件名一定是下划线式命名,且与类名对应
    3. 一定需要继承父类problem,t2t已经提供用于生成数据的problem,需要自行将自己的问题人脑分类找到对应的父类,主要定义的父类problem有:(运行 t2t-datagen 可以查看到problem list)
    4. 一定需要在__init__.py文件里导入自定义problem文件
  3. 使用t2t-datagen 将自己预处理后的数据转为t2t的格式化数据集
    1. 运行 t2t-datagen --help 或 t2t-datagen --helpfull
    2. 如果自定义problem代码的输出格式不正确,则此命令会报错
    3. 注意路径
    4. Eg. cd scripts && t2t-datagen --t2t_usr_dir=./ --data_dir=../train_data --tmp_dir=../tmp_data --problem=my_problem
  4. 使用t2t-trainer使用格式化的数据集进行训练
    1. 运行t2t-trainer --help 或 t2t-trainer --helpfull
    2. Eg. cd scripts && t2t-trainer --t2t_usr_dir=./ --problem=my_problem --data_dir=../train_data --model=transformer --hparams_set=transformer_base --output_dir=../output --train_steps=20 --eval_steps=100
    3. 主要的参数有:

      --eval_steps: Number of steps in evaluation. By default, eval will stop after eval_steps or when it runs through the eval dataset once in full, whichever comes first, so this can be a very large number. (default: '100')

      --output_dir: Base output directory for run. (default: '')

      --t2t_usr_dir: Path to a Python module that will be imported. The __init__.py file should include the necessary imports. The imported files should contain registrations, e.g. @registry.register_model calls, that will then be available to the t2t-trainer.

      --keep_checkpoint_every_n_hours: Number of hours between each checkpoint to be saved. The default value 10,000 hours effectively disables it. (default: '10000') (an integer)

      --keep_checkpoint_max: How many recent checkpoints to keep. (default: '20') (an integer)

      --local_eval_frequency: Save checkpoints and run evaluation every N steps during local training. (default: '1000') (an integer)

      --train_steps: The number of steps to run training for. (default: '250000') (an integer)

      --warm_start_from: Warm start from checkpoint.

      --worker_gpu: How many GPUs to use. (default: '1') (an integer)

  5. 使用t2t-decoder对测试集进行预测
    1. 如果想使用某一个checkpoint时的结果时,需要将checkpoint文件中的第一行: model_checkpoint_path: “model.ckpt-xxxx” 的最后的序号修改即可
    2. Eg. cd scripts && t2t-decoder --t2t_usr_dir=./ --problem=my_problem --data_dir=../train_data --model=transformer --hparams_set=transformer_base --output_dir=../output --decode_hparams=”beam_size=5,alpha=0.6” --decode_from_file=../decode_in/test_in.txt --decode_to_file=../decode_out/test_out.txt
    3. 注意路径
  6. 使用t2t-exporter导出训练模型
  7. 分析结果
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值