【问题标题】:Why is the result different for same dataset in torchtext.legecy.text when i change the position of data in the csv file?为什么当我更改 csv 文件中数据的位置时,torchtext.legecy.text 中相同数据集的结果不同?
【发布时间】:2021-09-06 01:39:34
【问题描述】:

我正在尝试学习 PyTorch NLP 基本文本分类并遵循 Lazy Programmer's Tutorial,我得到了与教程不同的结果,当我尝试更改数据时,我在输出中遇到了奇怪的变化。


import torchtext.legacy.data as ttd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime


data = {
    'label':[0, 1,1 ],
    'data':[   'ham and eggs or just  morning',
            'I like eggs and ham.',
            'Eggs I like!',
          
           
    ]
}

df = pd.DataFrame(data)
df.to_csv('thedata.csv', index=False)
TEXT = ttd.Field(
    sequential =True,
    batch_first =True,
    lower = True,
    tokenize ='spacy',
    pad_first = True
)
LABEL = ttd.Field(
    sequential=False,
    use_vocab=False,
    is_target  =True
)

dataset = ttd.TabularDataset(
    path = 'thedata.csv',
    format ='csv',
    skip_header=True,
    fields = [
              ('label', LABEL),
              ('data',TEXT)
    ]
)
train_dataset, test_dataset = dataset.split()
TEXT.build_vocab(train_dataset,)
vocab = TEXT.vocab
vocab.stoi

这是我的第一种代码,在数据中,如果你看到我在索引 1 中使用了“'ham and eggs or just morning'”,那么在运行代码之后,最后当我运行 vocab.stoi ,我得到以下输出。 The output for the code.


import torchtext.legacy.data as ttd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime


data = {
    'label':[0, 1,1 ],
    'data':[   
            'I like eggs and ham.',
            'Eggs I like!',
'ham and eggs or just  morning',
          
           
    ]
}

df = pd.DataFrame(data)
df.to_csv('thedata.csv', index=False)
TEXT = ttd.Field(
    sequential =True,
    batch_first =True,
    lower = True,
    tokenize ='spacy',
    pad_first = True
)
LABEL = ttd.Field(
    sequential=False,
    use_vocab=False,
    is_target  =True
)

dataset = ttd.TabularDataset(
    path = 'thedata.csv',
    format ='csv',
    skip_header=True,
    fields = [
              ('label', LABEL),
              ('data',TEXT)
    ]
)
train_dataset, test_dataset = dataset.split()
TEXT.build_vocab(train_dataset,)
vocab = TEXT.vocab
vocab.stoi

现在在第二个代码中,我在第三个索引中更改了数据“'ham and eggs or just morning'”的索引,现在如果我运行代码,那么我会得到 vocab.stoi 的不同输出 output for the second code。 我想知道原因以及 vocab_build 在 PyTorch 中的工作原理。 另外,这是我的第一个问题,如果问题不清楚,请告诉我。

【问题讨论】:

    标签: python pandas nlp pytorch torchtext


    【解决方案1】:

    我认为您应该使用随机种子来为您的所有运行获得相同的结果(以及比较您在模型中所做的更改的结果)。应该获取种子的代码部分是数据集拆分功能。正如documentation 所说,是这样的:

    _seed = 2021
    train_dataset, test_dataset = dataset.split(random_state=_seed)
    

    【讨论】:

      猜你喜欢
      • 2023-02-11
      • 1970-01-01
      • 2023-02-26
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      相关资源
      最近更新 更多