标签:__ use Datasets sentence text self torch array data
ref:
https://towardsdatascience.com/how-to-use-datasets-and-dataloader-in-pytorch-for-custom-text-data-270eed7f7c00
https://pytorch.org/tutorials/beginner/data_loading_tutorial.html
https://sparrow.dev/pytorch-dataloader/
Creating a PyTorch Dataset and managing it with Dataloader keeps your data manageable and helps to simplify your machine learning pipeline. a Dataset stores all your data, and Dataloader is can be used to iterate through the data, manage batches, transform the data, and much more.
Import libraries
import pandas as pd import torch from torch.utils.data import Dataset, DataLoader
Pandas is not essential to create a Dataset object. However, it’s a powerful tool for managing data so i’m going to use it.
torch.utils.data imports the required functions we need to create and use Dataset and DataLoader.
Create a custom Dataset class
If the original data are as follows:
in numbers.cvs:
torch.utils.data.Dataset
is an abstract class representing a dataset. Your custom dataset should inherit Dataset
and override the following methods:
__len__
so thatlen(dataset)
returns the size of the dataset.__getitem__
to support the indexing such thatdataset[i]
can be used to get iith sample.
We will read the csv in __init__
but leave the reading of images to __getitem__
. This is memory efficient because all the images are not stored in the memory at once but read as required.
class SeqDataset(Dataset): def __init__(self, file_root, max_length) -> None: super(SeqDataset).__init__() self.sentences = pd.read_csv(file_root) self.max_length = max_length def __len__(self): return len(self.sentences) def __getitem__(self, index): # 字符串处理 sentence_a = self.sentences.sentence_a[index][1:-1].split(",") sentence_b = self.sentences.sentence_b[index][1:-1].split(",") # ['3', '4', '5'] # ['6', '7', '8'] # listz转array sentence_a = np.array([int(x) for x in sentence_a]) sentence_b = np.array([int(x) for x in sentence_b]) # array([3, 4, 5]) # array([6, 7, 8]) # 补齐 sentence_a = np.pad(sentence_a, (0, self.max_length-sentence_a.shape[0]), 'constant', constant_values=(0,0)) sentence_b = np.pad(sentence_b, (0, self.max_length-sentence_b.shape[0]), 'constant', constant_values=(0,0)) # array([3, 4, 5, 0, 0, 0, 0, 0, 0, 0]) # array([6, 7, 8, 0, 0, 0, 0, 0, 0, 0]) return sentence_a, sentence_b
Iterating through the dataset
We can iterate over the created dataset with a for i in range
loop as before.
However, we are losing a lot of features by using a simple for
loop to iterate over the data. In particular, we are missing out on:
- Batching the data
- Shuffling the data
- Load the data in parallel using
multiprocessing
workers.
torch.utils.data.DataLoader
is an iterator which provides all these features. Parameters used below should be clear. One parameter of interest is collate_fn
. You can specify how exactly the samples need to be batched using collate_fn
. However, default collate should work fine for most use cases.
dataloader = DataLoader(dataset, batch_size=4, shuffle=False, num_workers=0, collate_fn=None) for batch_idx, batch in enumerate(dataloader): src, trg = batch print(src.shape) print(trg.shape)
Output:
(deeplearning) ➜ TransformerScratch python generate_data.py torch.Size([4, 10]) torch.Size([4, 10]) tensor([[3, 4, 5, 0, 0, 0, 0, 0, 0, 0], [2, 3, 4, 0, 0, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5, 6, 7, 8, 9, 0, 0, 0, 0, 0]]) tensor([[ 6, 7, 8, 0, 0, 0, 0, 0, 0, 0], [ 5, 6, 7, 0, 0, 0, 0, 0, 0, 0], [ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0], [10, 11, 12, 13, 14, 0, 0, 0, 0, 0]]) torch.Size([4, 10]) torch.Size([4, 10]) tensor([[4, 5, 0, 0, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 0, 0, 0, 0, 0], [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]]) tensor([[ 6, 7, 0, 0, 0, 0, 0, 0, 0, 0], [ 6, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 6, 7, 8, 9, 10, 0, 0, 0, 0, 0], [ 6, 7, 8, 9, 10, 0, 0, 0, 0, 0]]) torch.Size([2, 10]) torch.Size([2, 10]) tensor([[4, 5, 6, 0, 0, 0, 0, 0, 0, 0], [3, 4, 5, 0, 0, 0, 0, 0, 0, 0]]) tensor([[7, 8, 9, 0, 0, 0, 0, 0, 0, 0], [6, 7, 8, 0, 0, 0, 0, 0, 0, 0]])
Full Code
import torch from torch.utils.data import Dataset, DataLoader import pandas as pd import numpy as np import ipdb class SeqDataset(Dataset): def __init__(self, file_root, max_length) -> None: super(SeqDataset).__init__() self.sentences = pd.read_csv(file_root) self.max_length = max_length def __len__(self): return len(self.sentences) def __getitem__(self, index): # 字符串处理 sentence_a = self.sentences.sentence_a[index][1:-1].split(",") sentence_b = self.sentences.sentence_b[index][1:-1].split(",") # ['3', '4', '5'] # ['6', '7', '8'] # listz转array sentence_a = np.array([int(x) for x in sentence_a]) sentence_b = np.array([int(x) for x in sentence_b]) # array([3, 4, 5]) # array([6, 7, 8]) # 补齐 sentence_a = np.pad(sentence_a, (0, self.max_length-sentence_a.shape[0]), 'constant', constant_values=(0,0)) sentence_b = np.pad(sentence_b, (0, self.max_length-sentence_b.shape[0]), 'constant', constant_values=(0,0)) # array([3, 4, 5, 0, 0, 0, 0, 0, 0, 0]) # array([6, 7, 8, 0, 0, 0, 0, 0, 0, 0]) return sentence_a, sentence_b if __name__ == "__main__": dataset = SeqDataset("./numbers.csv", 10) # print(dataset.__len__()) # print(dataset.__getitem__(0)) # print(dataset.__getitem__(6)) dataloader = DataLoader(dataset, batch_size=4, shuffle=False, num_workers=0, collate_fn=None) for batch_idx, batch in enumerate(dataloader): src, trg = batch print(src.shape, trg.shape) print(src) print(trg) # ipdb.set_trace()
标签:__,use,Datasets,sentence,text,self,torch,array,data 来源: https://www.cnblogs.com/lfri/p/15479166.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。