|
| 1 | +# Copyright (c) 2020 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 | +from __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | +import paddle.fluid.core as core |
| 20 | +from op_test import OpTest |
| 21 | +import paddle |
| 22 | +from paddle import fluid, nn |
| 23 | +import paddle.fluid.dygraph as dg |
| 24 | +import paddle.nn.functional as F |
| 25 | +import paddle.fluid.initializer as I |
| 26 | + |
| 27 | + |
| 28 | +class LinearTestCase(unittest.TestCase): |
| 29 | + def setUp(self): |
| 30 | + self.dtype = 'float32' |
| 31 | + self.input = np.ones((3, 1, 2)).astype(self.dtype) |
| 32 | + self.weight = np.ones((2, 2)).astype(self.dtype) |
| 33 | + self.bias = np.ones((2)).astype(self.dtype) |
| 34 | + self.place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda( |
| 35 | + ) else paddle.CPUPlace() |
| 36 | + |
| 37 | + def functional(self, place): |
| 38 | + paddle.disable_static(place) |
| 39 | + input = paddle.to_tensor(self.input) |
| 40 | + weight = paddle.to_tensor(self.weight) |
| 41 | + bias = paddle.to_tensor(self.bias) |
| 42 | + out = F.linear(input, weight, bias) |
| 43 | + return out.numpy() |
| 44 | + |
| 45 | + def paddle_nn_layer(self, place): |
| 46 | + paddle.disable_static(place) |
| 47 | + input = paddle.to_tensor(self.input) |
| 48 | + weight_attr = fluid.ParamAttr( |
| 49 | + name="linear_weight", |
| 50 | + learning_rate=1.0, |
| 51 | + trainable=False, |
| 52 | + regularizer=None, |
| 53 | + initializer=paddle.fluid.initializer.ConstantInitializer(value=1.0)) |
| 54 | + bias_attr = fluid.ParamAttr( |
| 55 | + name="linear_bias", |
| 56 | + learning_rate=1.0, |
| 57 | + trainable=False, |
| 58 | + regularizer=None, |
| 59 | + initializer=paddle.fluid.initializer.ConstantInitializer(value=1.0)) |
| 60 | + linear = paddle.nn.Linear( |
| 61 | + 2, 2, weight_attr=weight_attr, bias_attr=bias_attr) |
| 62 | + y = linear(input) |
| 63 | + return y.numpy() |
| 64 | + |
| 65 | + def numpy_cal(self): |
| 66 | + res = np.matmul(self.input, self.weight) + self.bias |
| 67 | + return res |
| 68 | + |
| 69 | + def test_error(self, place=paddle.CPUPlace()): |
| 70 | + res_f = self.functional(place) |
| 71 | + res_nn = self.paddle_nn_layer(place) |
| 72 | + res_np = self.numpy_cal() |
| 73 | + np.testing.assert_array_almost_equal(res_f, res_nn) |
| 74 | + np.testing.assert_array_almost_equal(res_nn, res_np) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + unittest.main() |
0 commit comments