This code implements a custom data loader (SMDSegLoader) for loading time series data into a neural network for training and testing. The data loader inherits from the PyTorch Dataset class, making it easy to use in PyTorch. It supports various data modes like 'train', 'val', 'test', and 'predict' to handle different stages of the machine learning pipeline.

The __init__ method takes four parameters:

  • root_path: The directory where the data is stored.
  • win_size: The size of each data window.
  • step: The step size for moving the window.
  • flag: The type of dataset ('train', 'val', 'test', or 'predict').

In the __init__ method, training and test data are loaded and standardized using the StandardScaler function. Then, the dataset is split into train, validation, test, and prediction sets based on the flag argument. Train and validation sets are split from the training data, the test set is split from the test data, and the prediction set is used for prediction without labels.

The __len__ method calculates the number of windows in the dataset based on the dataset type and returns it. For example, in the training set, the number of windows is calculated by dividing the length of the training data by the step size and adding 1.

The __getitem__ method gets the data window and label information based on the index and converts them to float32 type. It retrieves the data window and label information from the training data for train or validation sets, from the test data for the test set, and from the test data based on the window size for the prediction set (without labels). The retrieved data window and label information are then returned as a tuple.

class SMDSegLoader(Dataset):
    def __init__(self, root_path, win_size, step=100, flag='train'):
        self.flag = flag
        self.step = step
        self.win_size = win_size
        self.scaler = StandardScaler()
        data = np.load(os.path.join(root_path, 'SMD_train.npy'))
        self.scaler.fit(data)
        data = self.scaler.transform(data)
        test_data = np.load(os.path.join(root_path, 'SMD_test.npy'))
        self.test = self.scaler.transform(test_data)
        self.train = data
        data_len = len(self.train)
        self.val = self.train[(int)(data_len * 0.8):]
        self.test_labels = np.load(os.path.join(root_path, 'SMD_test_label.npy'))

    def __len__(self):
        if self.flag == 'train':
            return (self.train.shape[0] - self.win_size) // self.step + 1
        elif (self.flag == 'val'):
            return (self.val.shape[0] - self.win_size) // self.step + 1
        elif (self.flag == 'test'):
            return (self.test.shape[0] - self.win_size) // self.step + 1
        else:
            return (self.test.shape[0] - self.win_size) // self.win_size + 1

    def __getitem__(self, index):
        index = index * self.step
        if self.flag == 'train':
            return np.float32(self.train[index:index + self.win_size]), np.float32(self.test_labels[0:self.win_size])
        elif (self.flag == 'val'):
            return np.float32(self.val[index:index + self.win_size]), np.float32(self.test_labels[0:self.win_size])
        elif (self.flag == 'test'):
            return np.float32(self.test[index:index + self.win_size]), np.float32(
                self.test_labels[index:index + self.win_size])
        else:
            return np.float32(self.test[
                              index // self.step * self.win_size:index // self.step * self.win_size + self.win_size]), np.float32(
                self.test_labels[index // self.step * self.win_size:index // self.step * self.win_size + self.win_size])
SMDSegLoader: A Custom Data Loader for Time Series Segmentation

原文地址: https://www.cveoy.top/t/topic/m9kS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录