当前位置:网站首页>transformers DataCollatorWithPadding类
transformers DataCollatorWithPadding类
2022-06-26 13:27:00 【不负韶华ღ】
构造方法
DataCollatorWithPadding(tokenizer:PreTrainedTokenizerBase
padding:typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = True
max_length : typing.Optional[int] = None
pad_to_multiple_of : typing.Optional[int] = None
return_tensors : str = 'pt ' )
在transfomers中,定义了一个DataCollator类,该类用于将数据集的单个元素打包成一批数据。DataCollatorWithPadding类是DataCollator类的一个实现类,该类在打包时将动态填充输入的数据。
参数tokenizer表示输入的分词器。参数padding可以为bool类型,True表示填充,False表示不填充;也可以为字符串,表示填充策略,"longest"表示根据输入数据中最长的数据来进行填充,"max_length"表示填充至参数max_length设置的长度,“do_not_pad"表示不填充。参数pad_to_multiple_of表示填充的数据的倍数。参数return_tensors表示返回的数据类型,可以为"pt”,pytorch数据类型;“tf”,tensorflow数据类型;“np”,"numpy"数据类型。
使用示例
>>> import transformers
>>> import datasets
>>> dataset = datasets.load_dataset("glue", "cola", split="train")
>>> dataset = dataset.map(lambda data: tokenizer(data["sentence"],padding=True), batched=True)
>>> dataset
Dataset({
features: ['sentence', 'label', 'idx', 'input_ids', 'token_type_ids', 'attention_mask'],
num_rows: 8551
})
>>> tokenizer = transformers.BertTokenizer.from_pretrained("bert-base-uncased")
>>> data_collator = transformers.DataCollatorWithPadding(tokenizer,
padding="max_length",
max_length=12,
return_tensors="tf")
>>> dataset = dataset.to_tf_dataset(columns=["label", "input_ids"], batch_size=16, shuffle=False, collate_fn=data_collator)
>>> dataset
<PrefetchDataset element_spec={'input_ids': TensorSpec(shape=(None, None), dtype=tf.int64, name=None), 'attention_mask': TensorSpec(shape=(None, None), dtype=tf.int64, name=None), 'labels': TensorSpec(shape=(None,), dtype=tf.int64, name=None)}>
边栏推荐
- Pycharm远程连接服务器来跑代码
- Caelus - full scene offline mixed Department solution
- d检查类型是指针
- ThreadLocal giant pit! Memory leaks are just Pediatrics
- DOS command
- Mathematical design D12 according to string function
- When drawing with origin, capital letter C will appear in the upper left corner of the chart. The removal method is as follows:
- Hands on data analysis unit 3 model building and evaluation
- 数学建模经验分享:国赛美赛对比/选题参考/常用技巧
- BP neural network for prediction
猜你喜欢

Server create virtual environment run code

windows版MySQL软件的安装与卸载

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

数学建模经验分享:国赛美赛对比/选题参考/常用技巧

8. Ribbon load balancing service call

Sword finger offer 05.58 Ⅱ string

Caelus - full scene offline mixed Department solution

Stream常用操作以及原理探索

Practice with the topic of bit operation force deduction

Sword finger offer 45.61 Sort (simple)
随机推荐
Win10 home vs pro vs enterprise vs enterprise LTSC
"Scoi2016" delicious problem solution
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
Generation and rendering of VTK cylinder
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
vmware部分设置
[path of system analyst] Chapter 15 double disk database system (database case analysis)
hands-on-data-analysis 第三单元 模型搭建和评估
Niuke challenge 48 e speed instant forwarding (tree over tree)
Sword finger offer 18.22.25.52 Double pointer (simple)
Mathematical design D12 according to string function
D check type is pointer
程序员必备,一款让你提高工作效率N倍的神器uTools
From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity
Bug memory management
2021-10-18 character array
Logical operation
Insect operator overloaded a fun
D中不用GC