简体   繁体   English

如何为自定义数据集类预处理 Pytorch 中的数据?

[英]How to preprocess data in Pytorch for custom Dataset Classes?

I am new to PyTorch, and I have a dataset given in a pickle file format, with the Dataset class already defined (which I'm not allowed to make changes to).我是 PyTorch 的新手,我有一个以 pickle 文件格式给出的数据集,其中数据集 class 已经定义(我不允许对其进行更改)。 The code for the Dataset class is as follows:数据集 class 的代码如下:

class MyData(Dataset):
    def __init__(self, mode):
        with open(mode+'.pkl', 'rb') as handle:
            data = pickle.load(handle)
            self.X = data['x'].astype('float')
            self.y = data['y'].astype('long')

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        sample = (self.X[idx], self.y[idx])

        return sample

The returned data is not normalized, and I was looking at how to do that in PyTorch, but most of them were using the transform method which I think I cannot use because I'm not allowed to change the Dataset class.返回的数据未标准化,我正在研究如何在 PyTorch 中执行此操作,但其中大多数都使用我认为我无法使用的transform方法,因为我不允许更改数据集 class。

How do I prepare the data before feeding it to the neural network through the DataLoader class?在通过 DataLoader class 将数据输入神经网络之前,如何准备数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM