PaddleNLP icon indicating copy to clipboard operation
PaddleNLP copied to clipboard

AttributeError: 'TSVDataset' object has no attribute 'map'

Open bbbxixixixi opened this issue 5 years ago • 4 comments

用simnet做一个语义匹配,自定义数据集,数据里元素已转为数字 仿造PaddleNLP/examples/text_matching/simnet/train.py写 数据读取使用 from paddlenlp.datasets import TSVDataset ds = TSVDataset('xxx.tsv') vocab = Vocab.load_vocabulary('mysimnet_vocab.txt', unk_token='[UNK]', pad_token='[PAD]')

def convert_example(example, vocab, is_test=False): text_a, text_b = example[0], example[1] a_ids = np.array(vocab.to_indices(text_a.split(' ')), dtype="int64") a_seq_len = np.array(len(a_ids), dtype="int64") b_ids = np.array(vocab.to_indices(text_b.split(' ')), dtype="int64") b_seq_len = np.array(len(b_ids), dtype="int64")

if not is_test:
    label = np.array(example[2], dtype="int64")
    return a_ids, b_ids, a_seq_len, b_seq_len, label
else:
    return a_ids, b_ids, a_seq_len, b_seq_len

def create_dataloader(dataset, trans_fn=None, mode='train', batch_size=1, use_gpu=False, batchify_fn=None): if trans_fn: dataset = dataset.map(trans_fn)

if mode == 'train' and use_gpu:
    sampler = paddle.io.DistributedBatchSampler(
        dataset=dataset, batch_size=batch_size, shuffle=True)
else:
    shuffle = True if mode == 'train' else False
    sampler = paddle.io.BatchSampler(
        dataset=dataset, batch_size=batch_size, shuffle=shuffle)
dataloader = paddle.io.DataLoader(
    dataset,
    batch_sampler=sampler,
    return_list=True,
    collate_fn=batchify_fn)
return dataloader

trans_fn = partial(convert_example, vocab=vocab, is_test=False) train_loader = create_dataloader( ds, trans_fn=trans_fn, batch_size=4, mode='train', use_gpu=False, batchify_fn=batchify_fn)

取数据测试的时候 for step, data in enumerate(train_loader): print(step) print(data) break

报了异常 File "D:/pythonCode/fund/med/ak.py", line 43, in create_dataloader dataset = dataset.map(trans_fn) AttributeError: 'TSVDataset' object has no attribute 'map'

bbbxixixixi avatar Mar 17 '21 02:03 bbbxixixixi

您好,现在paddlenlp最新版不推荐使用TSVDataset了。自定义数据集的写法请参考快递单的例子(69行的function) https://github.com/PaddlePaddle/PaddleNLP/blob/release/2.0-rc/examples/information_extraction/waybill_ie/run_bigru_crf.py 之后的版本会移除TSVDataset。

smallv0221 avatar Mar 17 '21 03:03 smallv0221

@bbbxixixixi 可以了解下https://github.com/PaddlePaddle/PaddleNLP/blob/develop/paddlenlp/datasets/experimental/dataset.py MapStyle Dataset和Iterable Style Dataset的用法,目前推荐使用这两种dataset,在自定义拓展上会更加方便。 @smallv0221 正在补充更多文档说明,欢迎持续反馈。

ZeyuChen avatar Mar 17 '21 05:03 ZeyuChen

This issue is stale because it has been open for 60 days with no activity. 当前issue 60天内无活动,被标记为stale。

github-actions[bot] avatar Feb 26 '23 00:02 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale. 当前issue 被标记为stale已有14天,即将关闭。

github-actions[bot] avatar Mar 13 '23 00:03 github-actions[bot]