1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15from tqdm import tqdm 16import numpy as np 17import mindspore as ms 18import mindspore.nn as nn 19from mindspore.dataset import NumpySlicesDataset 20from mindspore import context, Tensor 21 22context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 23 24class AutoEncoderTrainNetwork(nn.Cell): 25 def __init__(self): 26 super(AutoEncoderTrainNetwork, self).__init__() 27 self.loss_fun = nn.MSELoss() 28 self.net = nn.CellList([nn.Dense(2, 32), nn.Dense(32, 2)]) 29 self.relu = nn.ReLU() 30 31 def reconstruct_sample(self, x: Tensor): 32 for _, layer in enumerate(self.net): 33 x = layer(x) 34 x = self.relu(x) 35 return x 36 37 def construct(self, x: Tensor): 38 recon_x = self.reconstruct_sample(x) 39 return self.loss_fun(recon_x, x) 40 41 def sample_2d_data(self, n_normals=2000, n_outliers=400): 42 z = np.random.randn(n_normals, 2) 43 outliers = np.random.uniform(low=-6, high=6, size=(n_outliers, 2)) 44 centers = np.array([(2., 0), (-2., 0)]) 45 sigma = 0.3 46 normal_points = sigma * z + centers[np.random.randint(len(centers), size=(n_normals,))] 47 return np.vstack((normal_points, outliers)) 48 49 def create_synthetic_dataset(self): 50 transformed_dataset = self.sample_2d_data() 51 for dim in range(transformed_dataset.shape[1]): 52 min_val = transformed_dataset[:, dim].min() 53 max_val = transformed_dataset[:, dim].max() 54 if min_val != max_val: 55 transformed_dataset[:, dim] = (transformed_dataset[:, dim] - min_val) / (max_val - min_val) 56 elif min_val != 1: 57 transformed_dataset[:, dim] = transformed_dataset[:, dim] / min_val 58 transformed_dataset = transformed_dataset.astype(np.float32) 59 return transformed_dataset 60 61 62def test_auto_monad_layer(): 63 ae_with_loss = AutoEncoderTrainNetwork() 64 transformed_dataset = ae_with_loss.create_synthetic_dataset() 65 dataloader = NumpySlicesDataset(data=(transformed_dataset,), shuffle=True) 66 dataloader = dataloader.batch(batch_size=16) 67 optim = nn.RMSProp(params=ae_with_loss.trainable_params(), learning_rate=0.002,) 68 train_net = nn.TrainOneStepCell(ae_with_loss, optim) 69 train_net.set_train() 70 gen_samples = dict() 71 num_epoch = 21 72 for epoch in tqdm(range(num_epoch)): 73 loss = [] 74 for _, (batch,) in enumerate(dataloader): 75 batch = Tensor(batch, dtype=ms.float32) 76 loss_ = train_net(batch) 77 loss.append(loss_.asnumpy()) 78 avg_loss = np.array(loss).mean() 79 if epoch % 10 == 0: 80 gen_samples[epoch] = ae_with_loss.reconstruct_sample(Tensor(transformed_dataset)).asnumpy() 81 print(f"epoch: {epoch}/{num_epoch}, avg loss: {avg_loss}") 82