test_math_op_patch.py 9.6 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2
#
Y
Yang Yu 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
Y
Yang Yu 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
Y
Yang Yu 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from __future__ import print_function, division
16

Y
Yang Yu 已提交
17
import unittest
18
from decorator_helper import prog_scope
19
import paddle
20
import paddle.fluid as fluid
Y
Yang Yu 已提交
21 22 23 24
import numpy


class TestMathOpPatches(unittest.TestCase):
25 26 27
    def setUp(self):
        paddle.enable_static()

28
    @prog_scope()
Y
Yang Yu 已提交
29 30 31
    def test_add_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = a + 10
32 33 34 35
        ab = fluid.layers.concat(input=[a, b], axis=1)
        c = ab + 10
        d = ab + a
        # e = a + ab
Y
Yang Yu 已提交
36 37 38
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
39 40 41
        b_np, c_np, d_np = exe.run(fluid.default_main_program(),
                                   feed={"a": a_np},
                                   fetch_list=[b, c, d])
Y
Yang Yu 已提交
42
        self.assertTrue(numpy.allclose(a_np + 10, b_np))
43 44 45 46
        ab_np = numpy.concatenate([a_np, b_np], axis=1)
        self.assertTrue(numpy.allclose(ab_np + 10, c_np))
        d_expected = ab_np + numpy.concatenate([a_np, a_np], axis=1)
        self.assertTrue(numpy.allclose(d_expected, d_np))
Y
Yang Yu 已提交
47

48
    @prog_scope()
Y
Yang Yu 已提交
49 50 51 52 53 54 55 56 57 58 59
    def test_radd_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = 10 + a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(a_np + 10, b_np))

60
    @prog_scope()
Y
Yang Yu 已提交
61 62 63 64 65 66 67 68 69 70 71
    def test_sub_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = a - 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(a_np - 10, b_np))

72
    @prog_scope()
Y
Yang Yu 已提交
73 74 75 76 77 78 79 80 81 82 83
    def test_radd_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = 10 - a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(10 - a_np, b_np))

84
    @prog_scope()
Y
Yang Yu 已提交
85 86 87 88 89 90 91 92 93 94 95
    def test_mul_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = a * 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(a_np * 10, b_np))

96
    @prog_scope()
Y
Yang Yu 已提交
97 98 99 100 101 102 103 104 105 106 107
    def test_rmul_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = 10 * a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(10 * a_np, b_np))

108
    @prog_scope()
Y
Yang Yu 已提交
109 110 111 112 113 114 115 116 117 118 119
    def test_div_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = a / 10
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(a_np / 10, b_np))

120
    @prog_scope()
Y
Yang Yu 已提交
121 122 123 124 125 126 127 128 129 130 131 132
    def test_rdiv_scalar(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = 10 / a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2

        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(10 / a_np, b_np))

133
    @prog_scope()
Y
Yang Yu 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
    def test_div_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        c = a / b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = numpy.random.random(size=[10, 1]).astype('float32') + 1e-2
        c_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np,
                             'b': b_np},
                       fetch_list=[c])
        self.assertTrue(numpy.allclose(a_np / b_np, c_np))

148
    @prog_scope()
Y
Yang Yu 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    def test_mul_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        c = a * b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = numpy.random.random(size=[10, 1]).astype('float32')
        c_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np,
                             'b': b_np},
                       fetch_list=[c])
        self.assertTrue(numpy.allclose(a_np * b_np, c_np))

163
    @prog_scope()
Y
Yang Yu 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
    def test_add_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        c = a + b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = numpy.random.random(size=[10, 1]).astype('float32')
        c_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np,
                             'b': b_np},
                       fetch_list=[c])
        self.assertTrue(numpy.allclose(a_np + b_np, c_np))

178
    @prog_scope()
Y
Yang Yu 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192
    def test_sub_two_tensor(self):
        a = fluid.layers.data(name="a", shape=[1])
        b = fluid.layers.data(name="b", shape=[1])
        c = a - b
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.random(size=[10, 1]).astype('float32')
        b_np = numpy.random.random(size=[10, 1]).astype('float32')
        c_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np,
                             'b': b_np},
                       fetch_list=[c])
        self.assertTrue(numpy.allclose(a_np - b_np, c_np))

193 194 195
    @prog_scope()
    def test_integer_div(self):
        a = fluid.layers.data(name="a", shape=[1], dtype='int64')
S
ShenLiang 已提交
196
        b = a / 7
197 198
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
S
ShenLiang 已提交
199
        a_np = numpy.array([3, 4, 10, 14, 9, 18]).astype('int64')
200 201 202
        b_np, = exe.run(fluid.default_main_program(),
                        feed={"a": a_np},
                        fetch_list=[b])
S
ShenLiang 已提交
203

204 205
        b_np_actual = (a_np / 7).astype('float32')
        self.assertTrue(numpy.allclose(b_np, b_np_actual))
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    @prog_scope()
    def test_equal(self):
        a = fluid.layers.data(name="a", shape=[1], dtype='float32')
        b = fluid.layers.data(name="b", shape=[1], dtype='float32')
        c = (a == b)

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.array([3, 4, 10, 14, 9, 18]).astype('float32')
        b_np = numpy.array([3, 4, 11, 15, 8, 18]).astype('float32')

        c_np, = exe.run(fluid.default_main_program(),
                        feed={"a": a_np,
                              "b": b_np},
                        fetch_list=[c])

        self.assertTrue(numpy.array_equal(c_np, a_np == b_np))
        self.assertEqual(c.dtype, fluid.core.VarDesc.VarType.BOOL)

    @prog_scope()
    def test_equal_and_cond(self):
        a = fluid.layers.data(name="a", shape=[1], dtype='float32')
        b = fluid.layers.data(name="b", shape=[1], dtype='float32')

        one = fluid.layers.ones(shape=[1], dtype='int32')
        zero = fluid.layers.zeros(shape=[1], dtype='int32')
        cond = (one == zero)
        c = fluid.layers.cond(cond, lambda: a + b, lambda: a - b)

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.array([3, 4, 10, 14, 9, 18]).astype('float')
        b_np = numpy.array([3, 4, 11, 15, 8, 18]).astype('float')
        c_np, = exe.run(fluid.default_main_program(),
                        feed={"a": a_np,
                              "b": b_np},
                        fetch_list=[c])

        self.assertTrue(numpy.array_equal(c_np, a_np - b_np))

247 248 249 250 251 252 253 254 255 256 257 258 259
    @prog_scope()
    def test_neg(self):
        a = fluid.layers.data(name="a", shape=[10, 1])
        b = -a
        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        a_np = numpy.random.uniform(-1, 1, size=[10, 1]).astype('float32')

        b_np = exe.run(fluid.default_main_program(),
                       feed={"a": a_np},
                       fetch_list=[b])
        self.assertTrue(numpy.allclose(-a_np, b_np))

Y
Yang Yu 已提交
260 261 262

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