当前位置:网站首页>Dataloader参数collate_fn的使用
Dataloader参数collate_fn的使用
2022-06-28 02:07:00 【xx_xjm】
简而言之,这个参数就是用来设定dataloader最后输出的batch内容;dataloader一次性从dataset得到batch大小的数据,但这些数据本身是分散的,拿图片举例,比如我们batch为8,则我们得到的是8个[3,256,256](256为图片形状,随便设置的)大小的张量,通过collate_fn这个参数转化为形状为[8,3,256,256]的张量作为dataloader的输出。
一般情况下,这个参数是不用设置的,那什么是不一般的情况呢,比如数据长度不同的时候,最明显的拿NLP里面的句子长度举例,每个batch里面的句子长度不一样,如果使用默认的collate_fn方法,就可能报错,这时候就需要自定义collate_fn参数。
而重写这个参数也很简单,就是自定义一个函数,假设我们这里给这个函数取名字叫做my_collate_fn(batch),注意,只能有一个输入变量batch,batch包含了dataset里面getitem返回的所有值。
拿vqa任务来举例子,比如dataset的getitem每次返回一张图片的数据data,label,以及相应的question,answer,如果我们设定dataloader一次性获得8个大小的batch,则此时传入my_collate_fn的变量为一个list,这个list包含8个[data, label, question, answer],如果要使用默认的collate_fn,则要求,这8个[data, label, question, answer]里面,每一个变量的形状都是相同的,就是说8个data的形状相同,8个label的形状相同,8个question的形状,8个answer的形状也相同,这样才可以用默认的collate_fn参数,否则会报错。如果不满足的话就需要自定义collate_fn参数
举例如下,此时自定义的vqa_collate_fn的输入是list为8的变量,我们单独看其中的每个变量的第三个元素,可以发现,第0和第1个变量的第三个元素长度是不相同的,这时候如果使用默认的collate_fn就会报错,而在这里,我们自定义的函数里面,我们知识把每个变量的第三个元素放在一个list里面直接返回:
例子来源于ALBEF

边栏推荐
- PSM summary
- 无代码软件发展简史及未来趋势
- Notepad++--列编辑模式--用法/实例
- 为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
- 访问网站提示:您未被授权查看该页恢复办法
- Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
- 嵌入式DSP音频开发
- Arduino esp8266 web LED control
- How to write concise code? (upper)
- Raspberry pie - environment settings and cross compilation
猜你喜欢

2-5基础配置-Win2003增加攻击面

数字化时代,企业须做好用户信息安全

2-5 basic configuration -win2003 add attack surface

空闲中断无法清除

Get 5 offers after being notified of layoffs

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队

Writing based on stm32
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

暴雨去哪儿?天气预报不准谁的锅?

如何获取GC(垃圾回收器)的STW(暂停)时间?
随机推荐
您的物联网安全性是否足够强大?
Is online stock investment exchange group safe? Is it reliable to open an account for free?
Domain Name System
Gateway微服务路由使微服务静态资源加载失败
Initial linear regression
Writing based on stm32
[iptables & ICMP] description of ICMP Protocol in iptables default policy
Single page application (SPA) hash route and historical API route
Flow based depth generation model
论文阅读:Generative Adversarial Transformers
Yes, it's about water
Tencent games released more than 40 products and projects, including 12 new games
Agileplm exception resolution session
What are the technologies to be mastered in the test? Database design for software testing
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
be fond of the new and tired of the old? Why do it companies prefer to spend 20K on recruiting rather than raise salaries to retain old employees
买股票应该下载什么软件最好最安全?
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
无心剑汉英双语诗004.《剑》