def test_ctc_add_blanks(): BATCHES = 3 N_LABELS = 3 y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks( y=y, blank_symbol=1, y_mask=y_mask) Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) Y_mask[-1, 0] = 0 Blanked_y_mask = blanked_y_mask.eval({y_mask: Y_mask}) Blanked_y = blanked_y.eval({y: Y}) assert (Blanked_y == np.array([[1, 1, 1], [0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype='int32')).all() assert (Blanked_y_mask == np.array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [0., 1., 1.], [0., 1., 1.]], dtype=floatX)).all()
def test_ctc_log_path_probabs(): LENGTH = 10 BATCHES = 3 CLASSES = 2 N_LABELS = 3 y_hat = T.tensor3('features') input_mask = T.matrix('features_mask') y_hat_mask = input_mask y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks( y=y, blank_symbol=1, y_mask=y_mask) p = ctc_cost._log_path_probabs(blanked_y, y_hat, blanked_y_mask, y_hat_mask, 1) Y_hat = np.zeros((LENGTH, BATCHES, CLASSES + 1), dtype=floatX) Y_hat[:, :, 0] = .7 Y_hat[:, :, 1] = .2 Y_hat[:, :, 2] = .1 Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_hat_mask = np.ones((LENGTH, BATCHES), dtype=floatX) Y_hat_mask[-2:, 0] = 0 Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) forward_probs = p.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask}) assert forward_probs[-2, 0, 0] == -np.inf Y_mask[-1] = 0 forward_probs_y_mask = p.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask}) assert forward_probs_y_mask[-1, 1, -2] == -np.inf assert not np.isnan(forward_probs).any()
def test_ctc_log_forward_backward(): LENGTH = 8 BATCHES = 4 CLASSES = 2 N_LABELS = 3 y_hat = T.tensor3('features') input_mask = T.matrix('features_mask') y_hat_mask = input_mask y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks(y=y, blank_symbol=1, y_mask=y_mask) f, b = ctc_cost._log_forward_backward(blanked_y, y_hat, blanked_y_mask, y_hat_mask, CLASSES) Y_hat = np.zeros((LENGTH, BATCHES, CLASSES + 1), dtype=floatX) Y_hat[:, :, 0] = .7 Y_hat[:, :, 1] = .2 Y_hat[:, :, 2] = .1 Y_hat[3, :, 0] = .3 Y_hat[3, :, 1] = .4 Y_hat[3, :, 2] = .3 Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_hat_mask = np.ones((LENGTH, BATCHES), dtype=floatX) Y_hat_mask[-2:] = 0 Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) Y_mask[-2:, 0] = 0 y_prob = ctc_cost._class_batch_to_labeling_batch(blanked_y, y_hat, y_hat_mask) forward_probs = f.eval({ y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask }) backward_probs = b.eval({ y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask }) y_probs = y_prob.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask}) assert not ((forward_probs + backward_probs)[:, 0, :] == -np.inf).all() marg = forward_probs + backward_probs - np.log(y_probs) forward_probs = np.exp(forward_probs) backward_probs = np.exp(backward_probs) L = (forward_probs * backward_probs[::-1][:, :, ::-1] / y_probs).sum(2) assert not np.isnan(forward_probs).any()
def test_ctc_log_forward_backward(): LENGTH = 8 BATCHES = 4 CLASSES = 2 N_LABELS = 3 y_hat = T.tensor3('features') input_mask = T.matrix('features_mask') y_hat_mask = input_mask y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks( y=y, blank_symbol=1, y_mask=y_mask) f, b = ctc_cost._log_forward_backward(blanked_y, y_hat, blanked_y_mask, y_hat_mask, CLASSES) Y_hat = np.zeros((LENGTH, BATCHES, CLASSES + 1), dtype=floatX) Y_hat[:, :, 0] = .7 Y_hat[:, :, 1] = .2 Y_hat[:, :, 2] = .1 Y_hat[3, :, 0] = .3 Y_hat[3, :, 1] = .4 Y_hat[3, :, 2] = .3 Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_hat_mask = np.ones((LENGTH, BATCHES), dtype=floatX) Y_hat_mask[-2:] = 0 Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) Y_mask[-2:, 0] = 0 y_prob = ctc_cost._class_batch_to_labeling_batch(blanked_y, y_hat, y_hat_mask) forward_probs = f.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask}) backward_probs = b.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask}) y_probs = y_prob.eval({y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask}) assert not ((forward_probs + backward_probs)[:, 0, :] == -np.inf).all() marg = forward_probs + backward_probs - np.log(y_probs) forward_probs = np.exp(forward_probs) backward_probs = np.exp(backward_probs) L = (forward_probs * backward_probs[::-1][:, :, ::-1] / y_probs).sum(2) assert not np.isnan(forward_probs).any()
def test_ctc_add_blanks(): BATCHES = 3 N_LABELS = 3 y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks(y=y, blank_symbol=1, y_mask=y_mask) Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) Y_mask[-1, 0] = 0 Blanked_y_mask = blanked_y_mask.eval({y_mask: Y_mask}) Blanked_y = blanked_y.eval({y: Y}) assert (Blanked_y == np.array([[1, 1, 1], [0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 1]], dtype='int32')).all() assert (Blanked_y_mask == np.array( [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [0., 1., 1.], [0., 1., 1.]], dtype=floatX)).all()
def test_ctc_log_path_probabs(): LENGTH = 10 BATCHES = 3 CLASSES = 2 N_LABELS = 3 y_hat = T.tensor3('features') input_mask = T.matrix('features_mask') y_hat_mask = input_mask y = T.lmatrix('phonemes') y_mask = T.matrix('phonemes_mask') blanked_y, blanked_y_mask = ctc_cost._add_blanks(y=y, blank_symbol=1, y_mask=y_mask) p = ctc_cost._log_path_probabs(blanked_y, y_hat, blanked_y_mask, y_hat_mask, 1) Y_hat = np.zeros((LENGTH, BATCHES, CLASSES + 1), dtype=floatX) Y_hat[:, :, 0] = .7 Y_hat[:, :, 1] = .2 Y_hat[:, :, 2] = .1 Y = np.zeros((N_LABELS, BATCHES), dtype='int64') Y_hat_mask = np.ones((LENGTH, BATCHES), dtype=floatX) Y_hat_mask[-2:, 0] = 0 Y_mask = np.asarray(np.ones_like(Y), dtype=floatX) forward_probs = p.eval({ y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask }) assert forward_probs[-2, 0, 0] == -np.inf Y_mask[-1] = 0 forward_probs_y_mask = p.eval({ y_hat: Y_hat, y: Y, y_hat_mask: Y_hat_mask, y_mask: Y_mask }) assert forward_probs_y_mask[-1, 1, -2] == -np.inf assert not np.isnan(forward_probs).any()