Exemple #1
0
    def test_tanh_default_output(self):
        cases = [
            ParameterSet(2, 10, 3, 20, 15)
        ]
        for case in cases:
            input = self._make_input(case)
            hidden = self._make_hidden(case)
            weights = self._make_weights(case)
            bias = self._make_bias(case)

            def build_network(network):
                h = network.add_input(
                    "input", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(
                        case.batch_size, case.seq_len, case.data_size))
                h_hidden = network.add_input(
                    "hidden", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(
                        case.batch_size, case.num_layers, case.hidden_size))
                h, _ = L.rnn_tanh_v2(h, case.seq_len, weights, bias,
                                     hidden_state=h_hidden)
                network.mark_output("output", h)
            actual = execute_inference(
                {"input": input, "hidden": hidden}, build_network)

            expect = unidirection_rnn(
                input, hidden, weights, bias, lambda x: np.tanh(x))
            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))
Exemple #2
0
    def test_brnn_relu_default_output(self):
        cases = [ParameterSet(2, 10, 3, 20, 15)]
        for case in cases:
            input = self._make_input(case)
            hidden = self._make_hidden(case)
            fwd_weights = self._make_weights(case)
            bwd_weights = self._make_weights(case)
            fwd_bias = self._make_bias(case)
            bwd_bias = self._make_bias(case)

            def build_network(network):
                h = network.add_input(
                    "input", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(case.seq_len, case.batch_size,
                                          case.data_size))
                h_hidden = network.add_input(
                    "hidden", turret.DataType.FLOAT,
                    turret.Dimensions.CHW(case.num_layers, case.batch_size,
                                          case.hidden_size * 2))
                h, _ = L.brnn_relu(h,
                                   case.seq_len,
                                   fwd_weights,
                                   bwd_weights,
                                   fwd_bias,
                                   bwd_bias,
                                   hidden_state=h_hidden)
                network.mark_output("output", h)

            actual = execute_inference({
                "input": input,
                "hidden": hidden
            },
                                       build_network,
                                       max_batch_size=1)

            t_input = input.transpose(0, 2, 1, 3)
            t_hidden = hidden.transpose(0, 2, 1, 3)
            t_expect = bidirection_rnn(t_input, t_hidden, fwd_weights,
                                       bwd_weights, fwd_bias, bwd_bias,
                                       lambda x: np.maximum(x, 0.0))
            expect = t_expect.transpose(0, 2, 1, 3)

            self.assertEqual(expect.shape, actual.shape)
            self.assertTrue(np.allclose(expect, actual))