|
| 1 | +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. |
| 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 decorators |
| 17 | +import paddle.v2.fluid as fluid |
| 18 | +import numpy |
| 19 | + |
| 20 | + |
| 21 | +class TestMathOpPatches(unittest.TestCase): |
| 22 | + @decorators.prog_scope() |
| 23 | + def test_add_scalar(self): |
| 24 | + a = fluid.layers.data(name="a", shape=[1]) |
| 25 | + b = a + 10 |
| 26 | + place = fluid.CPUPlace() |
| 27 | + exe = fluid.Executor(place) |
| 28 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 29 | + b_np = exe.run(fluid.default_main_program(), |
| 30 | + feed={"a": a_np}, |
| 31 | + fetch_list=[b]) |
| 32 | + self.assertTrue(numpy.allclose(a_np + 10, b_np)) |
| 33 | + |
| 34 | + @decorators.prog_scope() |
| 35 | + def test_radd_scalar(self): |
| 36 | + a = fluid.layers.data(name="a", shape=[1]) |
| 37 | + b = 10 + a |
| 38 | + place = fluid.CPUPlace() |
| 39 | + exe = fluid.Executor(place) |
| 40 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 41 | + b_np = exe.run(fluid.default_main_program(), |
| 42 | + feed={"a": a_np}, |
| 43 | + fetch_list=[b]) |
| 44 | + self.assertTrue(numpy.allclose(a_np + 10, b_np)) |
| 45 | + |
| 46 | + @decorators.prog_scope() |
| 47 | + def test_sub_scalar(self): |
| 48 | + a = fluid.layers.data(name="a", shape=[1]) |
| 49 | + b = a - 10 |
| 50 | + place = fluid.CPUPlace() |
| 51 | + exe = fluid.Executor(place) |
| 52 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 53 | + b_np = exe.run(fluid.default_main_program(), |
| 54 | + feed={"a": a_np}, |
| 55 | + fetch_list=[b]) |
| 56 | + self.assertTrue(numpy.allclose(a_np - 10, b_np)) |
| 57 | + |
| 58 | + @decorators.prog_scope() |
| 59 | + def test_radd_scalar(self): |
| 60 | + a = fluid.layers.data(name="a", shape=[1]) |
| 61 | + b = 10 - a |
| 62 | + place = fluid.CPUPlace() |
| 63 | + exe = fluid.Executor(place) |
| 64 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 65 | + b_np = exe.run(fluid.default_main_program(), |
| 66 | + feed={"a": a_np}, |
| 67 | + fetch_list=[b]) |
| 68 | + self.assertTrue(numpy.allclose(10 - a_np, b_np)) |
| 69 | + |
| 70 | + @decorators.prog_scope() |
| 71 | + def test_mul_scalar(self): |
| 72 | + a = fluid.layers.data(name="a", shape=[1]) |
| 73 | + b = a * 10 |
| 74 | + place = fluid.CPUPlace() |
| 75 | + exe = fluid.Executor(place) |
| 76 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 77 | + b_np = exe.run(fluid.default_main_program(), |
| 78 | + feed={"a": a_np}, |
| 79 | + fetch_list=[b]) |
| 80 | + self.assertTrue(numpy.allclose(a_np * 10, b_np)) |
| 81 | + |
| 82 | + @decorators.prog_scope() |
| 83 | + def test_rmul_scalar(self): |
| 84 | + a = fluid.layers.data(name="a", shape=[1]) |
| 85 | + b = 10 * a |
| 86 | + place = fluid.CPUPlace() |
| 87 | + exe = fluid.Executor(place) |
| 88 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 89 | + b_np = exe.run(fluid.default_main_program(), |
| 90 | + feed={"a": a_np}, |
| 91 | + fetch_list=[b]) |
| 92 | + self.assertTrue(numpy.allclose(10 * a_np, b_np)) |
| 93 | + |
| 94 | + @decorators.prog_scope() |
| 95 | + def test_div_scalar(self): |
| 96 | + a = fluid.layers.data(name="a", shape=[1]) |
| 97 | + b = a / 10 |
| 98 | + place = fluid.CPUPlace() |
| 99 | + exe = fluid.Executor(place) |
| 100 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 101 | + b_np = exe.run(fluid.default_main_program(), |
| 102 | + feed={"a": a_np}, |
| 103 | + fetch_list=[b]) |
| 104 | + self.assertTrue(numpy.allclose(a_np / 10, b_np)) |
| 105 | + |
| 106 | + @decorators.prog_scope() |
| 107 | + def test_rdiv_scalar(self): |
| 108 | + a = fluid.layers.data(name="a", shape=[1]) |
| 109 | + b = 10 / a |
| 110 | + place = fluid.CPUPlace() |
| 111 | + exe = fluid.Executor(place) |
| 112 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2 |
| 113 | + |
| 114 | + b_np = exe.run(fluid.default_main_program(), |
| 115 | + feed={"a": a_np}, |
| 116 | + fetch_list=[b]) |
| 117 | + self.assertTrue(numpy.allclose(10 / a_np, b_np)) |
| 118 | + |
| 119 | + @decorators.prog_scope() |
| 120 | + def test_div_two_tensor(self): |
| 121 | + a = fluid.layers.data(name="a", shape=[1]) |
| 122 | + b = fluid.layers.data(name="b", shape=[1]) |
| 123 | + c = a / b |
| 124 | + place = fluid.CPUPlace() |
| 125 | + exe = fluid.Executor(place) |
| 126 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 127 | + b_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2 |
| 128 | + c_np = exe.run(fluid.default_main_program(), |
| 129 | + feed={"a": a_np, |
| 130 | + 'b': b_np}, |
| 131 | + fetch_list=[c]) |
| 132 | + self.assertTrue(numpy.allclose(a_np / b_np, c_np)) |
| 133 | + |
| 134 | + @decorators.prog_scope() |
| 135 | + def test_mul_two_tensor(self): |
| 136 | + a = fluid.layers.data(name="a", shape=[1]) |
| 137 | + b = fluid.layers.data(name="b", shape=[1]) |
| 138 | + c = a * b |
| 139 | + place = fluid.CPUPlace() |
| 140 | + exe = fluid.Executor(place) |
| 141 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 142 | + b_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 143 | + c_np = exe.run(fluid.default_main_program(), |
| 144 | + feed={"a": a_np, |
| 145 | + 'b': b_np}, |
| 146 | + fetch_list=[c]) |
| 147 | + self.assertTrue(numpy.allclose(a_np * b_np, c_np)) |
| 148 | + |
| 149 | + @decorators.prog_scope() |
| 150 | + def test_add_two_tensor(self): |
| 151 | + a = fluid.layers.data(name="a", shape=[1]) |
| 152 | + b = fluid.layers.data(name="b", shape=[1]) |
| 153 | + c = a + b |
| 154 | + place = fluid.CPUPlace() |
| 155 | + exe = fluid.Executor(place) |
| 156 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 157 | + b_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 158 | + c_np = exe.run(fluid.default_main_program(), |
| 159 | + feed={"a": a_np, |
| 160 | + 'b': b_np}, |
| 161 | + fetch_list=[c]) |
| 162 | + self.assertTrue(numpy.allclose(a_np + b_np, c_np)) |
| 163 | + |
| 164 | + @decorators.prog_scope() |
| 165 | + def test_sub_two_tensor(self): |
| 166 | + a = fluid.layers.data(name="a", shape=[1]) |
| 167 | + b = fluid.layers.data(name="b", shape=[1]) |
| 168 | + c = a - b |
| 169 | + place = fluid.CPUPlace() |
| 170 | + exe = fluid.Executor(place) |
| 171 | + a_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 172 | + b_np = numpy.random.random(size=[10, 1]).astype('float32') |
| 173 | + c_np = exe.run(fluid.default_main_program(), |
| 174 | + feed={"a": a_np, |
| 175 | + 'b': b_np}, |
| 176 | + fetch_list=[c]) |
| 177 | + self.assertTrue(numpy.allclose(a_np - b_np, c_np)) |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == '__main__': |
| 181 | + unittest.main() |
0 commit comments