• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# ============================================================================
15import numpy as np
16import pytest
17
18import mindspore.context as context
19import mindspore.nn as nn
20from mindspore import Tensor
21from mindspore.ops import composite as C
22
23context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
24
25
26class Net(nn.Cell):
27    def __init__(self, sample, replacement, seed=0):
28        super(Net, self).__init__()
29        self.sample = sample
30        self.replacement = replacement
31        self.seed = seed
32
33    def construct(self, x):
34        return C.multinomial(x, self.sample, self.replacement, self.seed)
35
36
37@pytest.mark.level0
38@pytest.mark.platform_x86_cpu
39@pytest.mark.env_onecard
40def test_multinomial_net():
41    x0 = Tensor(np.array([0.9, 0.2]).astype(np.float32))
42    x1 = Tensor(np.array([[0.9, 0.2], [0.9, 0.2]]).astype(np.float32))
43    net0 = Net(1, True, 20)
44    net1 = Net(2, True, 20)
45    net2 = Net(6, True, 20)
46    out0 = net0(x0)
47    out1 = net1(x0)
48    out2 = net2(x1)
49    assert out0.asnumpy().shape == (1,)
50    assert out1.asnumpy().shape == (2,)
51    assert out2.asnumpy().shape == (2, 6)
52