Exemplo n.º 1
0
    def test_partition(self):
        with self.cached_session():
            partition_enabled_initializers = [
                initializers.ZerosV2(),
                initializers.OnesV2(),
                initializers.RandomUniformV2(),
                initializers.RandomNormalV2(),
                initializers.TruncatedNormalV2(),
                initializers.LecunUniformV2(),
                initializers.GlorotUniformV2(),
                initializers.HeUniformV2()
            ]
            for initializer in partition_enabled_initializers:
                got = initializer(shape=(4, 2),
                                  partition_shape=(2, 2),
                                  partition_offset=(0, 0))
                self.assertEqual(got.shape, (2, 2))

            partition_forbidden_initializers = [
                initializers.OrthogonalV2(),
                initializers.IdentityV2()
            ]
            for initializer in partition_forbidden_initializers:
                with self.assertRaisesRegex(
                        ValueError,
                        "initializer doesn't support partition-related arguments"
                ):
                    initializer(shape=(4, 2),
                                partition_shape=(2, 2),
                                partition_offset=(0, 0))
Exemplo n.º 2
0
 def test_normal(self):
   tensor_shape = (8, 12, 99)
   with self.cached_session():
     self._runner(
         initializers.RandomNormalV2(mean=0, stddev=1, seed=153),
         tensor_shape,
         target_mean=0.,
         target_std=1)
    def test_embedding_lookup_sparse_with_initializer(self):
        id = 0
        embed_dim = 8
        elements_num = 262144
        for initializer, target_mean, target_stddev in [
            (init_ops.random_normal_initializer(0.0, 0.001), 0.0, 0.001),
            (init_ops.truncated_normal_initializer(0.0, 0.001), 0.0, 0.00088),
            (keras_init_ops.RandomNormalV2(mean=0.0,
                                           stddev=0.001), 0.0, 0.001),
        ]:
            with self.session(config=default_config,
                              use_gpu=test_util.is_gpu_available()):
                id += 1
                embedding_weights = de.get_variable(
                    "emb-init-bugfix-" + str(id),
                    key_dtype=dtypes.int64,
                    value_dtype=dtypes.float32,
                    devices=_get_devices() * 3,
                    initializer=initializer,
                    dim=embed_dim,
                )

                ids = np.random.randint(
                    -0x7FFFFFFFFFFFFFFF,
                    0x7FFFFFFFFFFFFFFF,
                    elements_num,
                    dtype=np.int64,
                )
                ids = np.unique(ids)
                ids = constant_op.constant(ids, dtypes.int64)
                vals_op = de.embedding_lookup(embedding_weights, ids,
                                              None).eval()

                mean = self.evaluate(math_ops.reduce_mean(vals_op))
                stddev = self.evaluate(math_ops.reduce_std(vals_op))
                rtol = 2e-5
                atol = rtol
                self.assertTrue(not (list(vals_op[0]) == list(vals_op[1])))
                self.assertAllClose(target_mean, mean, rtol, atol)
                self.assertAllClose(target_stddev, stddev, rtol, atol)
Exemplo n.º 4
0
  def test_safe_embedding_lookup_sparse_with_initializer(self):
    id = 0
    embed_dim = 8
    dense_shape = np.array([64, 128, 32])
    total_space = 64 * 128 * 32
    elements_num = int(total_space * 0.50)
    for initializer, target_mean, target_stddev in [
        (init_ops.random_normal_initializer(0.0, 0.001), 0.0, 0.00029),
        (init_ops.truncated_normal_initializer(0.0, 0.001), 0.0, 0.00029),
        (keras_init_ops.RandomNormalV2(mean=0.0, stddev=0.001), 0.0, 0.00029),
    ]:
      with self.session(config=default_config,
                        use_gpu=test_util.is_gpu_available()):
        id += 1
        embedding_weights = de.get_variable(
            "safe-init-bugfix-" + str(id),
            key_dtype=dtypes.int64,
            value_dtype=dtypes.float32,
            devices=_get_devices() * 3,
            initializer=initializer,
            dim=embed_dim,
        )

        indices_1d = np.random.randint(0, total_space, elements_num)
        indices_1d = np.unique(indices_1d)
        indices_1d.sort()
        indices_3d = []
        for _i in range(indices_1d.size):
          a_indice = []
          quotient = int(indices_1d[_i] / (128 * 32))
          remainder = indices_1d[_i] % (128 * 32)
          a_indice.append(quotient)
          quotient = int(remainder / 32)
          remainder = remainder % 32
          a_indice.extend([quotient, remainder])
          indices_3d.extend([a_indice])
        indices_3d = np.array(indices_3d)

        ids = np.random.randint(
            -0x7FFFFFFFFFFFFFFF,
            0x7FFFFFFFFFFFFFFF,
            indices_1d.size,
            dtype=np.int64,
        )

        sparse_ids = sparse_tensor.SparseTensor(
            constant_op.constant(indices_3d, dtypes.int64),
            constant_op.constant(ids, dtypes.int64),
            constant_op.constant(dense_shape, dtypes.int64),
        )
        vals_op = de.safe_embedding_lookup_sparse(embedding_weights,
                                                  sparse_ids,
                                                  None,
                                                  combiner="mean").eval()

        mean = self.evaluate(math_ops.reduce_mean(vals_op))
        stddev = self.evaluate(math_ops.reduce_std(vals_op))
        rtol = 2e-4
        atol = rtol
        self.assertTrue(not (vals_op[0][0][0] == vals_op[0][0][1]))
        self.assertAllClose(target_mean, mean, rtol, atol)
        self.assertAllClose(target_stddev, stddev, rtol, atol)