1# Copyright 2020 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# ============================================================================ 15""" test VAE interface """ 16import numpy as np 17 18import mindspore.common.dtype as mstype 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore.common.api import _cell_graph_executor 22from mindspore.nn.probability.dpn import VAE 23 24 25class Encoder(nn.Cell): 26 def __init__(self): 27 super(Encoder, self).__init__() 28 self.fc1 = nn.Dense(6, 3) 29 self.relu = nn.ReLU() 30 31 def construct(self, x): 32 x = self.fc1(x) 33 x = self.relu(x) 34 return x 35 36 37class Decoder(nn.Cell): 38 def __init__(self): 39 super(Decoder, self).__init__() 40 self.fc1 = nn.Dense(3, 6) 41 self.sigmoid = nn.Sigmoid() 42 43 def construct(self, z): 44 z = self.fc1(z) 45 z = self.sigmoid(z) 46 return z 47 48 49def test_vae(): 50 """ 51 Test the vae interface with the DNN model. 52 """ 53 encoder = Encoder() 54 decoder = Decoder() 55 net = VAE(encoder, decoder, hidden_size=3, latent_size=2) 56 input_data = Tensor(np.random.rand(32, 6), dtype=mstype.float32) 57 _cell_graph_executor.compile(net, input_data) 58