コード例 #1
0
def test_reward_matrix():
    matrix = reward_matrix('abc$', 'abc$', 'abc$', eos_label=3)
    should_be = numpy.array([[0, -1, -1, -3], [-1, 0, -1, -2], [-1, -1, 0, -1],
                             [-1, -1, -1, 0], [-1, -1, -1, -1]])
    assert_equal(matrix, should_be)
    matrix = reward_matrix('abc$', 'acb$', 'abc$', eos_label=3)
    should_be = numpy.array([[0, -1, -1, -3], [-1, 0, -1, -2],
                             [-2, -1, -1, -1], [-2, -2, -1, -2],
                             [-3, -3, -2, -2]])
    assert_equal(matrix, should_be)
コード例 #2
0
def test_reward_matrix():
    matrix = reward_matrix('abc$', 'abc$', 'abc$', eos_label=3)
    should_be = numpy.array([[ 0, -1, -1, -3],
                             [-1,  0, -1, -2],
                             [-1, -1,  0, -1],
                             [-1, -1, -1,  0],
                             [-1, -1, -1, -1]])
    assert_equal(matrix, should_be)
    matrix = reward_matrix('abc$', 'acb$', 'abc$', eos_label=3)
    should_be = numpy.array([[ 0, -1, -1, -3],
                             [-1,  0, -1, -2],
                             [-2, -1,  -1, -1],
                             [-2, -2, -1,  -2],
                             [-3, -3, -2,  -2]])
    assert_equal(matrix, should_be)
コード例 #3
0
    def perform(self, node, inputs, output_storage):
        groundtruth, recognized = inputs
        if (groundtruth.ndim != 2 or recognized.ndim != 2
                or groundtruth.shape[1] != recognized.shape[1]):
            raise ValueError
        batch_size = groundtruth.shape[1]
        all_rewards = numpy.zeros(recognized.shape + (self.alphabet_size, ),
                                  dtype='int64')
        all_gains = numpy.zeros(recognized.shape + (self.alphabet_size, ),
                                dtype='int64')
        alphabet = list(range(self.alphabet_size))
        for index in range(batch_size):
            y = list(groundtruth[:, index])
            y_hat = list(recognized[:, index])
            try:
                eos_pos = y.index(self.eos_label)
                y = y[:eos_pos + 1]
            except:
                # Sometimes groundtruth is in fact also a prediction
                # and in this case it might not have EOS label
                pass
            if self.eos_label in y_hat:
                y_hat_eos_pos = y_hat.index(self.eos_label)
                y_hat_trunc = y_hat[:y_hat_eos_pos + 1]
            else:
                y_hat_trunc = y_hat
            rewards_trunc = reward_matrix(y, y_hat_trunc, alphabet,
                                          self.eos_label)
            # pass freshly computed rewards to gain_matrix to speed things up
            # a bit
            gains_trunc = gain_matrix(y,
                                      y_hat_trunc,
                                      alphabet,
                                      given_reward_matrix=rewards_trunc)
            gains = numpy.ones((len(y_hat), len(alphabet))) * -1000
            gains[:(gains_trunc.shape[0] - 1), :] = gains_trunc[:-1, :]

            rewards = numpy.ones((len(y_hat), len(alphabet))) * -1
            rewards[:(rewards_trunc.shape[0] - 1), :] = rewards_trunc[:-1, :]
            all_rewards[:, index, :] = rewards
            all_gains[:, index, :] = gains

        output_storage[0][0] = all_rewards
        output_storage[1][0] = all_gains
コード例 #4
0
ファイル: ops.py プロジェクト: DingKe/attention-lvcsr
    def perform(self, node, inputs, output_storage):
        groundtruth, recognized = inputs
        if (groundtruth.ndim != 2 or recognized.ndim != 2
                or groundtruth.shape[1] != recognized.shape[1]):
            raise ValueError
        batch_size = groundtruth.shape[1]
        all_rewards = numpy.zeros(
            recognized.shape + (self.alphabet_size,), dtype='int64')
        all_gains = numpy.zeros(
            recognized.shape + (self.alphabet_size,), dtype='int64')
        alphabet = list(range(self.alphabet_size))
        for index in range(batch_size):
            y = list(groundtruth[:, index])
            y_hat = list(recognized[:, index])
            try:
                eos_pos = y.index(self.eos_label)
                y = y[:eos_pos + 1]
            except:
                # Sometimes groundtruth is in fact also a prediction
                # and in this case it might not have EOS label
                pass
            if self.eos_label in y_hat:
                y_hat_eos_pos = y_hat.index(self.eos_label)
                y_hat_trunc = y_hat[:y_hat_eos_pos + 1]
            else:
                y_hat_trunc = y_hat
            rewards_trunc = reward_matrix(
                y, y_hat_trunc, alphabet, self.eos_label)
            # pass freshly computed rewards to gain_matrix to speed things up
            # a bit
            gains_trunc = gain_matrix(y, y_hat_trunc, alphabet,
                                      given_reward_matrix=rewards_trunc)
            gains = numpy.ones((len(y_hat), len(alphabet))) * -1000
            gains[:(gains_trunc.shape[0] - 1), :] = gains_trunc[:-1, :]

            rewards = numpy.ones((len(y_hat), len(alphabet))) * -1
            rewards[:(rewards_trunc.shape[0] - 1), :] = rewards_trunc[:-1, :]
            all_rewards[:, index, :] = rewards
            all_gains[:, index, :] = gains

        output_storage[0][0] = all_rewards
        output_storage[1][0] = all_gains