test_detach.py 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from __future__ import print_function

import numpy as np
import paddle.fluid as fluid

20
from paddle.fluid.dygraph import Linear
21 22 23 24 25 26 27 28 29 30 31 32 33 34
from paddle.fluid.dygraph.base import to_variable

import unittest


class Test_Detach(unittest.TestCase):
    def generate_Data(self):
        data = np.array(
            [[1, 8, 3, 9], [7, 20, 9, 6], [4, 6, 8, 10]]).astype('float32')
        return data

    def no_detach_multi(self):
        data = self.generate_Data()
        with fluid.dygraph.guard():
35
            linear_w_param_attrs = fluid.ParamAttr(
36
                initializer=fluid.initializer.Constant(5.0))
37
            linear_b_param_attrs = fluid.ParamAttr(
38
                initializer=fluid.initializer.Constant(6.0))
39 40 41 42 43 44
            linear = Linear(
                4,
                10,
                param_attr=linear_w_param_attrs,
                bias_attr=linear_b_param_attrs)
            linear1_w_param_attrs = fluid.ParamAttr(
45
                initializer=fluid.initializer.Constant(7.0))
46
            linear1_b_param_attrs = fluid.ParamAttr(
47
                initializer=fluid.initializer.Constant(8.0))
48 49 50 51 52 53
            linear1 = Linear(
                10,
                1,
                param_attr=linear1_w_param_attrs,
                bias_attr=linear1_b_param_attrs)
            linear2_w_param_attrs = fluid.ParamAttr(
54
                initializer=fluid.initializer.Constant(9.0))
55
            linear2_b_param_attrs = fluid.ParamAttr(
56
                initializer=fluid.initializer.Constant(10.0))
57 58 59 60 61
            linear2 = Linear(
                10,
                1,
                param_attr=linear2_w_param_attrs,
                bias_attr=linear2_b_param_attrs)
62
            data = to_variable(data)
63 64 65
            x = linear(data)
            x1 = linear1(x)
            x2 = linear2(x)
66 67 68 69 70 71 72 73
            loss = x1 + x2
            # print(loss, loss.shape)
            loss.backward()
            return x.gradient()

    def no_detach_single(self):
        data = self.generate_Data()
        with fluid.dygraph.guard():
74
            linear_w_param_attrs = fluid.ParamAttr(
75
                initializer=fluid.initializer.Constant(5.0))
76
            linear_b_param_attrs = fluid.ParamAttr(
77
                initializer=fluid.initializer.Constant(6.0))
78 79 80 81 82 83
            linear = Linear(
                4,
                10,
                param_attr=linear_w_param_attrs,
                bias_attr=linear_b_param_attrs)
            linear1_w_param_attrs = fluid.ParamAttr(
84
                initializer=fluid.initializer.Constant(7.0))
85
            linear1_b_param_attrs = fluid.ParamAttr(
86
                initializer=fluid.initializer.Constant(8.0))
87 88 89 90 91
            linear1 = Linear(
                10,
                1,
                param_attr=linear1_w_param_attrs,
                bias_attr=linear1_b_param_attrs)
92
            data = to_variable(data)
93 94
            x = linear(data)
            x1 = linear1(x)
95 96 97 98 99 100 101 102
            loss = x1
            # print(loss, loss.shape)
            loss.backward()
            return x.gradient()

    def detach_multi(self):
        data = self.generate_Data()
        with fluid.dygraph.guard():
103
            linear_w_param_attrs = fluid.ParamAttr(
104
                initializer=fluid.initializer.Constant(5.0))
105
            linear_b_param_attrs = fluid.ParamAttr(
106
                initializer=fluid.initializer.Constant(6.0))
107 108 109 110 111 112
            linear = Linear(
                4,
                10,
                param_attr=linear_w_param_attrs,
                bias_attr=linear_b_param_attrs)
            linear1_w_param_attrs = fluid.ParamAttr(
113
                initializer=fluid.initializer.Constant(7.0))
114
            linear1_b_param_attrs = fluid.ParamAttr(
115
                initializer=fluid.initializer.Constant(8.0))
116 117 118 119 120 121
            linear1 = Linear(
                10,
                1,
                param_attr=linear1_w_param_attrs,
                bias_attr=linear1_b_param_attrs)
            linear2_w_param_attrs = fluid.ParamAttr(
122
                initializer=fluid.initializer.Constant(9.0))
123
            linear2_b_param_attrs = fluid.ParamAttr(
124
                initializer=fluid.initializer.Constant(10.0))
125 126 127 128 129
            linear2 = Linear(
                10,
                1,
                param_attr=linear2_w_param_attrs,
                bias_attr=linear2_b_param_attrs)
130
            data = to_variable(data)
131
            x = linear(data)
132
            x_detach = x.detach()
133 134
            x1 = linear1(x)
            x2 = linear2(x_detach)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
            loss = x1 + x2
            # print(loss, loss.shape)
            loss.backward()
            return x.gradient()

    def test_NoDetachMulti_DetachMulti(self):
        array_no_detach_multi = self.no_detach_multi()
        array_detach_multi = self.detach_multi()

        assert not np.array_equal(array_no_detach_multi, array_detach_multi)

    def test_NoDetachSingle_DetachMulti(self):
        array_no_detach_single = self.no_detach_single()
        array_detach_multi = self.detach_multi()
        assert np.array_equal(array_no_detach_single, array_detach_multi)

    def test_detach_exception(self):
        x = fluid.layers.data(name="a", shape=[3, 4], dtype='float32')
        y = fluid.layers.fc(input=x, size=10, bias_attr=True)
        try:
            y_detach = y.detach()
        except Exception as e:
157 158 159 160 161
            # Here is to check
            assert type(e) == AssertionError
            assert str(
                e
            ) == 'We Only support detach in Dygraph mode, please use fluid.dygraph.guard() as context to run it in Dygraph Mode'
162 163 164 165


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