当前位置:网站首页>Transformers datacollatorwithpadding class
Transformers datacollatorwithpadding class
2022-06-26 14:32:00 【Live up to your youth】
Construction method
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 ' )
stay transfomers in , Defined a DataCollator class , This class is used to package a single element of a dataset into a batch of data .DataCollatorWithPadding Class is DataCollator Class , This class will dynamically fill in the input data when packaging .
Parameters tokenizer Indicates the input word breaker . Parameters padding It can be for bool type ,True Indicates filling ,False Means not to fill ; It can also be a string , Indicates a population policy ,"longest" It means to fill according to the longest data in the input data ,"max_length" Indicates that it is filled to the parameter max_length Set the length ,“do_not_pad" Means not to fill . Parameters pad_to_multiple_of Represents a multiple of the filled data . Parameters return_tensors Indicates the data type returned , It can be for "pt”,pytorch data type ;“tf”,tensorflow data type ;“np”,"numpy" data type .
Examples of use
>>> 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)}>
边栏推荐
- A标签去掉下划线
- RISC-V 芯片架构新规范
- Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
- Sword finger offer 18.22.25.52 Double pointer (simple)
- oracle11g数据库导入导出方法教程[通俗易懂]
- Codeforces Global Round 21A~D
- 通俗语言说BM3D
- A solution to the problem that the display of newff function in neural network cannot be converted from double to struct
- Research on balloon problem
- Electron
猜你喜欢

Installation and uninstallation of MySQL software for windows

Pycharm远程连接服务器来跑代码

9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》

Sword finger offer 09.30 Stack

Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills

Hard (magnetic) disk (II)

Leaflet load day map

From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity

Build your own PE manually from winpe of ADK

Electron
随机推荐
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
Difference between classification and regression
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
Correlation analysis related knowledge
fileinput.js php,fileinput
[cqoi2015] task query system
Sword finger offer 09.30 Stack
[scoi2016] lucky numbers
工作上对金额价格类小数点的总结以及坑
C language | file operation and error prone points
Jianzhi offer 43.47.46.48 dynamic planning (medium)
MHA高可用配合及故障切换
C language | Consortium
Hard (magnetic) disk (I)
ArcGIS secondary development - arcpy delete layer
New specification of risc-v chip architecture
C language | the difference between heap and stack
Lucky numbers in the matrix
Matlab programming related knowledge