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 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# 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.

from __future__ import print_function

import unittest
import numpy
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.core as core
from paddle.fluid.contrib.layers import basic_gru, basic_lstm
from paddle.fluid.executor import Executor
from paddle.fluid import framework
from test_imperative_base import new_program_scope
import numpy as np


class TestBasicGRUApiName(unittest.TestCase):
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    def setUp(self):
        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"
        ])

    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
            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')
58 59 60 61 62 63 64 65 66 67 68 69 70 71


            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")

            var_list = fluid.io.get_program_parameter(
                fluid.default_main_program())

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


class TestBasicLSTMApiName(unittest.TestCase):
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    def setUp(self):
        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"
        ])

    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():
89 90 91 92 93 94 95 96 97 98 99 100
            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')
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

            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)

            var_list = fluid.io.get_program_parameter(
                fluid.default_main_program())

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


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