当前位置:网站首页>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
边栏推荐
- 【iptables&icmp】iptables默认策略中关于icmp协议的说明
- Gateway微服务路由使微服务静态资源加载失败
- adb双击POWER键指令
- Intel Ruixuan A380 graphics card will be launched in China
- 拾光者,云南白药!
- Apache, IIS6, ii7 independent IP host shielding restricts IP access
- 剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
- 访问网站提示:您未被授权查看该页恢复办法
- TensorRT 模型推理优化实现
- Feign远程调用fallback回调失败,无效果
猜你喜欢
[kotlin] basic introduction and understanding of its syntax in Android official documents
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
Gateway microservice routing failed to load microservice static resources
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
2022电工(初级)复训题库及在线模拟考试
2-5基础配置-Win2003增加攻击面
论文阅读:Generative Adversarial Transformers
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
随机推荐
论文阅读:Generative Adversarial Transformers
ARM Development Studio build编译报错
[iptables & ICMP] description of ICMP Protocol in iptables default policy
多快好省,低门槛AI部署工具FastDeploy测试版来了!
嵌入式DSP音频开发
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
Artifact for converting pcap to JSON file: joy (installation)
ADB double click the power key command
RichView TRVStyle ParaStyles
【活动早知道】LiveVideoStack近期活动一览
What are the good practices of cloud cost optimization?
apache、iis6、ii7独立ip主机屏蔽限制ip访问
A16z: metauniverse unlocks new opportunities in game infrastructure
【iptables&icmp】iptables默认策略中关于icmp协议的说明
2022电工(初级)复训题库及在线模拟考试
2-5 basic configuration -win2003 add attack surface
Domain Name System
apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)