• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.ops import operations as P
23
24context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
25
26
27class NetArgminWithValue(nn.Cell):
28    def __init__(self, axis=0, keep_dims=False):
29        super(NetArgminWithValue, self).__init__()
30        self.argmin = P.ArgMinWithValue(axis=axis, keep_dims=keep_dims)
31
32    def construct(self, x):
33        return self.argmin(x)
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_cpu
38@pytest.mark.env_onecard
39def test_argminwithvalue_fp32():
40    x = np.array([[1., 20., 5.],
41                  [67., 8., 9.],
42                  [130., 24., 15.],
43                  [-0.5, 25, 100]]).astype(np.float32)
44    argmin_a0 = NetArgminWithValue(axis=0, keep_dims=False)
45
46    output0, output1 = argmin_a0(Tensor(x))
47    expect0 = np.array([3, 1, 0]).astype(np.int32)
48    expect1 = np.array([-0.5, 8., 5.]).astype(np.float32)
49    error = np.ones(shape=expect1.shape) * 1.0e-6
50    assert np.all(output0.asnumpy() == expect0)
51    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
52
53    argmin_a0k = NetArgminWithValue(axis=0, keep_dims=True)
54
55    output0, output1 = argmin_a0k(Tensor(x))
56    expect0 = np.array([[3, 1, 0]]).astype(np.int32)
57    expect1 = np.array([[-0.5, 8., 5.]]).astype(np.float32)
58    error = np.ones(shape=expect1.shape) * 1.0e-6
59    assert np.all(output0.asnumpy() == expect0)
60    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
61
62    argmin_a1 = NetArgminWithValue(axis=1, keep_dims=False)
63
64    output0, output1 = argmin_a1(Tensor(x))
65    expect0 = np.array([0, 1, 2, 0]).astype(np.int32)
66    expect1 = np.array([1., 8., 15., -0.5]).astype(np.float32)
67    error = np.ones(shape=expect1.shape) * 1.0e-6
68    assert np.all(output0.asnumpy() == expect0)
69    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
70
71    argmin_a1k = NetArgminWithValue(axis=-1, keep_dims=True)
72
73    output0, output1 = argmin_a1k(Tensor(x))
74    expect0 = np.array([[0], [1], [2], [0]]).astype(np.int32)
75    expect1 = np.array([[1.], [8.], [15.], [-0.5]]).astype(np.float32)
76    error = np.ones(shape=expect1.shape) * 1.0e-6
77    assert np.all(output0.asnumpy() == expect0)
78    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
79
80
81@pytest.mark.level0
82@pytest.mark.platform_x86_cpu
83@pytest.mark.env_onecard
84def test_argminwithvalue_fp16():
85    x = np.array([[1., 20., 5.],
86                  [67., 8., 9.],
87                  [130., 24., 15.],
88                  [-0.5, 25, 100]]).astype(np.float16)
89    argmin_a0 = NetArgminWithValue(axis=0, keep_dims=False)
90
91    output0, output1 = argmin_a0(Tensor(x))
92    expect0 = np.array([3, 1, 0]).astype(np.int32)
93    expect1 = np.array([-0.5, 8., 5.]).astype(np.float16)
94    error = np.ones(shape=expect1.shape) * 1.0e-6
95    assert np.all(output0.asnumpy() == expect0)
96    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
97
98    argmin_a0k = NetArgminWithValue(axis=0, keep_dims=True)
99
100    output0, output1 = argmin_a0k(Tensor(x))
101    expect0 = np.array([[3, 1, 0]]).astype(np.int32)
102    expect1 = np.array([[-0.5, 8., 5.]]).astype(np.float16)
103    error = np.ones(shape=expect1.shape) * 1.0e-6
104    assert np.all(output0.asnumpy() == expect0)
105    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
106
107    argmin_a1 = NetArgminWithValue(axis=1, keep_dims=False)
108
109    output0, output1 = argmin_a1(Tensor(x))
110    expect0 = np.array([0, 1, 2, 0]).astype(np.int32)
111    expect1 = np.array([1., 8., 15., -0.5]).astype(np.float16)
112    error = np.ones(shape=expect1.shape) * 1.0e-6
113    assert np.all(output0.asnumpy() == expect0)
114    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
115
116    argmin_a1k = NetArgminWithValue(axis=-1, keep_dims=True)
117
118    output0, output1 = argmin_a1k(Tensor(x))
119    expect0 = np.array([[0], [1], [2], [0]]).astype(np.int32)
120    expect1 = np.array([[1.], [8.], [15.], [-0.5]]).astype(np.float16)
121    error = np.ones(shape=expect1.shape) * 1.0e-6
122    assert np.all(output0.asnumpy() == expect0)
123    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
124
125
126@pytest.mark.level0
127@pytest.mark.platform_x86_cpu
128@pytest.mark.env_onecard
129def test_argminwithvalue_tensor():
130    prop = 100 if np.random.random() > 0.5 else -100
131    x = np.random.randn(3, 4, 5, 6).astype(np.float16) * prop
132    argmin_a0 = NetArgminWithValue(axis=-2, keep_dims=False)
133
134    output0, output1 = argmin_a0(Tensor(x))
135    expect0 = np.argmin(x, axis=-2)
136    expect1 = np.min(x, axis=-2).astype(np.float16)
137    error = np.ones(shape=expect1.shape) * 1.0e-6
138    assert np.all(output0.asnumpy() == expect0)
139    assert np.all(np.abs(output1.asnumpy() - expect1) < error)
140