test_basic_rnn_name.py 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 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.

import unittest
16 17 18

from test_imperative_base import new_program_scope

19 20 21 22 23 24 25
import paddle.fluid as fluid
import paddle.fluid.layers as layers
from paddle.fluid.contrib.layers import basic_gru, basic_lstm


class TestBasicGRUApiName(unittest.TestCase):
    def setUp(self):
26 27 28 29 30 31 32 33 34 35 36 37
        self.name_set = set(
            [
                "test1_fw_w_0_gate",
                "test1_fw_w_0_candidate",
                "test1_fw_b_0_gate",
                "test1_fw_b_0_candidate",
                "test1_bw_w_0_gate",
                "test1_bw_w_0_candidate",
                "test1_bw_b_0_gate",
                "test1_bw_b_0_candidate",
            ]
        )
38 39 40 41 42 43 44 45 46 47 48

    def test_name(self):
        batch_size = 20
        input_size = 128
        hidden_size = 256
        num_layers = 1
        dropout = 0.5
        bidirectional = True
        batch_first = False

        with new_program_scope():
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            input = layers.data(
                name="input",
                shape=[-1, batch_size, input_size],
                dtype='float32',
            )
            pre_hidden = layers.data(
                name="pre_hidden", shape=[-1, hidden_size], dtype='float32'
            )
            sequence_length = layers.data(
                name="sequence_length", shape=[-1], dtype='int32'
            )

            rnn_out, last_hidden = basic_gru(
                input,
                pre_hidden,
                hidden_size,
                num_layers=num_layers,
                sequence_length=sequence_length,
                dropout_prob=dropout,
                bidirectional=bidirectional,
                batch_first=batch_first,
                param_attr=fluid.ParamAttr(name="test1"),
                bias_attr=fluid.ParamAttr(name="test1"),
                name="basic_gru",
            )
74 75

            var_list = fluid.io.get_program_parameter(
76 77
                fluid.default_main_program()
            )
78 79 80 81 82 83 84

            for var in var_list:
                self.assertTrue(var.name in self.name_set)


class TestBasicLSTMApiName(unittest.TestCase):
    def setUp(self):
85 86 87 88 89 90 91 92 93 94 95 96
        self.name_set = set(
            [
                "test1_fw_w_0",
                "test1_fw_b_0",
                "test1_fw_w_1",
                "test1_fw_b_1",
                "test1_bw_w_0",
                "test1_bw_b_0",
                "test1_bw_w_1",
                "test1_bw_b_1",
            ]
        )
97 98 99 100 101 102 103 104 105 106 107

    def test_name(self):
        batch_size = 20
        input_size = 128
        hidden_size = 256
        num_layers = 2
        dropout = 0.5
        bidirectional = True
        batch_first = False

        with new_program_scope():
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            input = layers.data(
                name="input",
                shape=[-1, batch_size, input_size],
                dtype='float32',
            )
            pre_hidden = layers.data(
                name="pre_hidden", shape=[-1, hidden_size], dtype='float32'
            )
            pre_cell = layers.data(
                name="pre_cell", shape=[-1, hidden_size], dtype='float32'
            )
            sequence_length = layers.data(
                name="sequence_length", shape=[-1], dtype='int32'
            )

            rnn_out, last_hidden, last_cell = basic_lstm(
                input,
                pre_hidden,
                pre_cell,
                hidden_size,
                num_layers=num_layers,
                sequence_length=sequence_length,
                dropout_prob=dropout,
                bidirectional=bidirectional,
                param_attr=fluid.ParamAttr(name="test1"),
                bias_attr=fluid.ParamAttr(name="test1"),
                batch_first=batch_first,
            )
136 137

            var_list = fluid.io.get_program_parameter(
138 139
                fluid.default_main_program()
            )
140 141 142 143 144 145 146

            for var in var_list:
                self.assertTrue(var.name in self.name_set)


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