test_while_op.py 2.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
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.

Y
Yang Yang(Tony) 已提交
15
import unittest
16 17 18 19
import paddle.fluid.layers as layers
from paddle.fluid.executor import Executor
import paddle.fluid.core as core
from paddle.fluid.backward import append_backward
Y
Yang Yang(Tony) 已提交
20 21 22 23 24 25
import numpy


class TestWhileOp(unittest.TestCase):
    def test_simple_forward(self):
        d0 = layers.data(
F
fengjiayi 已提交
26
            "d0", shape=[10], append_batch_size=False, dtype='float32')
Y
Yang Yang(Tony) 已提交
27
        d1 = layers.data(
F
fengjiayi 已提交
28
            "d1", shape=[10], append_batch_size=False, dtype='float32')
Y
Yang Yang(Tony) 已提交
29
        d2 = layers.data(
F
fengjiayi 已提交
30
            "d2", shape=[10], append_batch_size=False, dtype='float32')
Y
Yang Yang(Tony) 已提交
31 32 33
        i = layers.zeros(shape=[1], dtype='int64')
        i.stop_gradient = True
        init = layers.zeros(shape=[10], dtype='float32')
Y
Yang Yang(Tony) 已提交
34
        mem_array = layers.array_write(x=init, i=i)
Y
Yang Yang(Tony) 已提交
35 36 37 38 39 40 41 42 43 44 45 46
        data_array = layers.array_write(x=d0, i=i)

        i = layers.increment(i)
        layers.array_write(d1, i, array=data_array)

        i = layers.increment(i)
        layers.array_write(d2, i, array=data_array)

        i = layers.zeros(shape=[1], dtype='int64')
        i.stop_gradient = True

        array_len = layers.fill_constant(shape=[1], dtype='int64', value=3)
Y
Yang Yang(Tony) 已提交
47
        array_len.stop_gradient = True
Y
Yang Yang(Tony) 已提交
48 49 50 51 52 53 54
        cond = layers.less_than(x=i, y=array_len)

        while_op = layers.While(cond=cond)
        with while_op.block():
            d = layers.array_read(array=data_array, i=i)
            prev = layers.array_read(array=mem_array, i=i)
            result = layers.sums(input=[d, prev])
Y
Yang Yang(Tony) 已提交
55 56

            i = layers.increment(x=i, in_place=True)
Y
Yang Yang(Tony) 已提交
57 58
            layers.array_write(result, i=i, array=mem_array)
            layers.less_than(x=i, y=array_len, cond=cond)
Y
Yang Yang(Tony) 已提交
59 60

        sum_result = layers.array_read(array=mem_array, i=i)
Y
Yu Yang 已提交
61
        loss = layers.mean(sum_result)
Y
Yang Yang(Tony) 已提交
62

F
fengjiayi 已提交
63
        append_backward(loss)
Y
Yang Yang(Tony) 已提交
64 65 66 67 68

        cpu = core.CPUPlace()
        exe = Executor(cpu)
        d = []

69
        for i in range(3):
Y
Yang Yang(Tony) 已提交
70 71
            d.append(numpy.random.random(size=[10]).astype('float32'))

D
dzhwinter 已提交
72 73 74 75
        outs = exe.run(feed={'d0': d[0],
                             'd1': d[1],
                             'd2': d[2]},
                       fetch_list=[sum_result])
Y
Yang Yang(Tony) 已提交
76 77 78 79 80
        self.assertAlmostEqual(numpy.sum(d), numpy.sum(outs[0]), delta=0.01)


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