Coverage for mlprodict/onnxrt/ops_cpu/op_lrn.py: 100%
18 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-04 02:28 +0100
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-04 02:28 +0100
1# -*- encoding: utf-8 -*-
2# pylint: disable=E0203,E1101,C0111
3"""
4@file
5@brief Runtime operator.
6"""
7import math
8import numpy
9from ._op import OpRun
12class LRN(OpRun):
14 atts = {
15 'alpha': 9.999999747378752e-05,
16 'beta': 0.75,
17 'bias': 1.,
18 'size': 3,
19 }
21 def __init__(self, onnx_node, desc=None, **options):
22 OpRun.__init__(self, onnx_node, desc=desc,
23 expected_attributes=LRN.atts,
24 **options)
26 def _run(self, x, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221
27 if len(x.shape) != 4:
28 raise RuntimeError( # pragma: no cover
29 f"LRN only applies on 4D tensors but shape is {x.shape!r}.")
30 square_sum = numpy.zeros(x.shape).astype(x.dtype)
31 for ind in numpy.ndindex(x.shape):
32 n, c, h, w = ind
33 begin = max(0, c - int(math.floor((self.size - 1) / 2)))
34 end = min(5, c + int(math.ceil((self.size - 1) / 2)) + 1)
35 square_sum[n, c, h, w] = numpy.sum(x[n, begin:end, h, w] ** 2)
36 y = x / ((self.bias + (self.alpha / self.size) * square_sum) ** self.beta)
37 return (y.astype(x.dtype), )