Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / reduce.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import logging as log
18
19 import numpy as np
20
21
22 def tf_reduce_infer(node, op=None):
23     input_shape = node.in_node(0).shape
24     log.debug("input_shape: {}".format(input_shape))
25     axis = node.in_node(1).value
26     if input_shape is None or axis is None or input_shape.ndim != 1 or axis.ndim > 1:
27         return
28     output_shape = np.array(input_shape)
29     if len(axis.shape) == 0:  # fix since np.delete deprecate negative idxs
30         axis = axis.reshape([1])
31     axis[axis < 0] += output_shape.shape[0]
32     if node.keep_dims:
33         output_shape[axis] = 1
34     else:
35         output_shape = np.delete(output_shape, axis)
36     node.out_node().shape = output_shape
37     if op is not None and node.in_node(0).value is not None:
38         node.out_node(0).value = np.array(op(node.in_node(0).value, (*axis,)),
39                                           dtype=node.in_node(0).value.dtype)  # TODO extend to multi-dimensional axis
40         log.debug("value: {}".format(node.out_node(0).value))