Esempio n. 1
0
    def __init__(self, compute_grad=True):
        if not ctc_available():
            raise RuntimeError("Baidu CTC is not available and "
                               "GpuConnectionistTemporalClassification Op "
                               "can not be constructed.")

        self.compute_grad = compute_grad
        # Return only the cost. Gradient will be returned by grad()
        self.default_output = 0

        super().__init__(self.func_file, self.func_name)
Esempio n. 2
0
import aesara
import aesara.gpuarray
from aesara.gpuarray.ctc import GpuConnectionistTemporalClassification, gpu_ctc
from aesara.gradient import grad
from aesara.tensor.math import mean
from aesara.tensor.nnet.ctc import (
    ConnectionistTemporalClassification,
    ctc,
    ctc_available,
)
from tests import unittest_tools as utt
from tests.gpuarray.config import mode_with_gpu, mode_without_gpu
from tests.tensor.nnet.test_ctc import setup_ctc_case, setup_grad_case, setup_torch_case


@pytest.mark.skipif(not ctc_available(),
                    reason="Optional library warp-ctc not available")
class TestCTC:
    def check_ctc(self, activations, labels, input_length, expected_costs,
                  expected_grads):
        # Create symbolic variables
        t_activations = aesara.shared(activations, name="activations")
        t_activation_times = aesara.shared(input_length,
                                           name="activation_times")
        t_labels = aesara.shared(labels, name="labels")

        inputs = [t_activations, t_labels, t_activation_times]

        # Execute several tests for each test case
        self.check_expected_values(t_activations, t_labels, t_activation_times,
                                   expected_costs, expected_grads)
Esempio n. 3
0
        [
            [[0.1, 0.6, 0.1, 0.1, 0.1], [0.1, 0.1, 0.6, 0.1, 0.1]],
            [[0.6, 0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.5, 0.2, 0.1]],
        ],
        dtype=np.float32,
    )

    activation_times = np.asarray([2, 2], dtype=np.int32)

    labels = np.asarray([[1, 2], [1, 2]], dtype=np.int32)

    return [activations, labels, activation_times]


@pytest.mark.skipif(
    not ctc_available(), reason="Optional library warp-ctc not available"
)
@pytest.mark.skipif(
    aesara.config.mode == "FAST_COMPILE" or aesara.config.cxx == "",
    reason="We need a c compiler",
)
class TestCTC:
    """
    Test Baidu CTC wrapper implementation.

    Expected values for costs and gradients are obtained through an external
    C implementation, that uses the library directly.
    """

    def run_ctc(
        self, activations, labels, input_length, expected_costs, expected_grads