Pytorch-DataLoader的使用

Pytorch-DataLoader的使用原文连接:http://chenhao.space/post/d313d236.htmlpytorch-DataLoader的使用importtorchimporttorch.utils.dataasData#[1,1,1]相当于一句话的wordembedding,这个tensor中含有三句话x=torch.tensor([[1,1,1],[2,2,2…

大家好,又见面了,我是你们的朋友全栈君。

原文连接: http://chenhao.space/post/d313d236.html

pytorch-DataLoader的使用

import torch
import torch.utils.data as Data

# [1, 1, 1]相当于一句话的word embedding,这个tensor中含有三句话
x = torch.tensor([[1, 1, 1], [2, 2, 2,], [3, 3, 3], [4, 4, 4], [5, 5, 5,], [6, 6, 6],[7, 7, 7], [8, 8, 8,], [9, 9, 9], [10, 10, 10]])
# [1, 2, 3]分别是这三句话的标签
y = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

torch_dataset = Data.TensorDataset(x, y)

# dataset:Dataset类型,从其中加载数据 
# batch_size:int,可选。每个batch加载多少样本 
# shuffle:bool,可选。为True时表示每个epoch都对数据进行洗牌 
# sampler:Sampler,可选。从数据集中采样样本的方法。 
# num_workers:int,可选。加载数据时使用多少子进程。默认值为0,表示在主进程中加载数据。 
# collate_fn:callable,可选。 
# pin_memory:bool,可选 
# drop_last:bool,可选。True表示如果最后剩下不完全的batch,丢弃。False表示不丢弃。
loader = Data.DataLoader(torch_dataset, batch_size=3, shuffle=True, num_workers=0)

data = iter(loader)

n = len(y)//3 if len(y)%3 == 0 else len(y)//3 + 1   # batch的数量
for i in range(n):   
    print(next(data))
[tensor([[5, 5, 5],
        [9, 9, 9],
        [8, 8, 8]]), tensor([5, 9, 8])]
[tensor([[10, 10, 10],
        [ 2,  2,  2],
        [ 7,  7,  7]]), tensor([10,  2,  7])]
[tensor([[6, 6, 6],
        [1, 1, 1],
        [3, 3, 3]]), tensor([6, 1, 3])]
[tensor([[4, 4, 4]]), tensor([4])]

for epoch in range(5):   # 训练所有数据5次
    i = 0
    for sentence, label in loader:
        i += 1
        print('Epoch:{} | num:{} | sentence:{} | label:{}'.format(epoch,i,sentence,label))
Epoch:0 | num:1 | sentence:tensor([[10, 10, 10],
        [ 2,  2,  2],
        [ 8,  8,  8]]) | label:tensor([10,  2,  8])
Epoch:0 | num:2 | sentence:tensor([[7, 7, 7],
        [9, 9, 9],
        [5, 5, 5]]) | label:tensor([7, 9, 5])
Epoch:0 | num:3 | sentence:tensor([[6, 6, 6],
        [4, 4, 4],
        [1, 1, 1]]) | label:tensor([6, 4, 1])
Epoch:0 | num:4 | sentence:tensor([[3, 3, 3]]) | label:tensor([3])
Epoch:1 | num:1 | sentence:tensor([[9, 9, 9],
        [3, 3, 3],
        [4, 4, 4]]) | label:tensor([9, 3, 4])
Epoch:1 | num:2 | sentence:tensor([[8, 8, 8],
        [6, 6, 6],
        [5, 5, 5]]) | label:tensor([8, 6, 5])
Epoch:1 | num:3 | sentence:tensor([[ 1,  1,  1],
        [10, 10, 10],
        [ 2,  2,  2]]) | label:tensor([ 1, 10,  2])
Epoch:1 | num:4 | sentence:tensor([[7, 7, 7]]) | label:tensor([7])
Epoch:2 | num:1 | sentence:tensor([[4, 4, 4],
        [6, 6, 6],
        [7, 7, 7]]) | label:tensor([4, 6, 7])
Epoch:2 | num:2 | sentence:tensor([[10, 10, 10],
        [ 8,  8,  8],
        [ 5,  5,  5]]) | label:tensor([10,  8,  5])
Epoch:2 | num:3 | sentence:tensor([[3, 3, 3],
        [2, 2, 2],
        [9, 9, 9]]) | label:tensor([3, 2, 9])
Epoch:2 | num:4 | sentence:tensor([[1, 1, 1]]) | label:tensor([1])
Epoch:3 | num:1 | sentence:tensor([[7, 7, 7],
        [5, 5, 5],
        [3, 3, 3]]) | label:tensor([7, 5, 3])
Epoch:3 | num:2 | sentence:tensor([[10, 10, 10],
        [ 1,  1,  1],
        [ 6,  6,  6]]) | label:tensor([10,  1,  6])
Epoch:3 | num:3 | sentence:tensor([[9, 9, 9],
        [8, 8, 8],
        [4, 4, 4]]) | label:tensor([9, 8, 4])
Epoch:3 | num:4 | sentence:tensor([[2, 2, 2]]) | label:tensor([2])
Epoch:4 | num:1 | sentence:tensor([[ 5,  5,  5],
        [ 7,  7,  7],
        [10, 10, 10]]) | label:tensor([ 5,  7, 10])
Epoch:4 | num:2 | sentence:tensor([[9, 9, 9],
        [3, 3, 3],
        [4, 4, 4]]) | label:tensor([9, 3, 4])
Epoch:4 | num:3 | sentence:tensor([[2, 2, 2],
        [8, 8, 8],
        [1, 1, 1]]) | label:tensor([2, 8, 1])
Epoch:4 | num:4 | sentence:tensor([[6, 6, 6]]) | label:tensor([6])
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146258.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SpringBoot集成Redis,并自定义对象序列化

    SpringBoot集成Redis,并自定义对象序列化SpringBoot项目使用redis非常简单,pom里面引入redis的场景启动器,在启动类上加@EnableCaching注解,项目启动会自动匹配上redis,这样项目中就可以愉快地使用了,使用方法:要么使用@Cacheable一类的注解自动缓存,要么使用RedisTemplate手动缓存。(前提是你的本机或者是远程主机要先搭好redis环境)虽然SpringBoot好用,但这里也有好多…

    2022年9月22日
    3
  • 渗透测试工具——SET「建议收藏」

    渗透测试工具——SET「建议收藏」社会工程学使用计谋、假情报或人际关系去获得利益和其他敏感信息。 攻击对象一-一人一-秘密信息的保存者,信息安全链中最薄弱的环节。 利用受害者的本能反应、好奇心、信任、贪婪等心理弱点进行欺骗、伤害。常见的社会工程学攻击方式环境渗透:对特定的环境进行渗透,是社会工程学为了获得所需的情报或敏感信息经常采用的手段之一。社会工程学攻击者通过观察目标对电子邮件的响应速度、重视程度以及可能提供的相关资料,比如一个人的姓名、生日、ID电话号码、管理员的IP地址、邮箱等,通过这些收集信息来判断目标的网

    2022年8月12日
    7
  • [精华] kingate代理服务器指南

    [精华] kingate代理服务器指南

    2021年8月19日
    43
  • python怎么保留四位小数_jq四舍五入取小数点后两位

    python怎么保留四位小数_jq四舍五入取小数点后两位在很多场景的计算中,最终得到的数值例如123.45678,要截取2位小数得到123.45,而不是默认的四舍五入方法得到123.46,如何实现呢?一.小数点后取2位(四舍五入)的方法方法一:round()函数方法二:’%.2f’%f方法方法三:Decimal()函数二.小数点后取2位(四舍五不入)的方法方法一:一.小数点后取2位(四舍五入…

    2022年8月12日
    8
  • StretchBlt用法[通俗易懂]

    StretchBlt用法[通俗易懂]首先定义protected: BITMAPbmp;其次实现///////////////////////////////////////////////////////////////////////////////CExamineViewdrawingvoidCExamineView::OnDraw(CDC*pDC){ CExamineDoc*pDoc=GetDo

    2025年6月13日
    2
  • 页面浮动广告举例_html向下浮动代码

    页面浮动广告举例_html向下浮动代码今天做网页两侧浮动广告图片,用js代码是实现,下面是一个*.js文件,在需要用到的网页加一句代码language=”javascript”src=”*.js”>即可使用,我可弄好的,分享给有需要朋友。leftcode1=”href=’http://www.dizhu88.com/’target=’_blank’>src=’http://www.dizhu88.com/images/xibao…

    2022年9月16日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号