예제 #1
0
    def testDecoderFPropDouble(self):
        with self.session(use_gpu=False):
            tf.set_random_seed(8372749040)
            np.random.seed(827374)

            p = self._DecoderParams(
                vn_config=py_utils.VariationalNoiseParams(None, False, False))
            p.dtype = tf.float64

            dec = decoder.AsrDecoder(p)
            src_seq_len = 5
            src_enc = tf.constant(np.random.uniform(size=(src_seq_len, 2, 8)),
                                  tf.float64)
            src_enc_padding = tf.constant(
                [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 1.0], [1.0, 1.0]],
                dtype=tf.float64)
            target_ids = tf.transpose(
                tf.constant([[0, 1, 2, 3], [1, 2, 3, 4], [10, 11, 12, 15],
                             [5, 6, 7, 8], [10, 5, 2, 5]],
                            dtype=tf.int32))
            target_labels = tf.transpose(
                tf.constant([[0, 1, 2, 3], [1, 2, 3, 4], [10, 11, 12, 13],
                             [5, 7, 8, 10], [10, 5, 2, 4]],
                            dtype=tf.int32))
            target_paddings = tf.transpose(
                tf.constant([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0],
                             [0, 1, 0, 0], [1, 1, 1, 1]],
                            dtype=tf.float64))
            target_transcripts = tf.constant(
                ['abcd', 'bcde', 'klmp', 'fghi', 'kfcf'])
            target_weights = 1.0 - target_paddings
            targets = py_utils.NestedMap({
                'ids': target_ids,
                'labels': target_labels,
                'weights': target_weights,
                'paddings': target_paddings,
                'transcripts': target_transcripts,
            })
            encoder_outputs = py_utils.NestedMap(encoded=src_enc,
                                                 padding=src_enc_padding)
            metrics = dec.FPropDefaultTheta(encoder_outputs, targets)
            loss = metrics['loss'][0]

            tf.global_variables_initializer().run()

            test_utils.CompareToGoldenSingleFloat(self, 3.483581, loss.eval())
            # Second run to make sure the function is determistic.
            test_utils.CompareToGoldenSingleFloat(self, 3.483581, loss.eval())
예제 #2
0
    def _testDecoderFPropHelper(self, params):
        """Computes decoder from params and computes loss with random inputs."""
        dec = decoder.AsrDecoder(params)
        src_seq_len = 5
        src_enc = tf.random_normal([src_seq_len, 2, 8],
                                   seed=982774838,
                                   dtype=py_utils.FPropDtype(params))
        src_enc_padding = tf.constant(
            [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 1.0], [1.0, 1.0]],
            dtype=py_utils.FPropDtype(params))
        encoder_outputs = py_utils.NestedMap(encoded=src_enc,
                                             padding=src_enc_padding)
        # shape=[4, 5]
        target_ids = tf.transpose(
            tf.constant([[0, 1, 2, 3], [1, 2, 3, 4], [10, 11, 12, 15],
                         [5, 6, 7, 8], [10, 5, 2, 5]],
                        dtype=tf.int32))
        # shape=[4, 5]
        target_labels = tf.transpose(
            tf.constant([[0, 1, 2, 3], [1, 2, 3, 4], [10, 11, 12, 13],
                         [5, 7, 8, 10], [10, 5, 2, 4]],
                        dtype=tf.int32))
        # shape=[4, 5]
        target_paddings = tf.transpose(
            tf.constant([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0],
                         [0, 1, 0, 0], [1, 1, 1, 0]],
                        dtype=py_utils.FPropDtype(params)))
        target_transcripts = tf.constant(
            ['abcd', 'bcde', 'klmp', 'fghi', 'kfcf'])
        target_weights = 1.0 - target_paddings
        # ids/labels/weights/paddings are all in [batch, time] shape.
        targets = py_utils.NestedMap({
            'ids': target_ids,
            'labels': target_labels,
            'weights': target_weights,
            'paddings': target_paddings,
            'transcripts': target_transcripts,
        })
        metrics, per_sequence_loss = dec.FPropWithPerExampleLoss(
            encoder_outputs, targets)
        loss = metrics['loss']

        return loss, per_sequence_loss
예제 #3
0
 def testDecoderConstruction(self):
     """Test that decoder can be constructed from params."""
     p = self._DecoderParams(
         vn_config=py_utils.VariationalNoiseParams(None, True, False))
     _ = decoder.AsrDecoder(p)