kaldi_test.py 3.79 KB
Newer Older
1
"""
2
 Copyright (c) 2018-2019 Intel Corporation
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 30 31

 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

import numpy as np

from mo.graph.graph import Node
from mo.pipeline.kaldi import apply_biases_to_last_layer
from mo.utils.unittest.graph import build_graph


class TestKaldiPipeline(unittest.TestCase):
    def test_apply_biases_to_ScaleShift(self):
        nodes = {'input': {'kind': 'data'},
                 'weights': {'value': None, 'kind': 'data'},
                 'biases': {'value': np.zeros(10), 'kind': 'data'},
                 'sc': {'op': 'ScaleShift', 'kind': 'op'},
32
                 'output': {'kind': 'data'},
33
                 'op_output': {'op': 'Result', 'kind': 'op'}
34 35 36 37 38 39
                 }
        graph = build_graph(nodes,
                            [
                                ('input', 'sc'),
                                ('weights', 'sc'),
                                ('biases', 'sc'),
40 41 42
                                ('sc', 'output'),
                                ('output', 'op_output')
                            ])
43 44 45 46 47 48 49 50 51
        counts = -0.5 * np.ones(10)
        apply_biases_to_last_layer(graph, counts)
        sc_node = Node(graph, 'sc')
        self.assertTrue(np.array_equal(sc_node.in_node(2).value, -counts))

    def test_apply_biases_to_FullyConnected(self):
        nodes = {'input': {'kind': 'data'},
                 'weights': {'kind': 'data'},
                 'biases': {'value': None, 'shape': None, 'kind': 'data'},
52
                 'fc': {'op': 'MatMul', 'kind': 'op'},
53
                 'output': {'kind': 'data'},
54
                 'op_output': {'op': 'Result', 'kind': 'op'}
55 56 57 58 59 60
                 }
        graph = build_graph(nodes,
                            [
                                ('input', 'fc'),
                                ('weights', 'fc'),
                                ('biases', 'fc'),
61 62 63
                                ('fc', 'output'),
                                ('output', 'op_output')
                            ])
64 65 66 67 68 69 70 71 72
        counts = -0.5 * np.ones(10)
        apply_biases_to_last_layer(graph, counts)
        fc_node = Node(graph, 'fc')
        self.assertTrue(np.array_equal(fc_node.in_node(2).value, -counts))

    def test_apply_biases_to_graph_with_SoftMax(self):
        nodes = {'input': {'kind': 'data'},
                 'weights': {'value': None, 'kind': 'data'},
                 'biases': {'value': None, 'shape': None, 'kind': 'data'},
73
                 'fc': {'op': 'MatMul', 'kind': 'op'},
74 75
                 'data': {'kind': 'data'},
                 'softmax': {'op': 'SoftMax', 'kind': 'op'},
76
                 'output': {'kind': 'data'},
77
                 'op_output': {'op': 'Result', 'kind': 'op'}
78 79 80 81 82 83 84 85
                 }
        graph = build_graph(nodes,
                            [
                                ('input', 'fc'),
                                ('weights', 'fc'),
                                ('biases', 'fc'),
                                ('fc', 'data'),
                                ('data', 'softmax'),
86 87 88
                                ('softmax', 'output'),
                                ('output', 'op_output')
                            ])
89 90 91 92
        counts = -0.5 * np.ones(10)
        apply_biases_to_last_layer(graph, counts)
        fc_node = Node(graph, 'fc')
        self.assertTrue(np.array_equal(fc_node.in_node(2).value, -counts))