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