首页 > 解决方案 > FastAI 关于使用 TextList 加载数据的问题

问题描述

我的最终目标是使用 FastAI 实现 ULMFit 来预测灾难推文(作为Kaggle 竞赛的一部分)我想做的是从数据框中读取推文。但由于我不知道的原因,我被困在数据加载阶段。我根本无法使用以下方法这样做 -

from fastai.text.all import *
train= pd.read_csv('../input/nlp-getting-started/train.csv')

dls_lm = (TextList.from_df(path,train,cols='text',is_lm=True)
            .split_by_rand_pct(0.1)
            #.label_for_lm()           
            .databunch(bs=64))

此行抛出 - NameError: name 'TextList' is not defined。

我可以使用以下代码解决此问题 -

dls_lm = DataBlock(
        blocks=TextBlock.from_df('text', is_lm=True),
        get_x=ColReader('text'), 
        splitter=RandomSplitter(0.1) 
    # using only 10% of entire comments data for validation inorder to learn more
)
dls_lm = dls_lm.dataloaders(train, bs=64, seq_len=72)

为什么这行得通,而不是以前的方法?

笔记本链接供参考。

标签: machine-learningdeep-learningfast-ai

解决方案


你运行的是哪个版本的 fastai?

import fastai
print(fastai.__version__)

TextList 类来自 FastAI v1,但在我看来,您的导入路径适用于 Fastai v2,而在 v2 中,TextList 使用https://docs.fast.ai/text.data.html#TextBlock更改(这就是它工作的原因使用 Datablock 部分是处理此问题的好方法)


推荐阅读