|
| 1 | +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 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 | +import unittest |
| 16 | +import numpy as np |
| 17 | +from op_test import OpTest |
| 18 | + |
| 19 | + |
| 20 | +def fc_refer(matrix, with_bias): |
| 21 | + in_n, in_c, in_h, in_w = matrix.input.shape |
| 22 | + w_i, w_o = matrix.weights.shape |
| 23 | + |
| 24 | + x_data = np.reshape(matrix.input, [in_n, in_c * in_h * in_w]) |
| 25 | + w_data = np.reshape(matrix.weights, [w_i, w_o]) |
| 26 | + b_data = np.reshape(matrix.bias, [1, w_o]) |
| 27 | + result = None |
| 28 | + |
| 29 | + if with_bias: |
| 30 | + result = np.dot(x_data, w_data) + b_data |
| 31 | + else: |
| 32 | + result = np.dot(x_data, w_data) |
| 33 | + |
| 34 | + return result |
| 35 | + |
| 36 | + |
| 37 | +class MatrixGenerate: |
| 38 | + def __init__(self, mb, ic, oc, h, w): |
| 39 | + self.input = np.random.random((mb, ic, h, w)).astype("float32") |
| 40 | + self.weights = np.random.random((ic * h * w, oc)).astype("float32") |
| 41 | + self.bias = np.random.random((1, oc)).astype("float32") |
| 42 | + |
| 43 | + |
| 44 | +class TestFCOp(OpTest): |
| 45 | + def setUp(self): |
| 46 | + self.op_type = "fc" |
| 47 | + self.matrix = MatrixGenerate(1, 10, 15, 3, 3) |
| 48 | + |
| 49 | + self.with_bias = True |
| 50 | + if self.with_bias: |
| 51 | + self.inputs = { |
| 52 | + 'Input': self.matrix.input, |
| 53 | + 'W': self.matrix.weights, |
| 54 | + 'Bias': self.matrix.bias |
| 55 | + } |
| 56 | + else: |
| 57 | + self.inputs = {'Input': self.matrix.input, 'W': self.matrix.weights} |
| 58 | + |
| 59 | + self.attrs = {'use_mkldnn': False} |
| 60 | + |
| 61 | + self.outputs = {'Out': fc_refer(self.matrix, self.with_bias)} |
| 62 | + |
| 63 | + def test_check_output(self): |
| 64 | + self.check_output() |
| 65 | + |
| 66 | + |
| 67 | +class TestFCOpBiasBoth(TestFCOp): |
| 68 | + def init_shapes(self, mb, ic, oc, h, w): |
| 69 | + for with_bias in {True, False}: |
| 70 | + self.with_bias = with_bias |
| 71 | + self.matrix = MatrixGenerate(mb, ic, oc, h, w) |
| 72 | + |
| 73 | + |
| 74 | +class TestFCOp1(TestFCOpBiasBoth): |
| 75 | + def init_op_type(self): |
| 76 | + self.init_shapes(2, 8, 10, 1, 1) |
| 77 | + |
| 78 | + |
| 79 | +class TestFCOp2(TestFCOpBiasBoth): |
| 80 | + def init_op_type(self): |
| 81 | + self.init_shapes(4, 5, 6, 2, 2) |
| 82 | + |
| 83 | + |
| 84 | +class TestFCOp4(TestFCOpBiasBoth): |
| 85 | + def init_op_type(self): |
| 86 | + self.init_shapes(1, 32, 64, 3, 3) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + unittest.main() |
0 commit comments