pytorch笔记:Dataloader
生活随笔
收集整理的這篇文章主要介紹了
pytorch笔记:Dataloader
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Dataloader是?torch 給我們用來包裝數(shù)據(jù)的工具。所以我們要將自己的 (ndarray或其他) 數(shù)據(jù)形式裝換成 Tensor, 然后再放進Dataloader這個包裝器中。?使用Dataloader有什么好處呢? 就是它可以幫我們有效地迭代數(shù)據(jù)。
1 準備部分
1.1 導入庫
import torch import torch.utils.data as Data1.2 數(shù)據(jù)集部分
x=torch.linspace(1,10,10) y=torch.linspace(-10,-1,10) print(x,'\n',y) ''' tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) tensor([-10., -9., -8., -7., -6., -5., -4., -3., -2., -1.]) ''' BATCH_SIZE=52 方法1 TensorDataset+DataLoader
torch_dataset=Data.TensorDataset(x,y) #先轉(zhuǎn)化成pytorch可以識別的Dataset格式loader=Data.DataLoader(dataset=torch_dataset,batch_size=BATCH_SIZE,shuffle=True) #把dataset導入dataloader,并設(shè)置batch_size和shufflefor epoch in range(3):for step,(batch_x,batch_y) in enumerate(loader):print('epoch: ',epoch)print('step: ',step,'\n x: ',batch_x,'\n y: ',batch_y)print('*'*30)''' epoch: 0 step: 0 x: tensor([9., 2., 6., 5., 3.]) y: tensor([-2., -9., -5., -6., -8.]) ****************************** epoch: 0 step: 1 x: tensor([10., 1., 7., 4., 8.]) y: tensor([ -1., -10., -4., -7., -3.]) ****************************** epoch: 1 step: 0 x: tensor([ 3., 5., 2., 10., 4.]) y: tensor([-8., -6., -9., -1., -7.]) ****************************** epoch: 1 step: 1 x: tensor([7., 6., 8., 1., 9.]) y: tensor([ -4., -5., -3., -10., -2.]) ****************************** epoch: 2 step: 0 x: tensor([10., 3., 1., 8., 9.]) y: tensor([ -1., -8., -10., -3., -2.]) ****************************** epoch: 2 step: 1 x: tensor([5., 7., 2., 6., 4.]) y: tensor([-6., -4., -9., -5., -7.]) ****************************** '''3 方法2 自定義類+DataLoader
class MyDataSet(Data.Dataset):def __init__(self,x,y):super(MyDataSet,self).__init__()self.x=xself.y=ydef __len__(self):return self.x.shape[0]#有幾組數(shù)據(jù)def __getitem__(self,idx):return(self.x[idx],self.y[idx])#根據(jù)索引找到數(shù)據(jù)loader2=Data.DataLoader(MyDataSet(x,y),batch_size=BATCH_SIZE,shuffle=True)for epoch in range(3):for step,(batch_x,batch_y) in enumerate(loader2):print('epoch: ',epoch)print('step: ',step,'\n x: ',batch_x,'\n y: ',batch_y)print('*'*30)''' epoch: 0 step: 0 x: tensor([9., 7., 2., 6., 3.]) y: tensor([-2., -4., -9., -5., -8.]) ****************************** <class 'torch.Tensor'> epoch: 0 step: 1 x: tensor([ 5., 4., 8., 10., 1.]) y: tensor([ -6., -7., -3., -1., -10.]) ****************************** <class 'torch.Tensor'> epoch: 1 step: 0 x: tensor([ 6., 3., 5., 10., 9.]) y: tensor([-5., -8., -6., -1., -2.]) ****************************** <class 'torch.Tensor'> epoch: 1 step: 1 x: tensor([4., 7., 1., 8., 2.]) y: tensor([ -7., -4., -10., -3., -9.]) ****************************** <class 'torch.Tensor'> epoch: 2 step: 0 x: tensor([ 6., 1., 10., 3., 4.]) y: tensor([ -5., -10., -1., -8., -7.]) ****************************** <class 'torch.Tensor'> epoch: 2 step: 1 x: tensor([2., 9., 7., 5., 8.]) y: tensor([-9., -2., -4., -6., -3.]) ****************************** '''?
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的pytorch笔记:Dataloader的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文巾解题 1. 两数之和
- 下一篇: 博弈论笔记:谈判与讨价还价