1# Copyright 2019 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# ============================================================================ 15import numpy as np 16 17import mindspore.context as context 18import mindspore.nn as nn 19from mindspore import Tensor 20from mindspore.common.api import ms_function 21 22context.set_context(device_target="Ascend") 23 24 25class Net(nn.Cell): 26 def __init__(self): 27 super(Net, self).__init__() 28 self.dense = nn.Dense(2048, 1001) 29 30 @ms_function 31 def construct(self, x): 32 return self.dense(x) 33 34class MultiLayerDense(nn.Cell): 35 def __init__(self): 36 super(MultiLayerDense, self).__init__() 37 self.dense1 = nn.Dense(in_channels=256, out_channels=512) 38 self.dense2 = nn.Dense(in_channels=512, out_channels=1024) 39 40 @ms_function 41 def construct(self, x): 42 x = self.dense1(x) 43 x = self.dense2(x) 44 return x 45 46 47def test_net(): 48 x = np.random.randn(32, 2048).astype(np.float32) 49 net = Net() 50 output = net(Tensor(x)) 51 print(x) 52 print(output.asnumpy()) 53 54 55def test_net_ND(): 56 x = np.random.randn(2, 332, 2048).astype(np.float32) 57 net = Net() 58 output = net(Tensor(x)) 59 print(x) 60 print(output.asnumpy()) 61 62 63def test_net_multilayer(): 64 x = np.random.randn(16, 32, 256).astype(np.float32) 65 net = MultiLayerDense() 66 output = net(Tensor(x)) 67 print(x) 68 print(output.asnumpy()) 69