Skip to content

examples/language_translation/src/data.py Code iterators will fail. #1405

@Zeping-Jian

Description

@Zeping-Jian

Your issue may already be reported!
Please search on the issue tracker before creating one.

Context

  • Pytorch version:
  • Operating System and version:
    训练的数据无法到达有效的效果

Your Environment

  • Installed using source? [yes/no]: no
  • Are you planning to deploy it using docker container? [yes/no]: no
  • Is it a CPU or GPU environment?: cpu + gpu
  • Which example are you using: examples/language_translation/src/data.py
  • Link to code or data to repro [if any]:

Expected Behavior

训练的模型无法到达有效的效果
The trained model failed to achieve effective results.

Current Behavior

examples/language_translation/src/data.py 代码迭代器会失效
train_iterator 这个迭代器第二次使用会失效 是么

Possible Solution

是的,你观察得很对!train_iterator 在第二次使用时确实会失效。

让我检查一下代码中 train_iterator 被使用的位置:

Read , lines 36 to 60

没错!这是一个经典的迭代器耗尽问题:

问题分析:

  1. 第 36 行创建了 train_iterator
  2. 第 45 行第一次使用它构建 src_vocab - 迭代器被耗尽
  3. 第 51 行第二次使用它构建 tgt_vocab - 此时迭代器已空,无法再遍历数据

解决方案:
需要在第二次使用前重新创建迭代器。让我修复这个问题:

Using "Replace String in File"

现在还需要在创建 DataLoader 之前再次重新创建迭代器,因为构建 tgt_vocab 后它又被耗尽了:

Read , lines 90 to 100

Using "Replace String in File"

已修复!

问题总结:

  • Python 的迭代器是一次性的,遍历完后就耗尽了,无法再次使用
  • 原代码中 train_iterator 被使用了 3 次:
    1. 构建 src_vocab (第45行)
    2. 构建 tgt_vocab (第54行) - 此时已失效
    3. 创建 train_dataloader (第96行) - 此时已失效

修复方法:
在每次重新使用前调用 Multi30k() 重新创建迭代器,确保每次都有新鲜的数据流。

Made changes.

Steps to Reproduce

...

Failure Logs [if any]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions