示例#1
0
  def testBiEncoderForwardPassWithInputPacking(self):
    with self.session(use_gpu=False):
      with tf.variable_scope('bienc_test', reuse=tf.AUTO_REUSE):
        bs = 3
        sl = 3
        tf.random.set_seed(8372749040)
        p = self._BiEncoderParams()
        mt_enc = encoder.MTEncoderBiRNN(p)
        packed_params = p.Copy()
        packed_params.packed_input = True
        mt_enc_packed = encoder.MTEncoderBiRNN(packed_params)

        batch = py_utils.NestedMap()
        batch.ids = tf.constant(
            np.random.randint(low=0, high=15, size=[bs, sl], dtype=np.int32))
        batch.paddings = tf.zeros([bs, sl])

        packed_batch = py_utils.NestedMap()
        packed_batch.ids = tf.reshape(batch.ids, [1, -1])
        packed_batch.paddings = tf.reshape(batch.paddings, [1, -1])
        packed_batch.segment_ids = tf.constant(
            [[0, 0, 0, 1, 1, 1, 2, 2, 2]], dtype=tf.float32)
        packed_batch.segment_pos = tf.constant(
            [[0, 1, 2, 0, 1, 2, 0, 1, 2]], dtype=tf.int32)
        enc_out = mt_enc.FPropDefaultTheta(batch).encoded
        enc_out = tf.transpose(enc_out, [1, 0, 2])

        packed_enc_out = mt_enc_packed.FPropDefaultTheta(packed_batch)
        packed_enc_out = tf.reshape(packed_enc_out.encoded, tf.shape(enc_out))

        self.evaluate(tf.global_variables_initializer())
        actual_enc_out, actual_packed_enc_out = self.evaluate(
            [enc_out, packed_enc_out])
        self.assertAllClose(actual_packed_enc_out, actual_enc_out)
示例#2
0
    def testBiEncoderForwardPassWithTransparent(self):
        with self.session(use_gpu=False):
            tf.set_random_seed(8372749040)
            p = self._BiEncoderParams()
            p.is_transparent = True
            mt_enc = encoder.MTEncoderBiRNN(p)
            batch = py_utils.NestedMap()
            batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
            batch.paddings = tf.zeros([2, 4])
            enc_out = mt_enc.FPropDefaultTheta(batch).encoded

            tf.global_variables_initializer().run()
            actual_enc_out = enc_out.eval()
            # pylint: disable=bad-whitespace,bad-continuation
            # pyformat: disable
            expected_enc_out = [[[-4.95129862e-05, 1.92920983e-04],
                                 [-2.18278728e-10, -1.56396545e-05]],
                                [[1.80083007e-04, 6.55014810e-05],
                                 [8.51639197e-05, 6.82225800e-05]],
                                [[2.12642481e-05, -2.92667974e-05],
                                 [-8.88068098e-05, 5.24776005e-05]],
                                [[-1.33993672e-04, 3.61708371e-05],
                                 [-1.35903974e-04, 1.31157576e-05]]]
            # pyformat: enable
            # pylint: enable=bad-whitespace,bad-continuation
            self.assertAllClose(expected_enc_out, actual_enc_out)
示例#3
0
    def testBiEncoderForwardPassWithDropout(self):
        with self.session(use_gpu=False):
            tf.set_random_seed(8372749040)
            p = self._BiEncoderParams()
            p.dropout_prob = 0.5
            mt_enc = encoder.MTEncoderBiRNN(p)
            batch = py_utils.NestedMap()
            batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
            batch.paddings = tf.zeros([2, 4])
            enc_out = mt_enc.FPropDefaultTheta(batch).encoded

            tf.global_variables_initializer().run()
            actual_enc_out = enc_out.eval()
            print('bi_enc_actual_enc_out_with_dropout',
                  np.array_repr(actual_enc_out))
            # pylint: disable=bad-whitespace,bad-continuation
            # pyformat: disable
            expected_enc_out = [[[-2.25614094e-05, 1.19781353e-05],
                                 [-2.74532852e-07, 8.17993077e-06]],
                                [[2.66865045e-05, 1.02941645e-04],
                                 [1.51371260e-05, 3.78371587e-05]],
                                [[3.50117516e-05, 7.65562072e-06],
                                 [-1.30227636e-05, 3.01171349e-06]],
                                [[2.27566215e-06, 1.42354111e-07],
                                 [1.04521234e-06, 2.50320113e-06]]]
            # pyformat: enable
            # pylint: enable=bad-whitespace,bad-continuation
            self.assertAllClose(expected_enc_out, actual_enc_out)
示例#4
0
    def testBiEncoderForwardPassWithTransparent(self):
        with self.session(use_gpu=False):
            tf.random.set_seed(8372749040)
            p = self._BiEncoderParams()
            p.is_transparent = True
            mt_enc = encoder.MTEncoderBiRNN(p)
            batch = py_utils.NestedMap()
            batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
            batch.paddings = tf.zeros([2, 4])
            enc_out = mt_enc.FPropDefaultTheta(batch).encoded

            self.evaluate(tf.global_variables_initializer())
            actual_enc_out = enc_out.eval()
            tf.logging.info(
                'testBiEncoderForwardPassWithTransparent actual_enc_out %r' %
                actual_enc_out)
            expected_enc_out = [[[1.53976856e-04, -1.66475205e-04],
                                 [-1.02031634e-04, 1.39693424e-04]],
                                [[1.62726530e-04, -2.22654475e-04],
                                 [-4.89080339e-05, 1.10912690e-04]],
                                [[1.28586107e-04, -1.62333992e-04],
                                 [7.22907062e-05, -9.17545694e-05]],
                                [[9.02724860e-05, -1.71898617e-04],
                                 [-9.77059244e-06, 7.55862275e-05]]]
            self.assertAllClose(expected_enc_out, actual_enc_out)
示例#5
0
  def testBiEncoderForwardPass(self):
    with self.session(use_gpu=False):
      tf.set_random_seed(8372749040)
      p = self._BiEncoderParams()
      mt_enc = encoder.MTEncoderBiRNN(p)
      batch = py_utils.NestedMap()
      batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
      batch.paddings = tf.zeros([2, 4])
      enc_out = mt_enc.FPropDefaultTheta(batch).encoded

      tf.global_variables_initializer().run()
      actual_enc_out = enc_out.eval()
      expected_enc_out = [[[1.42110639e-06, 1.31101151e-05], [
          -6.62138473e-06, -1.11313329e-06
      ]], [[1.14506956e-05, 2.98347204e-05], [-5.89276988e-06, 5.54328744e-06]],
                          [[1.35346390e-05, 1.00745674e-05],
                           [-4.80002745e-06, -1.23648788e-05]],
                          [[2.00507566e-06, -1.51463591e-05],
                           [-5.71241526e-06, -1.87959231e-05]]]
      self.assertAllClose(expected_enc_out, actual_enc_out)
示例#6
0
  def testBiEncoderForwardPass(self):
    with self.session(use_gpu=False):
      tf.random.set_seed(8372749040)
      p = self._BiEncoderParams()
      mt_enc = encoder.MTEncoderBiRNN(p)
      batch = py_utils.NestedMap()
      batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
      batch.paddings = tf.zeros([2, 4])
      enc_out = mt_enc.FPropDefaultTheta(batch).encoded

      self.evaluate(tf.global_variables_initializer())
      actual_enc_out = enc_out.eval()
      expected_enc_out = [[[4.0744379e-07, -2.0108675e-06],
                           [-4.2056736e-06, 9.2221135e-06]],
                          [[1.2086311e-06, -2.2510878e-07],
                           [-2.2938407e-06, 9.3108029e-06]],
                          [[3.4632390e-06, -3.1495360e-06],
                           [9.1814104e-07, 1.9459947e-06]],
                          [[-9.0593801e-08, -1.2912932e-06],
                           [-5.8420886e-07, -6.5603672e-07]]]
      self.assertAllClose(expected_enc_out, actual_enc_out)
示例#7
0
  def testBiEncoderForwardPassWithTransparent(self):
    with self.session(use_gpu=False):
      tf.random.set_seed(8372749040)
      p = self._BiEncoderParams()
      p.is_transparent = True
      mt_enc = encoder.MTEncoderBiRNN(p)
      batch = py_utils.NestedMap()
      batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
      batch.paddings = tf.zeros([2, 4])
      enc_out = mt_enc.FPropDefaultTheta(batch).encoded

      self.evaluate(tf.global_variables_initializer())
      actual_enc_out = enc_out.eval()
      expected_enc_out = [[[-7.4536911e-05, 8.8465633e-05],
                           [2.8940600e-05, 3.2297492e-05]],
                          [[-1.9775725e-05, 9.8312848e-05],
                           [5.1837378e-05, 1.2998647e-05]],
                          [[4.5528584e-05, -6.8125606e-05],
                           [1.0955606e-04, -2.1024598e-04]],
                          [[8.5454740e-05, -1.8263397e-04],
                           [5.2042866e-05, -1.6407830e-04]]]
      self.assertAllClose(expected_enc_out, actual_enc_out)
示例#8
0
  def testBiEncoderForwardPassWithDropout(self):
    with self.session(use_gpu=False):
      tf.random.set_seed(8372749040)
      p = self._BiEncoderParams()
      p.dropout_prob = 0.5
      mt_enc = encoder.MTEncoderBiRNN(p)
      batch = py_utils.NestedMap()
      batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
      batch.paddings = tf.zeros([2, 4])
      enc_out = mt_enc.FPropDefaultTheta(batch).encoded

      self.evaluate(tf.global_variables_initializer())
      actual_enc_out = enc_out.eval()
      print('bi_enc_actual_enc_out_with_dropout', np.array_repr(actual_enc_out))
      expected_enc_out = [[[1.60383240e-06, 1.22550023e-06],
                           [-7.21660126e-06, 1.05704457e-05]],
                          [[1.42539475e-05, -2.06075638e-05],
                           [-4.98754298e-06, 1.51066461e-05]],
                          [[-7.15192800e-06, -6.44075908e-06],
                           [5.02962678e-07, -3.40795486e-06]],
                          [[-6.54424548e-06, 9.88359807e-06],
                           [1.42836643e-06, -1.68607176e-06]]]
      self.assertAllClose(expected_enc_out, actual_enc_out)
示例#9
0
    def testBiEncoderForwardPass(self):
        with self.session(use_gpu=False):
            tf.random.set_seed(8372749040)
            p = self._BiEncoderParams()
            mt_enc = encoder.MTEncoderBiRNN(p)
            batch = py_utils.NestedMap()
            batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
            batch.paddings = tf.zeros([2, 4])
            enc_out = mt_enc.FPropDefaultTheta(batch).encoded

            self.evaluate(tf.global_variables_initializer())
            actual_enc_out = enc_out.eval()
            tf.logging.info('testBiEncoderForwardPass actual_enc_out %r' %
                            actual_enc_out)
            expected_enc_out = [[[-2.47998378e-06, 7.36457878e-06],
                                 [7.89248020e-07, -2.67464316e-06]],
                                [[-2.98803275e-06, 8.20233890e-06],
                                 [1.00139073e-06, -2.24554151e-06]],
                                [[-5.06675951e-06, 1.15983785e-05],
                                 [-4.58391014e-07, -2.99553108e-07]],
                                [[-4.34937465e-06, 8.58816838e-06],
                                 [-1.74859031e-06, 3.99598093e-06]]]
            self.assertAllClose(expected_enc_out, actual_enc_out)
示例#10
0
    def testBiEncoderForwardPassWithDropout(self):
        with self.session(use_gpu=False):
            tf.random.set_seed(8372749040)
            p = self._BiEncoderParams()
            p.dropout_prob = 0.5
            mt_enc = encoder.MTEncoderBiRNN(p)
            batch = py_utils.NestedMap()
            batch.ids = tf.transpose(tf.reshape(tf.range(0, 8, 1), [4, 2]))
            batch.paddings = tf.zeros([2, 4])
            enc_out = mt_enc.FPropDefaultTheta(batch).encoded

            self.evaluate(tf.global_variables_initializer())
            actual_enc_out = enc_out.eval()
            print('bi_enc_actual_enc_out_with_dropout',
                  np.array_repr(actual_enc_out))
            expected_enc_out = [[[-1.8358192e-05, 1.2103478e-05],
                                 [2.9347059e-06, -3.0652325e-06]],
                                [[-8.1282624e-06, 4.5443494e-06],
                                 [3.0826509e-06, -5.2950490e-06]],
                                [[-4.6669629e-07, 2.4246765e-05],
                                 [-1.5221613e-06, -1.9654153e-06]],
                                [[-1.1511075e-05, 1.9061190e-05],
                                 [-5.7250163e-06, 9.2785704e-06]]]
            self.assertAllClose(expected_enc_out, actual_enc_out)
示例#11
0
 def testTransparentEncoderConstruction(self):
   p = self._BiEncoderParams()
   p.is_transparent = True
   _ = encoder.MTEncoderBiRNN(p)