def testVariableDevicePlacement(self):
    classes = np.random.randint(5, size=(20000,))  # Uniformly sampled
    target_dist = [0.9, 0.05, 0.05, 0.0, 0.0]
    with ops.device(
        device_setter.replica_device_setter(ps_tasks=1, ps_device="/cpu:0")):
      dataset = (dataset_ops.Dataset.from_tensor_slices(classes)
                 .shuffle(200, seed=21)
                 .map(lambda c: (c, string_ops.as_string(c))))
      dataset = dataset_ops.rejection_resample(
          dataset, target_dist=target_dist, initial_dist=None,
          class_func=lambda c, _: c, seed=27)

      self.assertEqual(1, len(variables.local_variables()))
      self.assertEqual(b"",
                       compat.as_bytes(variables.local_variables()[0].device))
    def testVariableDevicePlacement(self):
        classes = np.random.randint(5, size=(20000, ))  # Uniformly sampled
        target_dist = [0.9, 0.05, 0.05, 0.0, 0.0]
        with ops.device(
                device_setter.replica_device_setter(ps_tasks=1,
                                                    ps_device="/cpu:0")):
            dataset = (dataset_ops.Dataset.from_tensor_slices(classes).shuffle(
                200, seed=21).map(lambda c: (c, string_ops.as_string(c))))
            dataset = dataset_ops.rejection_resample(dataset,
                                                     target_dist=target_dist,
                                                     initial_dist=None,
                                                     class_func=lambda c, _: c,
                                                     seed=27)

            self.assertEqual(1, len(variables.local_variables()))
            self.assertEqual(
                b"", compat.as_bytes(variables.local_variables()[0].device))
Exemple #3
0
  def _testDistribution(self, initial_known):
    classes = np.random.randint(5, size=(20000,))  # Uniformly sampled
    target_dist = [0.9, 0.05, 0.05, 0.0, 0.0]
    initial_dist = [0.2] * 5 if initial_known else None
    iterator = dataset_ops.Iterator.from_dataset(
        dataset_ops.rejection_resample(
            (dataset_ops.Dataset.from_tensor_slices(classes)
             .shuffle(200, seed=21)
             .map(lambda c: (c, string_ops.as_string(c)))),
            target_dist=target_dist,
            initial_dist=initial_dist,
            class_func=lambda c, _: c,
            seed=27))
    init_op = iterator.initializer
    get_next = iterator.get_next()
    variable_init_op = variables.global_variables_initializer()

    with self.test_session() as sess:
      sess.run(variable_init_op)
      sess.run(init_op)
      returned = []
      with self.assertRaises(errors.OutOfRangeError):
        while True:
          returned.append(sess.run(get_next))

    returned_classes, returned_classes_and_data = zip(*returned)
    _, returned_data = zip(*returned_classes_and_data)
    self.assertAllEqual([compat.as_bytes(str(c))
                         for c in returned_classes], returned_data)
    total_returned = len(returned_classes)
    # Subsampling rejects a large precentage of the initial data in
    # this case.
    self.assertGreater(total_returned, 20000 * 0.2)
    class_counts = np.array([
        len([True for v in returned_classes if v == c])
        for c in range(5)])
    returned_dist = class_counts / total_returned
    self.assertAllClose(target_dist, returned_dist, atol=1e-2)
Exemple #4
0
    def _testDistribution(self, initial_known):
        classes = np.random.randint(5, size=(20000, ))  # Uniformly sampled
        target_dist = [0.9, 0.05, 0.05, 0.0, 0.0]
        initial_dist = [0.2] * 5 if initial_known else None
        iterator = dataset_ops.Iterator.from_dataset(
            dataset_ops.rejection_resample(
                (dataset_ops.Dataset.from_tensor_slices(classes).shuffle(
                    200, seed=21).map(lambda c: (c, string_ops.as_string(c)))),
                target_dist=target_dist,
                initial_dist=initial_dist,
                class_func=lambda c, _: c,
                seed=27))
        init_op = iterator.initializer
        get_next = iterator.get_next()
        variable_init_op = variables.global_variables_initializer()

        with self.test_session() as sess:
            sess.run(variable_init_op)
            sess.run(init_op)
            returned = []
            with self.assertRaises(errors.OutOfRangeError):
                while True:
                    returned.append(sess.run(get_next))

        returned_classes, returned_classes_and_data = zip(*returned)
        _, returned_data = zip(*returned_classes_and_data)
        self.assertAllEqual(
            [compat.as_bytes(str(c)) for c in returned_classes], returned_data)
        total_returned = len(returned_classes)
        # Subsampling rejects a large percentage of the initial data in
        # this case.
        self.assertGreater(total_returned, 20000 * 0.2)
        class_counts = np.array([
            len([True for v in returned_classes if v == c]) for c in range(5)
        ])
        returned_dist = class_counts / total_returned
        self.assertAllClose(target_dist, returned_dist, atol=1e-2)