#数据增强 def data_augment(X, y, p=0.8, alpha=0.5, beta=0.5): """Regression SMOTE 1.将数据x分为 fix_X和X 2.对X进行重塑 对随机的均匀分布小于0.8的索引idx_to_change对应数据部分进行重塑。 X2为打乱顺序后的X beta值为 np.random.beta(alpha, beta, batch_size) / 2 + 0.5 ####beta值>0.5 确保重塑数据中大部分是原值 X[idx_to_change] = beta值*X[idx_to_change] +(1-beta值)*X2[idx_to_change] 3.合并fix_X和X成新的数据x 4.对于输入y同理处理 """ 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]) #返回符合均匀分布的,范围在[0, 1)的Tensor shape:[32] idx_to_change = random_values < p #32个值中,小于0.8的为False,其余为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#用于打乱数据编号 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)