当前位置:网站首页>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)}>
边栏推荐
- Setup instance of layout manager login interface
- hands-on-data-analysis 第三单元 模型搭建和评估
- 9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
- 虫子 类和对象 中
- Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
- Hands on data analysis unit 3 model building and evaluation
- Stream常用操作以及原理探索
- D check type is pointer
- One article of the quantification framework backtrader read observer
- Sword finger offer 05.58 Ⅱ string
猜你喜欢

character constants

程序员必备,一款让你提高工作效率N倍的神器uTools

Sword finger offer 40.41 Sort (medium)

Intellij IDEA--格式化SQL文件的方法

Codeforces Global Round 21A~D

How to call self written functions in MATLAB

Practice with the topic of bit operation force deduction

登录认证服务

Postman自动化接口测试

Sword finger offer 18.22.25.52 Double pointer (simple)
随机推荐
d的is表达式
ThreadLocal巨坑!内存泄露只是小儿科...
D check type is pointer
Common controls and custom controls
1075 pat judge (25 points)
Freefilesync folder comparison and synchronization software
First k large XOR value problem
[wc2006] director of water management
虫子 STL string 下 练习题
Matlab programming related knowledge
Mathematical design D12 according to string function
Installation tutorial about origin2019
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)
C language | the difference between heap and stack
9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
Relevant knowledge of information entropy
团队管理的最关键因素
Sword finger offer 40.41 Sort (medium)