当前位置:网站首页>Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)

Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)

2022-07-23 19:37:00 Lee Meier

# Data to enhance 
def data_augment(X, y, p=0.8, alpha=0.5, beta=0.5):
    """Regression SMOTE
    1. Put the data x It is divided into  fix_X and X
    2. Yes X To remodel 
         For random uniform distribution less than 0.8 The index of idx_to_change Reshape the data part .
        X2 It's after disordering the order X
        beta The value is  np.random.beta(alpha, beta, batch_size) / 2 + 0.5 ####beta value >0.5  Make sure that most of the reshaped data is the original value 

        X[idx_to_change] = beta value *X[idx_to_change] +(1-beta value )*X2[idx_to_change]

    3. Merge fix_X and X New data x
    4. For input y In the same way 
    """
    fix_X, X = X[:, :, :, :2], X[:, :, :, 2:]#[32, 134, 144, 2] [32, 134, 144, 10]
    fix_y, y = y[:, :, :, :2], y[:, :, :, 2:]#[32, 134, 288, 2] [32, 134, 288, 10]
    # print('fix_X, X:',fix_X.shape, X.shape)
    batch_size = X.shape[0]#32
    random_values = paddle.rand([batch_size]) # Returns uniformly distributed , The scope is [0, 1) Of Tensor  shape:[32]
    idx_to_change = random_values < p #32 A value of , Less than 0.8 For the False, Others are True

    # ensure that first element to switch has probability > 0.5
    np_betas = np.random.beta(alpha, beta, batch_size) / 2 + 0.5
    random_betas = paddle.to_tensor(
        np_betas, dtype="float32").reshape([-1, 1, 1, 1]) # [32, 1, 1, 1]
    index_permute = paddle.randperm(batch_size)# returns a 1-D Tensor filled with random permutation values from 0 to n-1# Used to disrupt data numbers 

    X[idx_to_change] = random_betas[idx_to_change] * X[idx_to_change]
    X[idx_to_change] += (
        1 - random_betas[idx_to_change]) * X[index_permute][idx_to_change]

    y[idx_to_change] = random_betas[idx_to_change] * y[idx_to_change]
    y[idx_to_change] += (
        1 - random_betas[idx_to_change]) * y[index_permute][idx_to_change]
    return paddle.concat([fix_X, X], -1), paddle.concat([fix_y, y], -1)
原网站

版权声明
本文为[Lee Meier]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231747417158.html