PyTorch小功能之TensorDataset解读
作者:菜鸟向前冲fighting 发布时间:2023-02-26 06:06:27
PyTorch之TensorDataset
TensorDataset 可以用来对 tensor 进行打包,就好像 python 中的 zip 功能。
该类通过每一个 tensor 的第一个维度进行索引。
因此,该类中的 tensor 第一维度必须相等。
from torch.utils.data import TensorDataset
import torch
from torch.utils.data import DataLoader
a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = torch.tensor([44, 55, 66, 44, 55, 66, 44, 55, 66, 44, 55, 66])
train_ids = TensorDataset(a, b)
# 切片输出
print(train_ids[0:2])
print('=' * 80)
# 循环取数据
for x_train, y_label in train_ids:
print(x_train, y_label)
# DataLoader进行数据封装
print('=' * 80)
train_loader = DataLoader(dataset=train_ids, batch_size=4, shuffle=True)
for i, data in enumerate(train_loader, 1): # 注意enumerate返回值有两个,一个是序号,一个是数据(包含训练数据和标签)
x_data, label = data
print(' batch:{0} x_data:{1} label: {2}'.format(i, x_data, label))
运行结果:
(tensor([[1, 2, 3],
[4, 5, 6]]), tensor([44, 55]))
================================================================================
tensor([1, 2, 3]) tensor(44)
tensor([4, 5, 6]) tensor(55)
tensor([7, 8, 9]) tensor(66)
tensor([1, 2, 3]) tensor(44)
tensor([4, 5, 6]) tensor(55)
tensor([7, 8, 9]) tensor(66)
tensor([1, 2, 3]) tensor(44)
tensor([4, 5, 6]) tensor(55)
tensor([7, 8, 9]) tensor(66)
tensor([1, 2, 3]) tensor(44)
tensor([4, 5, 6]) tensor(55)
tensor([7, 8, 9]) tensor(66)
================================================================================
batch:1 x_data:tensor([[1, 2, 3],
[1, 2, 3],
[4, 5, 6],
[4, 5, 6]]) label: tensor([44, 44, 55, 55])
batch:2 x_data:tensor([[4, 5, 6],
[7, 8, 9],
[7, 8, 9],
[7, 8, 9]]) label: tensor([55, 66, 66, 66])
batch:3 x_data:tensor([[1, 2, 3],
[1, 2, 3],
[7, 8, 9],
[4, 5, 6]]) label: tensor([44, 44, 66, 55])
注意:TensorDataset 中的参数必须是 tensor
Pytorch中TensorDataset的快速使用
Pytorch中,TensorDataset()可以快速构建训练所用的数据,不用使用自建的Mydataset(),如果没有熟悉适用的dataset可以使用TensorDataset()作为暂时替代。
只需要把data和label作为参数输入,就可以快速构建,之后便可以用Dataloader处理。
import numpy as np
from torch.utils.data import DataLoader, TensorDataset
data = np.loadtxt('x.txt')
label = np.loadtxt('y.txt')
data = torch.tensor(data)
label = torch.tensor(label)
train_data = TensorDataset(data, label)
train_loader = DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
来源:https://blog.csdn.net/qq_40211493/article/details/107529148
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 前言Python 的一大优点就是丰富的类库,所以我们经常会用 pip 来安装各种库,所以对于Python开发用户来讲,PIP安装软件包是家常
- 目录一、 环境准备:1.docker环境2.安装mariadb数据库二、ORM1.ORM简介2.django配置数据库第一种方式:第二种方式
- 1:php地址 http://127.0.0.6/?c=json2:java 输出的结果是[{"i
- 本文实例讲述了Python爬虫框架scrapy实现的文件下载功能。分享给大家供大家参考,具体如下:我们在写普通脚本的时候,从一个网站拿到一个
- 在实现贪吃蛇游戏时,首先需要使用python内置的turtle模块来实现创建游戏窗体与键盘按键的监听工作,然后使用freegames模块进行
- 很多时候我们都需要了解下python中导入包的属性方法信息,当然dir 是最便捷的了,不过如果想知道特定的,例如以_ 开头的属性,需要写个筛
- 一、PING简介PING(Packet Internet Grope),也称因特网包探索器。PING使用ICMP回送(ECHO)请求和回送(
- 本文实例讲述了python中requests模块的使用方法。分享给大家供大家参考。具体分析如下:在HTTP相关处理中使用python是不必要
- 1、pyecharts绘制饼图(显示百分比)# 导入模块from pyecharts import options as optsfrom
- 一、前言卷起来好吧,元旦已经过了,就开始写文章模式了。这篇文章会对完全数的各种侦测进行详细解释。写作不易,支持一波~二、完全数是什么1、定义
- 在Google上搜一下,可以发现一大堆对ASP不好的评价,什么运行速度慢、异常处理机制不好、缺乏面向对象机制、开发效率低、漏洞多等等。为了让
- 1. 作用将类方法转换为类属性,可以用 . 直接获取属性值或者对属性进行赋值2.实现方式使用property类来实现,也可以使用proper
- Perl的特殊符号@ 数组 &nb
- 有时候需要在网页中某个div载入之后,动态引入一段javascript,IE下的解决方案: newjs. onreadystatechang
- 项目开始时是一个关键时刻,选择会对项目产生长期的影响。有很多关于如何开始使用Django框架的教程,但很少讨论如何专业地使用Django,或
- 测试靶机为DVWA,适合DVWA暴力破解模块的Low和Medium等级关键代码解释url指定url地址url = &qu
- 14个超酷的js显示时间效果,一定有你想要的。正常时间显示运行效果图:<title>正常显示的时钟 - asp之家 - http
- 如果查询结果很多,服务器解释你的ASP script将花费大量的时间,因为有许多的Response.Write语句要处理. 如果你将输出的全
- 导入相关库import time1. 时间戳1.1 time.time()time.time()可以得到的是 时间戳 。即 1970年1月1
- 设计师不等于美工设计无所不在,但大多数企业不知道如何使用它。现代设计进入中国大概是二十多年的时间,而在国外,尤其在美国在欧洲,大概有一百年的