You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mindspore/tests/ut/python/pipeline/parse/test_operator.py

134 lines
3.8 KiB

# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_operator """
import numpy as np
from mindspore.ops import operations as P
from mindspore.nn import ReLU
from mindspore.nn import Cell
from mindspore import Tensor, Model, context
from ...ut_filter import non_graph_engine
class arithmetic_Net(Cell):
""" arithmetic_Net definition """
def __init__(self, symbol, loop_count=(1, 3)):
super().__init__()
self.symbol = symbol
self.loop_count = loop_count
self.relu = ReLU()
def construct(self, x):
a, b = self.loop_count
y = self.symbol
if y == 1:
a += b
for _ in (b, a):
x = self.relu(x)
elif y == 2:
b -= a
for _ in (a, b):
x = self.relu(x)
elif y == 3:
z = a + b
for _ in (b, z):
x = self.relu(x)
elif y == 4:
z = b - a
for _ in (z, b):
x = self.relu(x)
elif y == 5:
z = a * b
for _ in (a, z):
x = self.relu(x)
elif y == 6:
z = b / a
for _ in (a, z):
x = self.relu(x)
elif y == 7:
z = b % a + 1
for _ in (a, z):
x = self.relu(x)
else:
if not a:
x = self.relu(x)
return x
class logical_Net(Cell):
""" logical_Net definition """
def __init__(self, symbol, loop_count=(1, 3)):
super().__init__()
self.symbol = symbol
self.loop_count = loop_count
self.fla = P.Flatten()
self.relu = ReLU()
def construct(self, x):
a, b = self.loop_count
y = self.symbol
if y == 1:
if b and a:
x = self.relu(x)
else:
x = self.fla(x)
else:
if b or a:
x = self.relu(x)
else:
x = self.fla(x)
return x
def arithmetic_operator_base(symbol):
""" arithmetic_operator_base """
input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
input_me = Tensor(input_np)
logical_operator = {"++": 1, "--": 2, "+": 3, "-": 4, "*": 5, "/": 6, "%": 7, "not": 8}
x = logical_operator[symbol]
net = arithmetic_Net(x)
context.set_context(mode=context.GRAPH_MODE)
model = Model(net)
model.predict(input_me)
def logical_operator_base(symbol):
""" logical_operator_base """
input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
input_me = Tensor(input_np)
logical_operator = {"and": 1, "or": 2}
x = logical_operator[symbol]
net = logical_Net(x)
context.set_context(mode=context.GRAPH_MODE)
model = Model(net)
model.predict(input_me)
@non_graph_engine
def test_ME_arithmetic_operator_0080():
""" test_ME_arithmetic_operator_0080 """
arithmetic_operator_base('not')
@non_graph_engine
def test_ME_arithmetic_operator_0070():
""" test_ME_arithmetic_operator_0070 """
logical_operator_base('and')
@non_graph_engine
def test_ME_logical_operator_0020():
""" test_ME_logical_operator_0020 """
logical_operator_base('or')