pytorch入门08--数据集转换 transform

#数据集转换 transform
import torch
import torchvision
from torch.utils.data import Dataset
import numpy as np


class WineDataset(Dataset):

    def __init__(self, transform=None):
        xy = np.loadtxt('./wine.csv', delimiter=',', dtype=np.float32, skiprows=1)
        self.n_samples = xy.shape[0]

        # note that we do not convert to tensor here
        self.x_data = xy[:, 1:]
        self.y_data = xy[:, [0]]
        self.transform = transform

    def __getitem__(self, index):  #用来获取一些索引的数据,使dataset[i]返回数据集中第i个样本。
        sample = self.x_data[index], self.y_data[index]
        if self.transform:
            sample = self.transform(sample)
        return sample

    def __len__(self):   #实现len(dataset)返回整个数据集的大小
        return self.n_samples

# Custom Transforms
# implement __call__(self, sample)
class ToTensor:
    # Convert ndarrays to Tensors  调用父类参数,无需再初始化 传入参数
    def __call__(self, sample):
        inputs, targets = sample
        return torch.from_numpy(inputs), torch.from_numpy(targets)

class MulTransform:
    # multiply inputs with a given factor
    def __init__(self, factor):   #需要传入参数factor,需要init初始化
        self.factor = factor

    def __call__(self, sample):
        inputs, targets = sample
        inputs *= self.factor
        return inputs, targets

print('Without Transform')
dataset = WineDataset()
first_data = dataset[0]
features, labels = first_data
print(type(features), type(labels))
print(features, labels)

print('\nWith Tensor Transform')
dataset = WineDataset(transform=ToTensor()) #获取酒类数据集,并用transform调用前面定义好的ToTensor转换类型
first_data = dataset[0]     #获取第一行数据
features, labels = first_data  #将第一行的特征赋予features,将第一韩的标签赋予labels
print(type(features), type(labels)) #打印类型 主要看是否转化为了tensor类型
print(features, labels)

print('\nWith Tensor and Multiplication Transform')
#torchvision.transforms.Compose组合变换连续一起操作
composed = torchvision.transforms.Compose([ToTensor(), MulTransform(4)])  #同时使用多个转换
dataset = WineDataset(transform=composed)
first_data = dataset[0]
features, labels = first_data
print(type(features), type(labels))
print(features, labels)

 

你可能感兴趣的:(torch)