Example #1
0
  def testUniformDistribution(self):
    shape = [100, 100]
    expect_mean = 0.
    expect_var = 1. / shape[0]
    init = init_ops_v2.VarianceScaling(distribution="uniform")

    with test_util.use_gpu():
      x = self.evaluate(init(shape))

    self.assertNear(np.mean(x), expect_mean, err=1e-2)
    self.assertNear(np.var(x), expect_var, err=1e-2)
Example #2
0
  def testNormalDistribution(self):
    shape = [100, 100]
    expect_mean = 0.
    expect_var = 1. / shape[0]
    init = init_ops_v2.VarianceScaling(distribution="truncated_normal")

    with test_util.use_gpu(), test.mock.patch.object(
        random_ops, "truncated_normal",
        wraps=random_ops.truncated_normal) as mock_truncated_normal:
      x = self.evaluate(init(shape))
      self.assertTrue(mock_truncated_normal.called)

    self.assertNear(np.mean(x), expect_mean, err=1e-2)
    self.assertNear(np.var(x), expect_var, err=1e-2)
Example #3
0
  def testInitializePartition(self):
    partition_shape = (100, 100)
    shape = [1000, 100]
    expect_mean = 0.
    expect_var = 1. / shape[0]
    init = init_ops_v2.VarianceScaling(distribution="untruncated_normal")

    with test_util.use_gpu(), test.mock.patch.object(
        random_ops, "random_normal",
        wraps=random_ops.random_normal) as mock_random_normal:
      x = self.evaluate(init(shape, partition_shape=partition_shape))
      self.assertTrue(mock_random_normal.called)

    self.assertEqual(x.shape, partition_shape)
    self.assertNear(np.mean(x), expect_mean, err=1e-3)
    self.assertNear(np.var(x), expect_var, err=1e-3)