def test_loss_gradient_batch_mode(self, art_warning, batch_mode, audio_data): try: import tensorflow.compat.v1 as tf1 test_input = audio_data test_target = np.array(["This", "is", "a dummy", "a dummy"]) lingvo = TensorFlowLingvoASR() if batch_mode: gradients = lingvo._loss_gradient_per_batch( test_input, test_target) else: gradients = lingvo._loss_gradient_per_sequence( test_input, test_target) gradients_abs_sum = np.array([np.abs(g).sum() for g in gradients], dtype=object) # test shape, equal inputs have equal gradients, non-zero inputs have non-zero gradient sums assert [x.shape for x in test_input] == [g.shape for g in gradients] assert_allclose(np.abs(gradients[2]).sum(), np.abs(gradients[3]).sum(), rtol=1e-01) assert_array_equal(gradients_abs_sum > 0, [False, True, True, True]) except ARTTestException as e: art_warning(e)
def test_loss_gradient(self, art_warning): try: transcripts = list() audios = list() for filename, sample in self.samples.items(): file_path = get_file(filename, sample["uri"]) _, audio = read(file_path) audios.append(audio) transcripts.append(sample["transcript"]) audio_batch = np.array(audios, dtype=object) target_batch = np.array(transcripts) lingvo = TensorFlowLingvoASR() gradient_batch = lingvo._loss_gradient_per_batch( audio_batch, target_batch) gradient_sequence = lingvo._loss_gradient_per_sequence( audio_batch, target_batch) gradient_batch_sum = np.array( [np.abs(gb).sum() for gb in gradient_batch], dtype=object) gradient_sequence_sum = np.array( [np.abs(gs).sum() for gs in gradient_sequence], dtype=object) # test loss gradients per batch and per sequence are the same assert_allclose(gradient_sequence_sum, gradient_batch_sum, rtol=1e-05) # test gradient_batch, gradient_sequence and audios items have same shapes assert ([gb.shape for gb in gradient_batch] == [gs.shape for gs in gradient_sequence] == [a.shape for a in audios]) except ARTTestException as e: art_warning(e)