test_norm_op.py 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import unittest
import numpy as np
from op_test import OpTest


def norm(input, scale, epsilon):
    s0, s1, s2, s3 = input.shape
    x_square = input * input
    for i in xrange(s0):
        input_batch = input[i:i + 1, :, :, :]
        input_batch = input_batch.reshape(s1, s2 * s3)
        x_square_batch = x_square[i:i + 1, :, :, :]
        x_square_batch = x_square_batch.reshape(s1, s2 * s3)
        square_colsum = x_square_batch.sum(axis=0) + epsilon
        tmp = pow(square_colsum, 0.5)
        tmp = np.reciprocal(tmp)
        tmp_tile = np.tile(tmp, s1)
        tmp_tile = tmp_tile.reshape(s1, s2 * s3)
        scale_tile = np.tile(scale, (1, s2 * s3))
        scale_tile = scale_tile.reshape(s1, s2 * s3)
        out_batch = input_batch * tmp_tile * scale_tile
        out_batch = out_batch.reshape(1, s1, s2, s3)
        if i == 0:
            out = out_batch
        else:
            out = np.concatenate((out, out_batch), 0)
    out.reshape(s0, s1, s2, s3)
    return out


class TestNormOp(OpTest):
    def setUp(self):
        self.op_type = "norm"
        self.init_test_case()
        input = np.random.random(self.shape).astype("float32")
        scale = np.array([10, 10, 10])
        self.inputs = {
            'X': input.astype('float32'),
            'Scale': scale.astype('float32')
        }
        self.attrs = {'epsilon': self.epsilon}
        output = norm(input, scale, self.epsilon)
        self.outputs = {'Out': output.astype('float32')}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')

    def init_test_case(self):
S
sweetsky0901 已提交
52
        self.shape = [2, 3, 2, 2]
53 54 55 56 57
        self.epsilon = 1e-6


if __name__ == '__main__':
    unittest.main()